Recitation 1
-
Upload
colorado-dunlap -
Category
Documents
-
view
17 -
download
0
description
Transcript of Recitation 1
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
$ vi add.s
$ objdump –d add.o
Example: asm => c
movl 16(%ebp), %eaxmovl 12(%ebp), %edxsubl %eax, %edxmovl %edx, %eaximull 8(%ebp),%edxsall $31, %eaxsarl $31, %eaxxorl %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;}