Assignment 3

Due: 10:45am, Tue Feb 6th, 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 10:45am to keep up with the lecture content, but if necessary, you may take until Wednesday 11:59pm.

  1. Read the assembly code below; add comments to explain what each line of code is doing; in one sentence, explain what this procedure is trying to accomplish. (15 points)

    new-proc:
    sll $a0, $a0, 24
    srl $a0, $a0, 24
    add $v0, $a0, $zero
    jr $ra

  2. Read the assembly code below; add comments to explain what each line of code is doing; provide a simple equation to express the return value v0 as a function of input arguments a0 and a1. (30 points)

    new-proc:
    blt $a1, $zero, loop2
    loop1:
    beq $a1, $zero, proc-end
    sll $a0, $a0, 1
    addi $a1, $a1, -1
    j loop1
    loop2:
    beq $a1, $zero, proc-end
    srl $a0, $a0, 1
    addi $a1, $a1, 1
    j loop2
    proc-end:
    add $v0, $a0, $zero
    jr $ra

  3. For the (pseudo) assembly code below, replace X, Y, P, and Q with the smallest set of instructions to save/restore values on the stack and update the stack pointer. Assume that procA and procB were written independently by two different programmers who are following the MIPS guidelines for caller-saved and callee-saved registers. In other words, the two programmers agree on the registers handling input arguments and return value of procB, but they can't see the code written by the other person. Assume that $fp isn't being used by either procA or procB. Be sure to read the class notes first so you understand the MIPS guidelines for caller-saved and callee-saved registers. (40 points)
    procA:
    $s0 = ...
    $s1 = ...
    $s2 = ...
    $s3 = ...
    $t0 = ...
    $t1 = ...
    $t2 = ...
    $t3 = ...
    X
    $a0 = ...
    $a1 = ...
    jal procB
    Y
    ... = $s3
    ... = $t1
    ... = $t2
    ... = $a0 (this is the value that was originally passed to procA as an argument)
    jr $ra

    procB:
    P
    ... = $a0
    ... = $a1
    $s1 = ...
    $s2 = ...
    $s3 = ...
    $t0 = ...
    $t1 = ...
    Q
    jr $ra

  4. Download and install the MARS simulator. Read this Google doc before you start. Create a new file and copy-paste the code below into the Edit window. Execute the program. The program should print "BOO7" at the bottom. Take a screenshot of your MARS screen. Annotate this image to show that one of the registers and one of the memory locations has the integer 7, and three registers and three memory locations have the ASCII codes for the three characters "B", "O", and "O". Your annotation can be as simple as a circle with the corresponding label (7, B, O, O). (15 points)


    .data

    str: .asciiz "BOO"
    myint: .word 7

    .text

    li $v0, 4 # load immediate; 4 is the code for print_string
    la $a0, str # the print_string syscall expects the string
    # address as the argument; la is the instruction
    # to load the address of the operand (str)
    syscall # MARS will now invoke syscall-4.
    # This should print the string on the bottom of the screen.
    lb $t1, ($a0) # load the byte at address $a0 into register $t1
    lb $t2, 1($a0) # load the byte at address $a0+1 into register $t2
    lb $t3, 2($a0) # load the byte at address $a0+2 into register $t3
    li $v0, 1 # syscall-1 corresponds to print_int
    lw $a0, myint # Bring the value at label myint to register $a0.
    # print_int expects the integer to be printed in $a0.
    syscall # MARS will now invoke syscall-1