CSE 410 22wi Homework 2 Solution

Out: Friday, 14 January
Due: Friday, 21 January
Turnin: Gradescope

This is a "paper and pencil" assignment about data and instruction representation. Turn-in is online, preferably as a pdf file.

  1. Consider the following assembler program:

            .text
            addi    x10, x10, 21
            andi    x11, x10, 6
            xor     x12, x10, x11
            lw      x13, X(x0)
            slli    x14, x13, 1
            and     x14, x14, x13
    done:
            .data
    X:      .word   49
        

    1. Assemble this program by hand to produce machine instructions. Give the instructions as hexadecimal numbers in your answer.

      You may find the RISC-V resources referenced in the course documentation page to be useful.
      NOTE: The actual RISC-V lw instruction differs from the one described in class and implemented by the simulator. For this question, assume that addresses in the .data section start at 0, and that the effective address generated by a lw is the address (index) of a word in the data section, exactly as it works in our simulator.

              .text
              addi    x10, x10, 21    # 0x01550513
              andi    x11, x10, 6     # 0x00657593
              xor     x12, x10, x11   # 0x00b54633
              lw      x13, X(x0)      # 0x00002683
              slli    x14, x13, 1     # 0x00169713
              and     x14, x14, x13   # 0x00d77733
      done:
              .data
      X:      .word   49
          

    2. What are the values in registers 13 and 14 after the code runs? (Assume it stops when it reaches label done.) Give your answers in hex.

      x13: 0x00000031
      x14: 0x00000020

  2. Assuming these bit strings are instructions, express their meaning in assembler:
          fd010113   addi x2,x2,-48
          02112623   sw   x1,44(x2)
        
  3. Suppose register 5 holds the value 0x13579BDF.
    1. What is the minimum value that when added to register 5 causes overflow when interpretting the values as signed integers?
      0x6CA86421
    2. What is the minimum value that when added to register 5 causes overflow when interpretting the values as unsigned integers?
      0xECA86421

  4. In this assembler code:
              add   x10,x11,x12
              slti  x13, x12, 0
              slt   x14, x10, x11
              bne   x13, x14, skip
              ...
    skip:
            
    What is the purpose of the three instructions that follow the add instruction?
    (You will have to look up the meanings of the slti and slt instructions. The resources in the documentation section may help, as might just a search.)

    The code is testing for signed integer overflow. If it branches to label skip then overflow occurred, otherwise not. (Parasphrasing the code, it says if the value in x12 is less than 0, then the result should be less than the value of the other operand, x11. If the value of x12 is greater than or equal to 0 then the result should be greater than or equal to operand x11.)

  5. What is the largest immediate value that can be used in an addi instruction?
    The immediate field in the instruction is 12 bits wide. Because the immediate is stored as a two's complement number, the high order bit must be 0. The largest value is therefore 0x7FF, which is 2,047 in decimal.

  6. What is the value of 0X5768793F as ASCII characters?
    Why?

  7. Write assembler that sets x4 to 0 if x3 is divisible by 8 and to 1 otherwise, without using any multiple or divide instructions.
    
                 slli    x4,x3,29      # get rid of all but lowest 3 bits
                 beq     x4,x0,done    # is value now zero?
                 addi    x4,x0,1       # if not, not a multiple of 8
           done: