#### Assignment 2

##### Due: 9:00am, Tue Jan 30th, 2024

Note: Make reasonable assumptions where necessary and clearly state them. Feel free to discuss problems with classmates, but the only written material that you may consult while writing your solutions are the textbook and lecture slides/videos. Solutions should be uploaded on Gradescope. Show your solution steps so you receive partial credit for incorrect answers and we know you have understood the material. Don't just show us the final answer.

Every homework has an automatic penalty-free 1.5 day extension to accommodate any covid/family-related disruptions. In other words, try to finish your homework by Tuesday 9:00am to keep up with the lecture content, but if necessary, you may take until Wednesday 11:59pm.

1. Annotate the following MIPS instructions to indicate source registers and destination registers. A source register is read during the instruction's execution, while a destination register is written during the instruction's execution. (12 points)
2. sub \$t3, \$t1, \$t4
4. lw \$s1, 4(\$gp)
5. sw \$s1, 12(\$gp)
6. bne \$t1, \$s2, loop1
2. Consider a program that declares global integer variables x, y, z, w[10]. Assume that an integer occupies 4 bytes. These variables are allocated starting at a base address of decimal 2000. All these variables have been initialized to decimal 10. The base address 2000 has been placed in \$gp. The program executes the following assembly instructions:
lw \$s1, 0(\$gp)
lw \$s2, 4(\$gp)
sub \$s1, \$s1, \$s2
sw \$s1, 8(\$gp)
sw \$s2, 12(\$gp)
subi \$s2, \$s3, 120
sw \$s2, 16(\$gp)
1. What are the memory addresses of variables x, y, z, w[0], and w[1]? (15 points)
2. What are the values of variables x, y, z, w[0], and w[1] at the end of the program? Explain this by adding comments to the code to show the effect of each instruction. (20 points)
3. Express the following decimal number in binary and hexadecimal forms: 194. Show your steps. (6 points)
4. Express the following binary number in decimal and hexadecimal forms: 10111100. Show your steps. (6 points)
5. Express the following hexadecimal number in decimal and binary forms: 0x9d. Show your steps. (6 points)
6. Write the MIPS assembly code that corresponds to the pseudo code below. Assume that the address for integer i is baseaddress+4 and the address for a[0] is baseaddress+8. Assume that the baseaddress is stored in \$gp. The code initializes i to 0; it then iterates from i=0 to i=9, setting a[i] = 32*i in each iteration. To make your code efficient, i must be loaded into a register at the start, and it must be updated in memory only after you've finished the for loop. You may not use a multiply instruction.
for (i=0; i<10; i++)
a[i] = 32*i;
(35 points)