Chapter 3 Machine-Leve Representation of Programs Guobao Jiang 11210240049@fudan.edu.cn
Chapter 3 Machine-Level Representation of Programs Guobao Jiang 11210240049@fudan.edu.cn
Problem 3. 1(P138 Assume the following values are stored at the indicated memory addresses and registers: Address Value Register Value ox100 OxFF %eax Ox100 Ox104 OXAB ecX Ox1 Ox108 0x13 led Ox3 O×10C O×11 Fill in the following table showing the values for the indicated operands 2021/10/29
Problem 3.1 (P138) • Assume the following values are stored at the indicated memory addresses and registers: • Fill in the following table showing the values for the indicated operands: 2021/10/29 2 Address Value 0x100 0xFF 0x104 0xAB 0x108 0x13 0x10C 0x11 Register Value %eax 0x100 %ecx 0x1 %edx 0x3
Address Value Registervalue 0×100 OXFF % 0x100 0×104 OXAB % ecx 0×1 0×108 0×13 %edx 0×3 0×10C O×11 value Imm(Eb, E, s) Register M[Imm+ R[Eb]+ R[E]'SI XAB Absolute address $Ox1 0×108 mmediate (%eax) OxFF Address o×100 4(%ea×) OXAB Address c×104 9(%eax,%ed×) O×11 Address0×10C 260(%ecX,%ed×) O×13 Address o×108 0×FC(,%ecX,4) OxFF Address0×100 (%eax,%ed×,4) O×11 Address0×10C 2021/10/29
0xFF Address 0x100 0x13 Address 0x108 0x11 Address 0x10C 0xAB Address 0x104 0xFF Address 0x100 0x108 Immediate 0xAB Absolute address 0x100 Register 0x11 Address 0x10C Operand Value %eax 0x104 $0x108 (%eax) 4(%eax) 9(%eax, %edx) 260(%ecx, %edx) 0xFC(, %ecx, 4) (%eax, %edx, 4) 2021/10/29 3 Address Value 0x100 0xFF 0x104 0xAB 0x108 0x13 0x10C 0x11 Register Value %eax 0x100 %ecx 0x1 %edx 0x3 Imm(Eb , Ei , S) M[Imm + R[Eb ] + R[Ei ]*S]
Problem 3.2( P142 you are given the following information. A function with prototype void decode(int xp, int yp, Int zI is compiled into assembly code. The body of the code is as follows: Parameters xp, yp and zp are stored at memory locations with offsets 8, 12, and 16, respectively, relative to the address in register ebl 2021/10/29
Problem 3.2 (P142) • You are given the following information. A function with prototype void decode1(int *xp, int *yp, int *zp) is compiled into assembly code. The body of the code is as follows: … • Parameters xp, yp and zp are stored at memory locations with offsets 8, 12, and 16, respectively, relative to the address in register %ebp 2021/10/29 4
Problem 3.2+ 1 movl 8 (%ebp),%edi 2 movl 12(%ebp), ebx int x =*xp 3 movl 16(%ebp), %esi int y ="yp int z=zp, 4 movI (%edi, %eax yP=X, 5mov(‰ebx),%ed zp=y 6 movl (%esi, %ecx 7mov%eax,(%eb×) 8 movl %edx, (%esi 9 movl %ecx, c%edi Write C code for decode1 that will have an effect equivalent to the assembly code above 2021/10/29
Problem 3.2+ 1 movl 8(%ebp), %edi 2 movl 12(%ebp), %ebx 3 movl 16(%ebp), %esi 4 movl (%edi), %eax 5 movl (%ebx), %edx 6 movl (%esi), %ecx 7 movl %eax, (%ebx) 8 movl %edx, (%esi) 9 movl %ecx, (%edi) • Write C code for decode1 that will have an effect equivalent to the assembly code above. 2021/10/29 5 int x = *xp; int y = *yp; int z = *zp; *yp = x; *zp = y; *xp = z;
Problem 3. 3 (143) Suppose register %eax holds values x and %ecx holds value y. Fill the table Expression Result leal 6(%eax), %edx 6+x leal (%eax, %ecx), %edx x+ y leal (%eax, %ecx, 4), %edx leal 7(%eax, %eax, 8). %edx+9x leal OxA %ecx, 4), %edx 10+4 leal 9(%eax, %ecx, 2),%edx+x+ 2021/10/29
9 + x + 2y 10 + 4y 7 + 9x x + 4y x + y 6 + x Problem 3.3 (143) • Suppose register %eax holds values x and %ecx holds value y. Fill the table … 2021/10/29 6 Expression Result leal 6(%eax), %edx leal (%eax, %ecx), %edx leal (%eax, %ecx, 4), %edx leal 7(%eax, %eax ,8), %edx leal 0xA (, %ecx, 4), %edx leal 9(%eax, %ecx, 2), %edx
Problem 3. 4( P145 Assume the following values are stored at the indicated memory addresses and registers: Address Value Register Value ox100 OxFF %eax Ox100 Ox104 OXAB ecX Ox1 Ox108 0x13 led Ox3 O×10C O×11 Fill in the following table showing the effects of the following instructions 2021/10/29
Problem 3.4 (P145) • Assume the following values are stored at the indicated memory addresses and registers: • Fill in the following table showing the effects of the following instructions … 2021/10/29 7 Address Value 0x100 0xFF 0x104 0xAB 0x108 0x13 0x10C 0x11 Register Value %eax 0x100 %ecx 0x1 %edx 0x3
Address Value Registervalue 0×100 OXFF % 0x100 0×104 OXAB loecx 0×1 0×108 0×13 %edx 0×3 0×10C O×11 Instruction Destination Value 0×116(dec) 0×100 0×100 (16+1)*16=256+ 16=0×110 0×104 OxA8 mul$16,(%eax,%edx,4)0×10C 0×110 nc|8(%ea×) 0x108 0×14 decl %ecx ecx 0×0 subl %edx. %eax eax OXFD 2021/10/29
%eax 0xFD %ecx 0x0 0x108 0x14 0x10C 0x110 0x104 0xA8 0x100 0x100 Instruction Destination Value addl %ecx, (%eax) subl %edx, 4(%eax) imull $16, (%eax, %edx, 4) incl 8(%eax) decl %ecx subl %edx, %eax 2021/10/29 8 Address Value 0x100 0xFF 0x104 0xAB 0x108 0x13 0x10C 0x11 Register Value %eax 0x100 %ecx 0x1 %edx 0x3 0x11 * 16(dec)= (16+1) * 16 = 256 + 16 = 0x 110
Problem 3. 5(P145 Suppose we want to generate assembly code for the following c function: int shift_left2_rightn(int x, int n) X>=n return x: 2021/10/29
Problem 3.5 (P145) • Suppose we want to generate assembly code for the following C Function: int shift_left2_rightn(int x, int n) { x >= n; return x; } 2021/10/29 9
Problem 3. 5+ Parameters x and n are stored af memory locations with offsets 8 and 12 respectively, relative to the address in register %oebp 1 movl 12(%ehn %ecx Get n %ecx Shifts 2 mot amounts between O Get x 3 sa and 31 gre allowe x>=n Note: the right shift should be performed arithmetically 2021/10/29
subl %cl, %eax sall $2, %eax Problem 3.5+ • Parameters x and n are stored at memory locations with offsets 8 and 12, respectively, relative to the address in register %ebp. 1 movl 12(%ebp), %ecx Get n 2 movl 8(%ebp), %eax Get x 3 ______________ x >= n • Note: the right shift should be performed arithmetically. 2021/10/29 10 %ecx ? Shifts amounts between 0 and 31 are allowed