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

C8 · Lesson 17 · From C to assembly

What a compiler turns your code into

Firmware for microcontrollers is mostly written in C, and a compiler turns it into instructions like the ones in the last five lessons. Each C construct has a usual translation: a variable becomes a register, an array access becomes a shift, an add and a load, a for loop becomes a test, a body and a jump back, and a function call follows the calling convention. Knowing the translation tells you what your code costs and lets you read a disassembly when something goes wrong. Below, a function that adds up an array steps along in C and in assembly together.

Play

  • Step through. The highlighted C line is the one the current instruction belongs to.
  • Which instructions does total = total + a[i]; become?
  • Where do total and i live while the loop runs?

Predict

How many instructions run in one pass of the loop, from bge to j loop?

Name it

How C constructs compile

The assembly above is the kind of code an optimizing compiler writes for RV32I; a real compiler's output differs in details, such as how it orders the loop.

CAssembly
a local inta register (t0 to t6 if it isn't needed after a call, s0 to s11 if it is)
x = y + 3addi
a[i] for 4-byte intslli by 2, add the base, lw
p->field or *(p + k)lw with an offset from p
if (x < y) {…}bge x, y, skip, then the body
for (init; test; step) {…}init; a test that branches out; the body; the step; j back
f(x, y)x in a0, y in a1, call f, the result in a0
return vv into a0, ret

Types set the scale. An array of char (1 byte) needs no shift, short (2 bytes) a shift by 1, int (4 bytes) a shift by 2. Signed types load with lb/lh and compare with blt; unsigned ones load with lbu/lhu and compare with bltu.

What costs what. Each pass of this loop runs 7 instructions, so sum takes 7n + 5 instructions for n elements: 2 to set up, 7 per pass, 1 last test, and 2 to return. Arithmetic on registers is cheap. Loads, stores, branches and calls are where time goes, which is why compilers keep busy variables in registers.

Worked example

Compile x = a[i] + 1; for an int array, with a in a0, i in a1 and x in t2.

Show the solution
  1. Scale the index. slli t0, a1, 2 makes 4i.
  2. Form the address. add t0, a0, t0 adds the base.
  3. Load the element. lw t1, 0(t0) loads a[i].
  4. Add one. addi t2, t1, 1 adds one into x.

Answer slli t0, a1, 2, add t0, a0, t0, lw t1, 0(t0), addi t2, t1, 1.

Worked example

Compile while (n != 0) { n = n >> 1; c++; } for unsigned n in a0 and c in a1.

Show the solution
  1. Test first. A while loop may run zero times, so the test comes before the body.
  2. The body. Shift n right by one and count.
  3. Jump back. Jump to the test, which leaves at done.
loop:
    beqz a0, done
    srli a0, a0, 1
    addi a1, a1, 1
    j    loop
done:

Answer loop: beqz a0, done, then srli a0, a0, 1, addi a1, a1, 1, j loop, and done:.

Worked example

Compile s->count++; where s points to struct { int id; int count; } and is in a0.

Show the solution
  1. Find the offset. id takes bytes 0 to 3, so count sits at offset 4.
  2. Load, add and store. lw t0, 4(a0), addi t0, t0, 1, sw t0, 4(a0).
  3. What -> costs. The compiler turns every field name into a fixed offset from the struct's address, so -> costs nothing beyond the load itself.
  4. A warning. If another piece of code (an interrupt handler, say) can change count between the load and the store, one increment is lost: the read-modify-write isn't atomic.

Answer lw t0, 4(a0), addi t0, t0, 1, sw t0, 4(a0).

Check

Which instructions compute the address of a[i]?

Check

How many instructions does sum run for an array of 75 elements, from its first instruction to its ret?

Check

An array whose elements are 8 bytes each starts at 0x1578. What's the address of element 6?

Check

Translate this C, with x in a0 and y in a1:

y = x;
if (y < 0) {
    y = -y;
}

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

Translate this C, with n in a0 and f in a1:

int f = 1;
for (int i = 0; i < n; i++) {
    f = f * 2;
}

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

Put the instructions for total = total + a[i]; in order (i in t1, the array's address in a0, total in t0).

  1. add t2, a0, t2
  2. lw t3, 0(t2)
  3. add t0, t0, t3
  4. slli t2, t1, 2

Check

a0 points to struct { int id; int count; }. Add a1 to s->count, store it back, and leave the new count in a2.

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

int m[10][8] is stored row by row from address 0.

(a)

At what address is m[3][7]?

(b)

How many bytes apart are m[i][j] and m[i + 1][j]?

Explain why

Why does the compiled loop keep total and i in registers rather than in memory?

Practice

Keep practicing

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