Moving values around

Two numbers in registers, added and doubled into a perimeter, with the answer read in the registers panel.

Two numbers go into registers, the program works out the perimeter of the rectangle they describe, and the answer stays in t2. Nothing is read from memory and nothing branches: every value is in a register from the first instruction to the last, which is what the registers panel next to the program shows you.

This is the first program of the ladder, and the ones after it are built out of the same two steps: a number into a register, and an instruction that reads two registers and writes a third.

You need to know: the "Getting started with RISC-V" lecture and the "The 32 registers and their names" lecture. What is new here is the destination being an operand of its own, so an add can leave both of the numbers it read exactly as they were.

li t0, 30 puts the number 30 into t0, and there is no # in front of it: an operand that is a number is a number, and a register is written by its name with nothing on the front. The two add instructions are one C line, perimeter = 2 * (width + height), and add t2, t2, t2 adds a register to itself, which is how you double a number without a multiplication.

The M68K writes that pair as a copy and two adds, because an add.l d1, d0 has two operands and one of them has to be the destination. Here the destination is named separately, so add t2, t0, t1 leaves 30 in t0 and 12 in t1 and nothing else moves. MIPS does the same, with a $ in front of every register name.

t2, t3 and t4 all come out at 00000054, which is 84. mv t3, t2 is a pseudo-instruction and the line under it is the same real instruction written out: adding zero, which always reads 0, copies a register.

Try changing sub t5, t0, t1 to sub t5, t1, t0. t5 comes out at FFFFFFEE instead of 00000012, which is -18: the order of the two operands after the destination decides which way round the subtraction goes, and the registers panel shows you the bits either way.