yeolyi.comSeongyeol's CS

Assembly

We read and wrote the multiplication and odd-or-even programs with mnemonics, like LOAD 14. But the CPU only reads binary like 01100011, which is machine code, so memory has to hold binary. Does a person have to translate it one bit at a time?

Writing with names

A language that writes machine instructions one per line with mnemonics, like LDI R0, 3, is called assembly language. Each line of assembly turns into exactly one machine instruction, so translating it only takes looking things up in the table from the instruction set post. A program that does this for us is an assembler.

Assembly
1LDI R0, 3
2ADD R2, R0
3SUBI R1, 1
4HALT
AddressAssemblyMachine code
0LDI R0, 30110 0011
1ADD R2, R00001 1000
2SUBI R1, 10101 0101
3HALT1111 0000
  1. Edit a line. The machine code changes right away.
  2. Write a register that doesn’t exist, like R4, or an instruction that doesn’t exist.

Adding lines

The odd-or-even program checks whether the number at address 13 is odd and writes the result to address 15. What happens if we give the odd-or-even program below more work by adding instructions at the top?

1LOAD 13
2SUBI R0, 1
3JZ 6
4SUBI R0, 1
5JZ 7
6JUMP 1
7LDI R0, 1
8STORE 15
9HALT

Switch between the original and the one with six lines added at the top.

Adding lines pushed every instruction after them down six slots, but addresses like the ones in JZ 6 and JUMP 1 stayed the same, so they jump to the wrong lines. STORE 15 now sits at address 13, taking the place of the number to test. Every time we add lines, we have to find and fix every one of these addresses.

Labels

So in assembly, we put a name called a label in front of a line and use that name instead of the address. Data gets a name too, written at the end of the program like num: DATA 7. That means “put the value 7 here instead of an instruction, and call this slot num.”

Assembly
1 LOAD num
2loop: SUBI R0, 1
3 JZ odd
4 SUBI R0, 1
5 JZ even
6 JUMP loop
7odd: LDI R0, 1
8even: STORE out
9 HALT
10num: DATA 7
11out: DATA 0

Labels loop = 1, odd = 6, even = 7, num = 9, out = 10

AddressAssemblyMachine code
0LOAD num1000 1001
1SUBI R0, 10101 0001
2JZ odd1101 0110
3SUBI R0, 10101 0001
4JZ even1101 0111
5JUMP loop1100 0001
6LDI R0, 10110 0001
7STORE out1001 1010
8HALT1111 0000
9DATA 70000 0111
10DATA 00000 0000

Add any instruction at the top. The labels’ addresses shift on their own, and the machine code follows.

The assembler reads the program twice. The first time it counts the address of every label, and the second time it replaces names with addresses and produces machine code. Data always sits after the program, so it never collides with instructions. A data slot with a name like this is called a variable.

Wrapping up

The computer so far handles one instruction from start to finish in a single clock, so the clock can’t be any faster than that. In the next post, let’s look at how to process instructions faster with the same parts.

Challenge

When the assembler translates the program below, what is the machine code for the JZ end line? JZ's opcode is 1101, and the last 4 bits are the address to jump to. Answer with 8 digits, no spaces.

      LDI R1, 3
loop: SUBI R1, 1
      JZ end
      JUMP loop
end:  HALT