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
blecompares 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?
2: 0x81 has two 1 bits, and each x & (x − 1) clears exactly one of them. The loop runs once per set bit instead of once per bit position.
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, 1reads the lowest bit;srli x, x, 1moves the next one down. Or clear the lowest set bit withaddi t0, x, -1andand 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
- Copy one word. Load src[0] and store it to dst[0].
- Advance. Move both pointers on by 4 and the count down by 1.
- Repeat. Go again while the count is above 0.
- 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
- Start at the ends. The pointers are at 'a' (address 0) and 'c' (address 2).
- Swap. That gives "cba".
- Move inward. The pointers move to 1 and 1. They've met, so stop.
- 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?
Subtracting 1 from a binary number turns its lowest 1 bit into a 0 and every 0 below it into a 1, leaving the higher bits unchanged. ANDing that with the original keeps the higher bits (they're the same in both) and clears everything from the lowest 1 bit down, since in those positions one of the two numbers has a 0. So each pass removes exactly one 1 bit, and a loop counts the 1s in as many passes as there are 1s.
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.