Fall Term 2016 SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C Assigned on: 20th Oct 2016 Due by: 27th Oct 2016 Pen & Paper exercise Assembly Code Fragments Consider the following pairs of C functions and assembly code. Fill in the missing instructions in the assembly code fragments (one instruction per blank). Your answers should be correct x86 64 assembly code. a) int f1(int a, int b) f1: pushq %rbp %rsp, %rbp urn a - b; %rbp, %rsp popq %rbp b) int f2(int a) f2: pushq %rbp %rsp, %rbp leal %rbp, %rsp urn a*5; popq %rbp c) int f3(int a) f3: pushq %rbp %rsp, %rbp
if (a <= 0) %edi, %eax urn -a; jle.l11 else.l8: %rbp, %rsp urn a; popq %rbp.l11: negl %eax jmp.l8 Conditional branches What is the value of %eax, when the last label (respectively.l3 and.l17) is reached? First, annotate the assembly code and then, write the corresponding C-statements! i) Assume %eax := a, %edx := d. ii) Assume %eax := 1, %ecx := N..L2:.L3: cmpl %eax, %edx jle.l2 subl %eax, %edx %edx, %eax jmp.l3 subl %edx, %eax.l18:.l17: testl %ecx, %ecx jle.l17 xorl %edx, %edx incl %edx addl %eax, %eax cmpl %edx, %ecx jne.l18 For Loop This problem tests your understanding of how for loops in C relate to machine code. Consider the following x86 64 assembly code for a procedure dog(). Based on the assembly code, fill in the blanks in its corresponding C source code. (Note: you may only use symbolic variables x, y, i, and result from the source code in your expressions below. Do not use register names.) dog:.l1:.l2: cmpl %esi, %edi jge.l2 imull %edi, %eax addl $2, %edi cmpl %esi, %edi jl.l1 q int dog(int x, int y) int i, result; result = ; for (i = ; ; ) result = ; urn result; 2
Switch Statement Consider the following C function and assembly code fragments. Which of the assembly code fragments matches the C function shown? C Code int woohoo(int a) int = 0; switch(a) case 11: = 4; case 22: case 55: = 7; case 33: case 44: = 11; default: = 1; urn ; Fragment 1 woohoo: $0, %ecx cmpl $11, %edi jne.l2 $4, %ecx jmp.l3.l2: cmpl $22, %edi jne.l3 $7, %ecx.l3: cmpl $55, %edi jne.l5 $7, %ecx.l5: cmpl $33, %edi sete %al cmpl $44, %edi sete %dl orl %edi, %eax testb $1, %al je.l6 $11, %ecx.l6: %ecx, %eax Fragment 2 woohoo: subl $1, %edi cmpl $4, %edi ja.l2 jmp *.L9(,%edi,4).section.rodata.align 4.L9:.long.L3.long.L5.long.L7.long.L7.long.L5.text.L3: $4, %eax jmp.l2.l5: $7, %eax jmp.l2.l7: $11, %eax.l2: Fragment 3 woohoo: je.l6 je.l7 je.l8 je.l8 je.l7 jmp.l9.l6: $4, %eax.l7: $7, %eax.l8: $11, %eax.l9:.l4: 3
Arrays in Assembly Suppose the start address of a short array A and size t index i are stored in registers %rdi and %rsi, respectively. For each of the following C expressions, give its type, a formula for its value, and an assembly code implementation. The result should be stored in register %rax if it is a pointer and in register element %ax if the result is a short integer. Note that each C expression can be implemented with one single assembly instruction by choosing a suitable addressing mode. A+3 A[5] &A[i] A[4*i+2] A+2*i-7 Structs Consider the following structure declaration: struct line char *id; short rgb[3]; int x; int y; int width; int height; char f; l; a) Draw the memory layout of the structure including the address offset of each field. b) How many total bytes does the structure require? c) Optimize the structure in terms of memory consumption and state the new size. d) The following procedure operates on the structure: void obfuscatedoperation(struct line *lp) = ; = ; Using the original struct from above, the compiler generated the following assembly code for the body of the procedure: sall sall $1, 24(%rdi) $1, 28(%rdi) From this assembly code, fill in the missing lines in obfuscatedoperation() and provide a more meaningful name for the procedure. 4
Assembly to C Express the operations of the following assembly language sequence as a C program. foo: pushq incl popq %rbp %rsp, %rbp %rdi, %rdx %rdi, %rax (%rax), %eax %eax %eax, (%rdx) $0, %eax %rbp, %rsp %rbp Give an example how the function foo can be called (provide type declarations for all parameters). Parameters in Assembly What parameters are expected by function huh and what does it do? huh: pushq %rbp %rsp, %rbp movslq %esi, %rsi leaq 0(,%rsi,4), %rdx $999, (%rdi,%rdx) %rbp, %rsp popq %rbp Hand-in instructions You have 1 week for this assignment. This is a paper exercise. If you want your solution to be revised please hand it in during your exercise class on the due date or send it to your assistent via mail. 5