Assignment 2

Due: 9:00am, Tue Jan 31st, 2023

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)
    1. add $t4, $t2, $t1
    2. sub $t3, $zero, $t4
    3. addi $t4, $t3, 100
    4. lw $s2, 4($gp)
    5. sw $s2, 8($gp)
    6. bne $t4, $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 4000. All these variables have been initialized to decimal 100. The base address 4000 has been placed in $gp. The program executes the following assembly instructions:
    lw $s1, 0($gp)
    lw $s2, 4($gp)
    add $s1, $s1, $s1
    add $s2, $s2, $s1
    sw $s1, 12($gp)
    sw $s2, 16($gp)
    subi $s2, $s2, 80
    sw $s2, 8($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: 171. (6 points)
  4. Express the following binary number in decimal and hexadecimal forms: 11000110. (6 points)
  5. Express the following hexadecimal number in decimal and binary forms: 0xd7. (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] = 128i 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] = 128*i;
    (35 points)