Fall 2019 - Assembly Programming
Fall 2019 - Assembly Programming
Assembly Programming
add a, b, c # a gets b + c
f = (g + h) - (i + j);
MIPS Code:
f = (g + h) - (i + j);
f, …, j in $s0, …, $s4
g = h + A[8];
g in $s1, h in $s2, base address of A in $s3
A[12] = h + A[8];
h in $s2, base address of A in $s3
Range: 0 to +2n – 1
Example
0000 0000 0000 0000 0000 0000 0000 10112
= 0 + … + 1×23 + 0×22 +1×21 +1×20
= 0 + … + 8 + 0 + 2 + 1 = 1110
Using 32 bits
0 to +4,294,967,295
x x 1111...1112 1
x 1 x
Example: negate +2
+2 = 0000 0000 … 00102
–2 = 1111 1111 … 11012 + 1
= 1111 1111 … 11102
MIPS instructions
Encoded as 32-bit instruction words
Small number of formats encoding operation code (opcode),
register numbers, …
Regularity!
Register numbers
$t0 – $t7 are reg’s 8 – 15
$t8 – $t9 are reg’s 24 – 25
$s0 – $s7 are reg’s 16 – 23
Chapter 2 — Instructions: Language of the Computer — 28
MIPS R-format Instructions
op rd rs rt shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
Instruction fields
op: operation code (opcode)
rs: first source register number
rt: second source register number
rd: destination register number
shamt: shift amount (00000 for now)
funct: function code (extends opcode)
Chapter 2 — Instructions: Language of the Computer — 29
R-format Example
op rd rs rt shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
0 8 17 18 0 32
000000100011001001000000001000002 = 0232402016
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
jal ProcedureLabel
Address of following instruction put in $ra
Jumps to target address
jr $ra
Copies $ra to program counter
Can also be used for computed jumps
e.g., for case/switch statements
Chapter 2 — Instructions: Language of the Computer — 47
Leaf Procedure Example
C code:
Write Programs
Write Programs
START today!