13
Recitation 1 Session 5 Ishani Chakraborty

Recitation 1

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Recitation 1

Recitation 1

Session 5

Ishani Chakraborty

Page 2: Recitation 1

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)

Page 3: Recitation 1

Assembly code

• Generated by –s option$ gcc –O2 –S code.c

• Using GCC compiler, the assembly code is generated in GAS format (GNU Assembler)

Page 4: Recitation 1

#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

Page 5: Recitation 1

$ vi add.s

Page 6: Recitation 1

$ objdump –d add.o

Page 7: Recitation 1

Example: asm => c

movl 16(%ebp), %eaxmovl 12(%ebp), %edxsubl %eax, %edxmovl %edx, %eaximull 8(%ebp),%edxsall $31, %eaxsarl $31, %eaxxorl %edx, %eax

Page 8: Recitation 1

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;}

Page 9: Recitation 1

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),…

Page 10: Recitation 1

• 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

….

Page 11: Recitation 1
Page 12: Recitation 1

int absdiff2(int x, int y){int result;

if (x< y) result = y-x;else result = x-y;return result;}

Page 13: Recitation 1