TOTAL DEDUCTIONS (score given top right)

CSE2421 HW 3 Su13 Grade Sheet NAME ____________________________________________________ PARTNER __________________________________________________ DED...
Author: Adam Henry
28 downloads 1 Views 289KB Size
CSE2421 HW 3 Su13 Grade Sheet NAME ____________________________________________________ PARTNER __________________________________________________ DEDUCTIONS ___________ Question #1. 20 points ___________ Question #2. 10 points ___________ Question #3. 10 points ___________ Question #4. 10 points ___________ Question #5. 10 points ___________ Question #6. 10 points ___________ Question #7. 15 points ___________ Question #8. 15 points ___________

TOTAL DEDUCTIONS (score given top right)

Score __________/100

SUMMER 2013 CSE2421 HOMEWORK#3

Due Date: 7/24/2013 IN CLASS

1. (20 points) For the given Y86 code: (a) explain what the code does (b) convert the code to a C function (c) comment each statement, len2: pushl %ebp rrmovl %esp, %ebp pushl %esi irmovl $4, %esi pushl %edi irmovl $1, %edi mrmovl 8(%ebp), %edx irmovl $0, %ecx mrmovl (%edx), %eax addl %esi, %edx andl %eax, %eax je Done Loop: addl %edi, %ecx mrmovl (%edx), %eax addl %esi, %edx andl %eax, %eax jne Loop Done: rrmovl %ecx, %eax popl %edi popl %esi rrmovl %ebp, %esp popl %ebp ret

... 8 pts # Save %ebp # New frame pointer # Save caller register # Constant 4 # Save # Constant 1 # Get a # len = 0 # Get *a # a++ # Test *a # If zero, goto Done # len++ # Get *a # a++ # Test *a # If !0, goto Loop # return length # Restore %edi # Restore %esi # Restore stack pointer # Restore frame pointer # return to calling routine

int len1(int a[])…. 2pts { int len; …. 1 pt for (len=0; a[len]; len++); … 3 pts return len; … 1 pt } // find number of elements in a null terminated list (5 pts) 2. (10 pts) Consider the following Y86 Assembly-language program: .pos 0x12345676 xorl %edx, %edx here: mrmovl here(%edx), %esi halt

What value will be in register %esi once the program terminates? Justify your answer. 76060000 or 78563412…. 5 pts value 5 pts for explanation Solution : The first instruction initializes register %edx to 0, and hence the mrmovl instruction moves four bytes (starting from location “here”) into register %esi. To know which four bytes these are, we need to determine the machine language representation of the mrmovl instruction because that is what “here” is pointing to. Its instruction code is 50, followed by the number corresponding to register rA (%esi = 6), then the number corresponding to register rB (%edx = 2), and finally the value of the label “here” which is 0x676 i.e. 0x00000676; and in little endian form 76 06 00 00. So the instruction is encoded to be 506276060000, however, only 4 bytes fit into a register with the top two bytes being truncated  %esi = 76060000. If you didn’t check it on the system, then it was okay if you assumed that the address, based on the .pos statement, was 12345678 and stored little endian. 3. (10 points) The following Y86 machine codes start at memory address 0x300, which is also the address of the label “loop”. Write corresponding Y86 assembly instructions. 0x300: loop: 0x300: 40 50 1c 04 00 00

________________________________________

0x306: 61 30

________________________________________

0x308: 73 00 03 00 00

________________________________________

ANSWERS: rmmovl %ebp, 0x41c(%eax) …. 5 pts subl %ebx, %eax …. 3 pts je 0x300 --- 2 pts also je loop 4. (10 points) The sequential Y86 implementation discussed in class has Fetch, Decode, Execute, Memory, Write-back and PC-update stages, in this order. List one Y86 instruction that could not be executed if the Write-back stage came before the Memory stage, and explain why. Solution : The two instructions that could not be executed are mrmovl and popl: both of these read a value from memory that is then stored in the register file. Swapping the Write-back and Memory stages would force us to store in a register a value that has not yet been read from memory.

5. (10 points) Consider the following fragment of IA32 code from the C standard library: 0x400446e3 : call 0x400446e8 0x400446e8 : popl %eax After the popl instruction completes, what hex value does register %eax contain? Highlighted :o) Since the call pushes the return address onto the top of the stack, then 0x400446e8 is on the top of the stack as it is the instruction following the call statement. However, the call statement then puts the call location, 0x40046e8, into the PC so the PC executes this instruction… which happens to be the next instruction (i.e. the popl instruction). Since the return address is on the top of the stack because of the call and the return address is the current address, the value popped into eax is the return address that was pushed by the call.

6. (10 points) Consider the source IA32 code below, where M and N are constants declared with #define. int array1[M][N]; int array2[N][M]; void copy(int i, int j) { array1[i][j] = array2[j][i]; } Suppose the above code generates the following assembly code: copy: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ecx movl 12(%ebp),%eax leal 0(,%eax,4),%ebx leal 0(,%ecx,8),%edx subl %ecx,%edx addl %ebx,%eax sall $2,%eax movl array2(%eax,%ecx,4),%eax movl %eax,array1(%ebx,%edx,4) popl %ebx movl %ebp,%esp popl %ebp ret What are the values of M and N? M = 5 N = 7…. 5 points each

6. The following is a silly recursive procedure: int silly(int n, int *p) { int val, val2; if (n > 0) val2 = silly(n

Suggest Documents