C819 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C8 · Lesson 19 · Assembly lab

Five small routines, written and tested on the board

Reading assembly and writing it are different skills, and the second only comes from practice. This lab is five routines of the kind every embedded programmer writes: find the largest value in an array, copy a block of memory, reverse a string in place, count the 1 bits in a word, and find a greatest common divisor. Each one is run on the simulated board and checked against your own numbers, so a routine that works for one input but not another will be caught.

  • A worked routine to study before you start: it finds the largest of six signed words. A pointer walks the array; a0 holds the largest so far and is replaced whenever a bigger value turns up.
  • Step it and watch a0 change only at the 27s. Notice that ble compares as signed numbers, so −3 is correctly smaller than 14.

Predict

A routine counts the 1 bits in a word by testing the lowest bit and shifting right, 32 times. A faster trick clears the lowest set bit each pass with x & (x − 1). How many passes does that take for 0x00000081?

Name it

Patterns you'll use

  • Running maximum: load the first element, then compare each next one and replace when larger (signed branches for signed data).
  • Block copy: two pointers and a counter; load from one, store to the other, advance both.
  • In-place reversal: a pointer at each end; swap the two bytes and move the pointers toward each other until they meet.
  • Bit counting: andi t0, x, 1 reads the lowest bit; srli x, x, 1 moves the next one down. Or clear the lowest set bit with addi t0, x, -1 and and x, x, t0, counting passes until x is 0.
  • Euclid's algorithm: gcd(a, b) is unchanged when the larger is replaced by the difference; repeat until they're equal. (With remainder, rem, it converges faster.)

Testing

Each task below runs your code with values set before it starts and checks registers or memory when it reaches ebreak. Data labels start at 0x1000: the first .data item is at 0x1000, the next word at 0x1004, and so on.

Worked example

Trace the block copy of three words from src to dst.

Show the solution
  1. Copy one word. Load src[0] and store it to dst[0].
  2. Advance. Move both pointers on by 4 and the count down by 1.
  3. Repeat. Go again while the count is above 0.
  4. After three passes. dst holds the same three words, and both pointers sit just past their blocks.

Answer three passes copy the three words, leaving both pointers just past their blocks.

Worked example

Reverse "abc" in place.

Show the solution
  1. Start at the ends. The pointers are at 'a' (address 0) and 'c' (address 2).
  2. Swap. That gives "cba".
  3. Move inward. The pointers move to 1 and 1. They've met, so stop.
  4. The middle character. It never moves.

Answer one swap turns "abc" into "cba".

Check

Leave the largest of the five signed words in vals in a0.

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

Copy the three words at src to dst (src is at 0x1000, dst at 0x100C).

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

Reverse the 4-character string word ("Ohms", at 0x1000) in place, so it reads "smhO".

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

Count the 1 bits in a0 (an 8-bit value) and leave the count in a1.

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

a0 and a1 hold two positive numbers. Leave their greatest common divisor in a0.

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Explain why

Why does clearing the lowest set bit with x & (x - 1) work?

Practice

Keep practicing

7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.