Professional Documents
Culture Documents
MIPS Processor R3000: Registers
MIPS Processor R3000: Registers
Generally…
Instructions execute after all previous instructions have
completed, therefore their results are available to the next
instruction.
But, you may see exceptions to these rules.
Example:
Instructions Variables
add $s0, $s1, $s1 $s0: 0, 12, 24, ?
add $s0, $s0, $s0 $s1: 6, ?
add $s0, $s0, $s0 $s2: 8
sub $s1, $s0, $s1 $s3: 10
Register 1 ($at) reserved for assembler, 26-27 (K0-K1) for operating system
Computer Architecture 1 3/27/2016
MIPS Arithmetic Operations
12
op rs rt rd shamt funct
Example 1:
C code: a = b + c;
MIPS code: add a, b, c
Example 2:
C code: a = b + c + d;
MIPS code: add a, b, c
add a, a, d
Computer Architecture 1 3/27/2016
Signed and Unsigned Numbers
15
Several shortcomings
Where to put the sign bit?
11 ).
two
+ 7ten = 0 111two
- 7ten = 1 000two
Masking:
1011 0110 1010 0100 0011 1101 1001 1010
0000 0000 0000 0000 0000 1111 1111 1111
The result of anding these two is:
0000 0000 0000 0000 0000 1101 1001 1010
Used for isolating bits and bytes in a word.
Zeroing some bits in a word:
andi $t0,$t0,0x0FFF
Setting some bits as ones in a word:
Let $t0 =0x12345678
Then after: ori $t0, $t0, 0xFFFF
$t0= 0x1234FFFF
high-order 16 bits are untouched, while the low-order 16 bits are
forced to 1s).
Computer Architecture 1 3/27/2016
Multiplication
26
In MIPS:
32-bit value x 32-bit value = 64-bit value
General Syntax:
mult register1, register2
Store high 32 bits in Register HI
Store low 32 bits in Register LO
Example:
in C: a = b * c;
in MIPS:
let b be $s2; let c be $s3; and let a be $s0 and $s1 (since it may be up to
64 bits)
mult $s2,$s3 # b * c
mfhi $s0 # upper half of product into $s0
mflo $s1 # lower half of product into $s1
General Syntax
lw rt, immediate(rs) #Reg[rt] = Mem[Reg[rs]+sxt(imm)]
―Fetch into rt the contents of the memory location whose
address is const (immediate) plus the contents of rs‖
Abbreviation: lw rt, imm for lw rt, imm($0)
sw rt, immediate(rs) #Mem[Reg[rs]+sxt(imm)] = Reg[rt]
―Store the contents of rt into the memory location whose
address is const plus the contents of rs‖
Abbreviation: sw rt, imm for sw rt, imm($0)
op rs rt 16 bit number
This instruction will take the pointer in $s2 (Base reg.), add
32 bytes (offset) to it, and then load the value from the memory
pointed to by this calculated sum into register $t0
Computer Architecture 1 3/27/2016
Translating MIPS Assembly Language into Machine
Language
33
Example
If $t1 has the base of the array A and $s2
corresponds to h, the assignment
statement A[300] = h + A[300]; is
compiled into:
lw $t0, 1200($t1) #reg $t0 gets A[300]
add $t0, $s2, $t0 # reg $t0 gets h +
A[300]
Sw $t0, 1200($t1) #Stores h+A[300]
back into A[300]
The constant in a data transfer
instruction (300) is called the offset, and
the register added to form the address
($t1) is called the base register.
Computer Architecture 1 3/27/2016
Example
34
Using the op given and the number of used registers the MIPS
machine language code for these instructions?
$ t0 = 8, $t1 = 9, $ s1 = 18
Binary Equivalent
Immediate
I-type: Branch Instructions
opcode specifies Beq vs. Bne
Function:
if (Reg[rs] == Reg[rt]) =>PC ← (PC + 4 ) + 4*sxt(immediate)
Relative Addressing:
The size of the constant field (Immediate) = (16-bits) limits the
range of branches.
PC is 32-bit pointer to memory
Final calculation:
If we don’t take the branch: (condition is false)
PC = PC + 4.
If we do take the branch: (condition is true)
PC = (PC + 4) + (immediate * 4).
Example:
Assuming (i at $s0, j at $s1, h at $s3)
in C : if (i == j) h = i + j;
in assembly language
bne $s0, $s1, Label
add $s3, $s0, $s1
Label: ....
Computer Architecture 1 3/27/2016
Control Flow
38
Example
Suppose register $s0 has the binary number
1111 1111 1111 1111 1111 1111 1111 1111two
and that register $s1 has the binary number
0000 0000 0000 0000 0000 0000 0000 0001two
What are the values of registers $t0 and $t1 after these
two instructions?
slt $t0, $s0, $s1 # signed comparison
sltu $t1, $s0, $s1 # unsigned comparison
The value in register $s0 represents - 1ten if it is an integer and
4,294,967,295ten if it is an unsigned integer. The value in
register $s1 represents 1ten in either case. Then register $t0
has the value 1, since - 1ten < 1ten, and register $t1 has the value
0, since 4,294,967,295ten > 1ten.
Computer Architecture 1 3/27/2016
Control Flow
40
226 = 64 MWords
Relative addressing
With respect to PC (program counter)
Most branches are local (principle of locality)
226 = 64 Mwords
Example
i = o
while ( save[i] == k )
i += 1;
Given a value for k, set i to the index of element in
array save [ ] that does not equal k.
80000 0 0 19 9 2 0 sll
80004 0 9 22 9 0 32 add
80008 35 9 8 0 lw
80012 5 8 21 Exit = +2
+2 bne
80016 8 19 19 1 addi
80020 2 Loop = 20000 (memory word address) j
80024 .....
Note: $t0 ≡ Reg 8, $t1 ≡ Reg 9, $s3 ≡ Reg 19, $s5 ≡ Reg 21, $s6 ≡ Reg 22
temp temp i k save
Computer Architecture 1 3/27/2016
Finding Branch Address Exit
50
J 20000
000010 00000000000100111000100000 = 20000
$PC = 80024, when jump is being executed
00000000000000010011100010011000 = 80024
Multiply J argument by 4 (convert to byte address)
0000000000010011100010000000 = 80000
Insert four leading bits from $PC
00000000000000010011100010000000 = 80000
Thus, Loop is memory byte address 80000.
switch (k) {
case 0: f=i+j; break; /* k=0*/
case 1: f=g+h; break; /* k=1*/
case 2: f=g-h; break; /* k=2*/
case 3: f=i-j; break; /* k=3*/
}
We could rewrite the code using if-else structure
If (k == 0) f = I + j;
else if (k == 1) f = g + h;
else if (k == 2) f = g - h;
else if(k == 3) f = - j;
Using this mapping:
f: $s0, g: $s1, h: $s2, i: $s3, j: $s4, k: $s5
Jal ProcedureAddress
An instruction that jumps to an address and simultaneously saves the
address of the following instruction (Return Address) in a register
($ra in MIPS).
Computer Architecture 1 3/27/2016
Procedures in MIPS
57
addi $sp, $sp, –12 # adjust stack to make room for 3 items
sw $t1, 8($sp) # save register $t1 for use afterwards
sw $t0, 4($sp) # save register $t0 for use afterwards
sw $s0, 0($sp) # save register $s0 for use afterwards
The values of the stack pointer and the stack (a) before, (b) during, and (c) after the procedure call.
The stack pointer always points to the “top” of the stack, or the last word in the stack in this drawing.
Computer Architecture 1 3/27/2016
Procedures Writing Steps
62
Stack allocation (a) before, (b) during, and (c) after procedure call. The frame
pointer ($fp) points to the first word of the frame, often a saved argument register, and the
stack pointer ($sp) points to the top of the stack
HEAP
00000000000000000001000000100101
00000000000001010100000000101010
00010001000000000000000000000011
00000000010001000001000000100000
00100000101001011111111111111111
00001000000100000000000000000001
Next step: separation of fields
Computer Architecture 1 3/27/2016
Example Cont.
71
0 0 0 2 0 37
0 0 5 8 0 42
4 8 0 +3
0 2 4 2 0 32
8 5 5 -1
2 00000100000000000000000001
0x00400000 or $2,$0,$0
0x00400004 slt $8,$0,$5
0x00400008 beq $8,$0, 3
0x0040000c add $2,$2,$4
0x00400010 addi $5,$5, -1
0x00400014 j 0x100001
or $v0,$0,$0
Loop: slt $t0, $0, $a1
beq $t0, $0, End_Loop
add $v0, $v0, $a0
addi $a1, $a1,-1
j Loop
End_Loop:
C code:
Mapping: $v0: product
$a0: mcand
$a1: mplier
product = 0;
while ( mplier > 0) {
product += mcand;
mplier -= 1; }
0 9 10 8 0 32
Computer Architecture 1 3/27/2016