Professional Documents
Culture Documents
Instructions: Language
of the Computer
2.1 Introduction
Instruction Set
Simplified implementation
add a, b, c # a gets b + c
All arithmetic operations have this form
Design Principle 1: Simplicity favours
regularity
2.2
2.2 Operations of the Computer Hardware
Arithmetic Operations
Arithmetic Example
C code:
f = (g + h) - (i + j);
# temp t0 = g + h
# temp t1 = i + j
# f = t0 - t1
Assembler names
2.3
2.3 Operands of the Computer Hardware
Register Operands
C code:
f = (g + h) - (i + j);
f, , j in $s0, , $s4
Memory Operands
C code:
g = h + A[8];
g in $s1, h in $s2, base address of A in $s3
lw $t0, 32($s3)
add $s1, $s2, $t0
offset
# load word
base register
C code:
A[12] = h + A[8];
h in $s2, base address of A in $s3
Immediate Operands
Cannot be overwritten
E.g., move between registers
add $t2, $s1, $zero
x n2 2
n2
x1 2 x 0 2
Range: 0 to +2n 1
Example
n 1
2.4
2.4 Signed and Unsigned Numbers
Using 32 bits
0 to +4,294,967,295
Chapter 2 Instructions: Language of the Computer 14
n2
x n2 2
x1 2 x 0 2
Range: 2n 1 to +2n 1 1
Example
n 1
Using 32 bits
2,147,483,648 to +2,147,483,647
Chapter 2 Instructions: Language of the Computer 15
Signed Negation
Complement means 1 0, 0 1
Example: negate +2
Sign Extension
Hexadecimal
Base 16
0
1
2
3
4
5
6
7
0100
0101
0110
0111
8
9
a
b
1000
1001
1010
1011
c
d
e
f
1100
1101
1110
1111
MIPS instructions
Register numbers
2.5
2.5 Representing Instructions in the Computer
Representing Instructions
op
rs
rt
rd
shamt
funct
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
Instruction fields
R-format Example
op
rs
rt
rd
shamt
funct
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
$s1
$s2
$t0
add
17
18
32
000000
10001
10010
01000
00000
100000
000000100011001001000000001000002 = 0232402016
Chapter 2 Instructions: Language of the Computer 22
rs
rt
constant or address
6 bits
5 bits
5 bits
16 bits
op
Instructions represented in
binary, just like data
Instructions and data stored
in memory
Programs can operate on
programs
Standardized ISAs
2.7
2.7 Instructions for Making Decisions
Conditional Operations
j L1
Compiling If Statements
C code:
if (i==j) f = g+h;
else f = g-h;
f, g, in $s0, $s1,
Otherwise, set to 0
1 < +1 $t0 = 1
# unsigned
Steps required
1.
2.
3.
4.
5.
6.
2.8
2.8 Supporting Procedures in Computer Hardware
Procedure Calling
Register Usage
C code:
int leaf_example (int g, h, i, j)
{ int f;
f = (g + h) - (i + j);
return f;
}
Arguments g, , j in $a0, , $a3
f in $s0 (hence, need to save $s0 on stack)
Result in $v0
MIPS code:
leaf_example:
addi $sp, $sp, -4
sw
$s0, 0($sp)
add $t0, $a0, $a1
add $t1, $a2, $a3
sub $s0, $t0, $t1
add $v0, $s0, $zero
lw
$s0, 0($sp)
addi $sp, $sp, 4
jr
$ra
Procedure body
Result
Restore $s0
Return
Non-Leaf Procedures
C code:
int fact (int n)
{
if (n < 1) return 1;
else return n * fact(n - 1);
}
Argument n in $a0
Result in $v0
MIPS code:
fact:
addi
sw
sw
slti
beq
addi
lw
lw
addi
jr
L1: addi
jal
lw
lw
addi
mul
jr
$sp,
$ra,
$a0,
$t0,
$t0,
$v0,
$ra,
$a0,
$sp,
$ra
$a0,
fact
$a0,
$ra,
$sp,
$v0,
$ra
$sp, -8
4($sp)
0($sp)
$a0, 1
$zero, L1
$zero, 1
4($sp)
0($sp)
$sp, 8
$a0, -1
0($sp)
4($sp)
$sp, 8
$a0, $v0
$sp,
$ra,
$a0,
$t0,
$t0,
$v0,
$sp,
$ra
$a0,
fact
$a0,
$ra,
$sp,
$v0,
$ra
$sp, -8
4($sp)
0($sp)
$a0, 1
$zero, L1
$zero, 1
$sp, 8
$a0, -1
0($sp)
4($sp)
$sp, 8
$a0, $v0
#
#
#
#
#
#
#
#
#
#
#
#
#
#
if so, result is 1
pop 2 items from stack
and return
else decrement n
recursive call
restore original n
and return address
pop 2 items from stack
multiply to get result
and return
Memory Layout
Byte/Halfword Operations
lb rt, offset(rs)
sb rt, offset(rs)
lh rt, offset(rs)
sh rt, offset(rs)
C code (nave):
Null-terminated string
void strcpy (char x[], char y[])
{ int i;
i = 0;
while ((x[i]=y[i])!='\0')
i += 1;
}
Addresses of x, y in $a0, $a1
i in $s0
MIPS code:
strcpy:
addi
sw
add
L1: add
lbu
add
sb
beq
addi
j
L2: lw
addi
jr
$sp,
$s0,
$s0,
$t1,
$t2,
$t3,
$t2,
$t2,
$s0,
L1
$s0,
$sp,
$ra
$sp, -4
0($sp)
$zero, $zero
$s0, $a1
0($t1)
$s0, $a0
0($t3)
$zero, L2
$s0, 1
0($sp)
$sp, 4
#
#
#
#
#
#
#
#
#
#
#
#
#
Branch Addressing
Forward or backward
op
rs
rt
constant or address
6 bits
5 bits
5 bits
16 bits
PC-relative addressing
Jump Addressing
address
6 bits
26 bits
Loop: sll
$t1, $s3, 2
80000
19
add
80004
22
32
lw
$t0, 0($t1)
80008
35
bne
21
19
19
80016
80020
Exit:
Loop
20000
80024
bne $s0,$s1, L2
j L1
L2:
Readings