CSE 410 22wi Homework 1 Solution

Out: Friday, 7 January
Due: Friday, 14 January, 11:59 pm
Turnin: Gradescope

This assignment asks you to write four small assembler programs and verify that they operate correctly by running them on the simulator.

Part 1

Create in file hw1p1.asm an assembly language program with .text and .data segments. The data segment should define six words of storage, labeled A through F (in that order) and having values 10, 11, 12, 13, 14, and 15, in that order. The .text segment should define label main at offset zero. It should contain code that computes the sum of the contents of the memory locations with labels A through F, prints that sum, and then halts execution. The code should not presume what values A through F contain (even though you and I know what they are).

Constraints:

  1. your code cannot use any branches

  .text
main: lw   x10, A
      lw   x11, B
      add  x10, x10, x11
      lw   x11, C
      add  x10, x10, x11
      lw   x11, D
      add  x10, x10, x11
      lw   x11, E
      add  x10, x10, x11
      lw   x11, F
      add  x10, x10, x11
      print x10
      halt
  .data
A:    .word 10
B:    .word 11
C:    .word 12
D:    .word 13
E:    .word 14
F:    .word 15

Part 2

Create in file hw1p2.asm an assembly language program with .text and .data segments. The data segment should initialize six consecutive words of memory to values 10 through 15, in that order. The value 10 should have the label A, but there should be no other labels in the data segment. The text segment establishes label main at offset zero, and contains code that sums the six words initialized in the data segment and then prints the result.

This part is doing something very similar to Part 1, but with two constraints:

  1. your code cannot use any branches.
  2. only the first word of data has a label
The code should look very similar to Part 2. You mainly need to figure out how to name the memory locations to be added when they don't have labels.

When you're done, think about the relationship of what you just did and summing the elements of an array in a higher level language.

       .text
main:  addi x12, x0, A
       lw   x10, 0(x12)
       lw   x11, 1(x12)
       add  x10, x10,x11
       lw   x11, 2(x12)
       add  x10, x10,x11
       lw   x11, 3(x12)
       add  x10, x10,x11
       lw   x11, 4(x12)
       add  x10, x10,x11
       lw   x11, 5(x12)
       add  x10, x10,x11
       print x10
       halt
      .data
A:    .word 10
      .word 11
      .word 12
      .word 13
      .word 14
      .word 15

Part 3

In file hw1p3.asm, re-implement part 2, except that this time your code should be structured similarly to a for loop in Java. That should result in a shorter text segment (fewer instructions). When you run the implementation, compare its dynamic instruction count (how many instructions it executed) to the implementation in Part 2.

Constraints:

  1. only the first word of data has a label
  2. your code can contain only a single lw instruction. (It can use as many branches as you want.)

      .text
main: addi  x10, x0, 0   # accumulator register
      addi  x12, x0, A   # pointer register
      addi  x13, x12, 6  # limit register
loop: lw    x11, 0(x12)
      add   x10, x10,x11
      addi  x12, x12, 1
      blt   x12, x13, loop
      print x10
      halt
      .data
A:    .word 10
      .word 11
      .word 12
      .word 13
      .word 14
      .word 15

Part 4

In this implementation, which goes in file hw1p4.asm, your code sums the words of memory from the data segment starting with the one labeled A and continuing to the label STOP. The sum does not include the word at label STOP.

Use the same six values for the words to be summed as in all the other parts. However, you code cannot rely on the fact that there are six words to be summed -- your code should work for any data segment that has a label A followed eventually by a label STOP.

Constraints:

  1. your code can contain only a single lw instruction
  2. the first word of data to be summed has label A and the word after the last word to be summed has label STOP.

      .text
main: addi  x10, x0, 0    # accumulator register
      addi  x12, x0, A    # base register
      addi  x13, x0, STOP # limit register
      bge   x12, x13, done
loop: lw    x11, 0(x12)
      add   x10, x10,x11
      addi  x12, x12, 1
      blt   x12, x13, loop
done: print x10
      halt
      .data
A:
      .word 10
      .word 11
      .word 12
      .word 13
      .word 14
      .word 15
STOP:

What to Turn In

Turn in your four short assembly programs, named as indicated above, on Canvas.

Appendix: Instructions Implemented by the Simulator

The following RISC-V instructions are implemented by the simulator. Information on what they do can be found in the RISC-V documents in the documentation section of the course web. A simpler and probably sufficient guide is to look at the sample code in the lecture 2 slides.

Additionally, these instructions are implemented in the simulator (but not RISC-V): Examples of use of all these instructions can be found in the sample code in the lecture 2 slides.