10.15 ASM Language
1.1 Introducing the platform
- Nintendo Game Boy: videogame console
- Released in 1989
- 120 million units sold worldwide
- Low cost, low energy consumption
- 8 bits CPU (name: SM83, sometimes called GBz80)
- 160x144 pixels display, 4 shades of green
- Still relevant in 2023 for education, research and fun
1.2 Game examples
1.3 The Game Boy CPU
A custom CPU made by Sharp (Japan)
It imitates closely two existing popular CPUs:
- Intel 8080: used in personal computers, the precursor of the x86 family still in use today
- Zilog Z80: used in many personal computers of the 1970s and 1980s, and pocket calculators
1.4 Our plan
- first weeks: get to know the CPU
- then: get to know the platform
- a computer is not only the CPU itself!
- it is: CPU + memory map + graphic device + buses
later weeks of semester: get the platform to work to run graphical programs
we will cover ~60% of the platform
objectives:
get experience in assembly programming
- understand the implications of defining a stored program without a compiler
- understand low-level issues of timing, instruction sizes
1.5 Instructions and instructions set
- Instructions are used to control a CPU
- Each instruction causes the CPU to perform a very specific task
- Each CPU has its own instruction set, that is a fixed set of instructions that it can understand.
- A program in machine code consists of a sequence of machine instructions and will only work on a specific CPU model
1.6 The CPU
CPU
+------------+
| |
instructions -> | internal |
| state |
| |
+------------+
inside the CPU, there are some data
CPU
+-------------+
| 8-bit |
instructions -> | registers |
| |
|a,b,c,d,e,h,l|
+-------------+
registers are vary small and call store 8-bit. Like memory, but very small and is in CPU
... CPU
instr1 +------------+
instr2 -> | 8-bit |
instr3 | registers |
... | |
|a,b,c,d,e,h,l|
+------------+
-
What are registers?
-
each register == 8 bits
- think of them as C variables char/uint8
- cannot add more registers
-
a
is often called the "accumulator" -
What are instructions?
-
small commands that tell the CPU to do something
- very simple syntax
1.7 Assembly language (ASM)
- ASM == writing programs as a sequence of instructions
- almost no syntax!
-
instructions are very different from C statements
-
there are infinitely many C statements because we can extend the statements are large as we want, e.g.:
a=b+c+d+e+f+g+...
- there are finitely many ASM instructions
...
...
Example:
In C:
a = 20;
b = 40;
c = 50;
d = (a + b) + c;
In ASM:
ld a,20
ld b,40
ld c,50
add b
add c
ld d,a
ld
means load data
All arithmetic operations use a
as the first data implicitly.
a
is accumulator, the result is temporarily stored in a
1.8 ASM syntax
ld a,10 ; decimal notation
ld b,20
add b ; a = a + b
ld b,a
ld a,%00001111 ; binary notation
ld c,%11000000
or c ; a = a | c
ld c,$F0 ; hexadecimal notation
xor c ; a = a ^ c
Format type | Prefix | Accepted characters |
---|---|---|
Hexadecimal | $ | 0123456789ABCDEF |
Decimal | none | 0123456789 |
Binary | % | 01 |
- A numerical constant is also called an "immediate" in assembly.
- The syntax is line-based, meaning that you do one instruction per line.
- Uppercase/lowercase does not matter
1.9 Today's instructions
We will cover part of the following:
- 8-bit load instructions
- 8-bit arithmetic and logic instructions
- arithmetic shift instructions
Then do a few exercises.
1.10 8-bit load instructions
Mnemonic | Description |
---|---|
ld r,r |
r=r |
ld r,n |
r=n |
-
r
is one of the registersa,b,c,d,e,h,l
-
n
is a 1 byte constant -
examples:
-
ld a,b
-
ld b,a
-
ld b,10
-
ld b,%0001010
(equivalent to previous one) -
ld h,l
1.11 increment/decrement instructions
Mnemonic | Description |
---|---|
inc r | r=r+1 |
dec r | r=r-1 |
-
examples:
-
inc a
-
inc b
-
inc c
-
dec d
-
dec e
-
dec h ...
LD A, \(\emptyset \leftarrow\) 00000000
DEC A \(\leftarrow\) 11111111
1.12 8-bit arithmetic instructions with register or constant arguments
Mnemonic | Description |
---|---|
add r | A=A+r |
add n | A=A+n |
sub r | A=A-r |
sub n | A=A-n |
-
a
is always the first argument of these instructions -
a
is also called the "accumulator" -
in some documentation, they are written with
a
as first argument: -
add a,b
-
sub a,c
the essence of it is plus the negative one - the Game Boy CPU has no multiply and divide instructions
1.13 8-bit logic instructions with register or constant arguments
Mnemonic | Description |
---|---|
and r | A=A & r |
and n | A=A & n |
xor r | A=A ^ r |
xor n | A=A ^ n |
or r | A=A | r |
or n | A=A | n |
cpl | A = A xor FF(=\(11111111_2\)) (invert all bits of A) |
1.14 Arithmetic Shift instructions
Mnemonic | Description |
---|---|
sla r | shift left arithmetic (b0=0) |
sra r | shift right arithmetic (b7=b7) |
If this register has an unsigned number, use SRL; otherwise, use SRA.
UNSIGNED CHAR B = 151;
B = B / 2;
After compiling,
..... SRL B.....
UNSIGNED CHAR B = -105;
B = B / 2;
After compiling,
..... SRA B.....
1.15 Exercise 1
What are the final values of all registers of this snippet?
LD A,0
LD B,0
INC A
INC B
ADD B
LD C,A
LD D,10
ADD D
You can write the answer in the most convenient way (decimal, binary or hexa).
When writing answer in decimal, use the most convenient way between unsigned and signed.
E.g., -128 and 128 are considered the same number; -1 and 255 are considered the same number.
Solution
A=12, B=1, C=2, D=10
1.16 Exercise 2
What are the final values of all registers of this snippet?
LD A,100
LD B,50
LD C,20
SUB B
SUB C
SUB C
DEC B
INC C
Solution
A=10, B=49, C=21
1.17 Exercise 3
LD A,0
LD B,255
DEC A
INC B
Solution
A=255, B=0
LD A,100
LD B,100
ADD B
ADD B
Solution
A=44, B=100
LD A,100
LD B,100
SUB B
SUB B
Solution
A=156, B=100
1.18 Exercise 4
LD A,%00000000
LD B,%10101010
OR B
Solution
A=%10101010, B=%10101010
LD A,%00001111
LD B,%10101010
OR B
Solution
A=%10101111, B=%10101010
LD A,%00001111
LD B,%10101010
AND B
Solution
A=%00001010, B=%10101010
1.19 Exercise 5
LD A, $0F
LD B, $AA
XOR B
Solution
A=%10100101, B=%10101010
LD A, $0F
LD B, $BB
CPL
LD B,A
A=%11110000, B=%11110000