Assignment #4: Assembly Language Programming


Assigned: Friday, October 5
Due: Friday, October 12   at the beginning of class

Contents:


Overview

Topic(s): Assembly Language, and the MIPS Instruction Set

Related Reading: class notes, and the MIPS Assembly Language and Simulator links.


Preparation

Read the directions for Getting Started with the MARS simulator.


Problems to be Submitted   (10 points)

When you turn in your assignment, you must include a signed cover sheet (PDF version) with your assignment (you're assignment will not be graded without a completed cover sheet).

You are allowed to submit your assignment via email, but if you choose to do so, you must bring a hardcopy of your assignment along with a completed cover sheet to the instructor at the next class. (Note: Do not email the instructor any .zip file attachments, as SLU's email may not accept these emails; i.e. the instructor may not receive your email.)

  1. (3 points)    Interpreting Assembly Instructions

    Indicate what registers are modified by the following instructions, and what their new values will be:

    Assume that the initial values in the registers are:

        $s0       = 0x1F
        $s2       = 0x62
        $s4       = 0x0C
        $t1       = 0x47
    
    1.   add    $s2, $s2, $s0
    2.   sll    $t1, $t1, 2
    3.   addi   $s4, $s4, -6
  2. (4 points)    Understanding Assembly Control Flow

    Given the following snippet of AVR assembly code (which you can also download: hwk4_problem2.asm), answer the questions below:

    Note: In this program, N is a variable stored in memory.

        li     $t0, 9
        li     $t4, 0
        lw     $s3, N		  # 'N' is a variable stored in memory (with some unknown value)
    
    Loop:
        beq    $s3, $t4, Exit
        addi   $t0, $t0, 4
        addi   $s3, $s3, -1
        j      Loop
    
    Exit:
        sw     $t0, f_N            # 'f_N' is the result, stored as the variable 'f_N' in memory
    
    
    1. Similar to the last problem on homework #3, create a trace of the execution of the program for N = 3. In the trace, for each instruction list its address (i.e. value of the PC register prior to executing that instruction) and the register values in $s3, $t0, and $t4 (after executing that instruction).
    2. All the instructions except li $t0, 9 and addi $t0, $t0, 4 control iteration of the loop.   How many times will the loop iterate if N is 3? (i.e. how many times will the body of the loop -- the two addi instructions and the j instruction -- be executed if N is 3?)?   How about if N is 7?   In general, how many times will the loop iterate for any value of N?
    3. This program implements a mathematical function, f(N), that creates a number series. What are the resulting values for f(0), f(1), f(2), and f(3)? In general, what is the function f(N) in terms of any value of N?
  3. (3 points)    Assembly-to-Mathematical Expression Conversion

    Given the following list of potential mathematical expressions:

        (1)   y = -3 * x
        (2)   y = 8 * x - 3
        (3)   y = (x * 4) + (x - 8)
        (4)   y = -1
        (5)   y = (4 * x - 1) / 8
        (6)   y = (8 * x) + (x - 3)
        (7)   y = 4 * x - 8
        (8)   y = (x / 8) + (4 * x)
        (9)   y = x - 12
    

    Which of the above mathematical expressions does each of the following assembly code sequences correspond to?

          Note: I would recommend that you try to figure out the answers to these problems without using the simulator. But you may want to use the simulator to verify your answers (e.g. here is the code for part b: hwk4_problem3b.asm).

    1.     lw     $s0, x
          li     $s4, 3
          sll    $s0, $s0, 3
          sub    $s0, $s0, $s4
          sw     $s0, y
      
    2.     lw     $s0, x
          li     $s4, 3
          mul    $s4, $s4, 4
          sub    $s0, $s0, $s4
          sw     $s0, y
      
    3.     lw     $s0, x
          mul    $s0, $s0, 4
          addi   $s0, $s0, -1
          srl    $s0, $s0, 3
          sw     $s0, y