130 likes | 227 Views
Recitation 1. Session 5 Ishani Chakraborty. Machine Level Representation. What happens to a C program when we compile it ? $ gcc prog.c Preprocessor (expands code) Compiler (source2asm) Assembler (asm2object) Linker (Combines objects to form a single executable). Assembly code.
E N D
Recitation 1 Session 5 Ishani Chakraborty
Machine Level Representation • What happens to a C program when we compile it ? $ gcc prog.c • Preprocessor (expands code) • Compiler (source2asm) • Assembler (asm2object) • Linker (Combines objects to form a single executable)
Assembly code • Generated by –s option $ gcc –O2 –S code.c • Using GCC compiler, the assembly code is generated in GAS format (GNU Assembler)
#include<stdio.h> int accum = 0; int sum(int x, int y) { int t = x+y; accum = accum + t; return t; } add.c • $ gcc –O2 –S add.c (generate asm .s code) • vi add.s • $ gcc –O2 –c add.c (generate binary machine .o code) • $ gcc add.s • $ gcc add.o
Example: asm => c movl 16(%ebp), %eax movl 12(%ebp), %edx subl %eax, %edx movl %edx, %eax imull 8(%ebp),%edx sall $31, %eax sarl $31, %eax xorl %edx, %eax
int asm2c(int x, int y, int z) { int t1 = y - z; int t2 = x * t1; int t3 = (t1 << 31) >> 31; int t4 = t3 ˆ t2; return t4; }
Control Flow • Control the sequence of operations. • Default: Sequential • Non sequential through conditionals/loops/switches etc. • Condition Codes: To perform non-sequential jumps. CF, ZF, SF, OF. • Jump instructions: jmp, je,jne,jg (signed greater), jge, ja (unsigned greater),…
Jump targets are using PC – relative ie., the difference between address of target instruction and instruction following the jump. • eg., what is the jump target for instr 1 and 2 ? (1) 0x08: 7e 11 jle 1b …. …. (2) 0x19: 7f f5 jg 10 ….
int absdiff2(int x, int y) { int result; if (x< y) result = y-x; else result = x-y; return result; }