CS031
CS31
Pascal Van Hentenryck
Lecture 15 1
MIPS
CS031 2
MIPS
A real assembly language
•! Basic architecture
•! ALU operations
•! Memory operations
•! Branch
•! I/O
•! Control Structures
Lecture 15
CS031 3
MIPS Architecture
Used in several commercial workstations.
Basic features •! 32 registers
•! Memory
•! load/store architecture
•! RISC (few instructions)
Load/store architecure •! All ALU operations are performed on
!"#$%&"!%'
•! Load/store are only used for ("()!* +,,"%%"%'
Lecture 15
CS031 4
MIPS Registers
Real Names
$0 . . . $31
Nicknames
$r0 $at $v0 $v1
$a0 $a1 $a2 $a3
$t0 $t1 $t2 $t3
$t4 $t5 $t6 $t7
$s0 $s1 $s2 $s3
$s4 $s5 $s6 $s7
$t8 $t9 $k0 $k1
$gp $sp $s8 $ra
Lecture 15
CS031 5
MIPS Memory MIPS is byte addressable, i.e. its memory is a large array of bytes
b7 b0
0
1
2
3
4
Each time you increment an address, MIPS refers to the next 8-bit byte
In Java terms
bytes[] m = new bytes[size of memory];
•! m[3] refers to the 4th byte in memory •! 3 is the address of that byte
Lecture 15
CS031 6
MIPS Memory (Words)
MIPS is an alignment machine 祩! -)!. +..!"%%"% +!" (/0&$10"%
)2 3'
MIPS is big-endian
addr 00
addr + 1 00
addr + 2 00
addr + 3 01
b7 . . . . b0
If you examine a word (4 bytes) in memory, its 4$#45)!."! byte comes first
addr 0? 00 00 0?
addr + 4
Lecture 15
CS031 7
Specifying Addresses
Addressing modes
label (direct): foo
“the contents of address foo”
constant (direct): x00010004
“the contents of address x00010004 register (indirect): ($s2)
“the contents of the address stored in $s2
indexed (relative): foo($s1) or 4($s2)
“&4" ,)6&"6&% )2 &4" +..!"%% )7&+$6". 7* '+..$6# &4"
,)6&"6&% )2 $s1 to foo”
0x0001004 foo: s1 0000008
s2 000100c
Lecture 15
CS031 8
Store Instructions
Store Word sw R, address Store the contents of register R at address in memory. Address must be word-aligned.
Store Byte sb R, address Store the low-order byte of register R at address in memory.
$s0 AB CD EF 02 sw $s0, foo sb $s0, bar la $s1, foo sw $s0, 8($s1) sw $s0, 10($s1)
foo
bar
AB CD EF 02
02
AB CD EF 02
Lecture 15
CS031 9
Sample Program
Average 3 integers:
.data
avg: .word #integer average
i1: .word 20 # first input
i2: .word 13 # second input
i3: .word 10 # third input
num = 3 # nb of nums
.text
__start: lw $s1,i1
lw $s2,i2
lw $s3,i3
add $s0,$s1,$s2
add $s0,$s0,$s3
div $s0,$s0,num
sw $s0,avg
done
Lecture 15
CS031 10
Branch Instructions bxx R1,R2,label
- R1 and R2 must be registers. - label can be a label (or a constant). • beq • bgt
• bge
• blt
• ble
• bne
Remember the short forms.
bxx R1,label
• bltz
• bgez
• blez
• bgtz
• beqz
• bnez
Lecture 15
CS031 11
Jump
Unconditional Jump
j label
Unconditional register jump
jr r
Lecture 15
CS031 12
Input / Output
For now we’ll use simple system calls.
Every system call has
- a call number
- arguments
You load these values into special registers, then do a magic syscall command.
Your code will continue where you left off.
Lecture 15
CS031 13
Printing
To print an integer: lw $a0,num # the value to print li $v0,1 # code to print integer
syscall # do it
To print a string: .data
str: .asciiz “Are we having fun yet?\n”
.text
la $a0,str #address of the string li $v0,4 # code to print string
syscall # do it
All the characters will be printed until a NULL (0) is reached.
The .asciiz declaration automaticallly puts a NULL at the end of the string.
Lecture 15
CS031 14
A Stupid Program
# Loop, asking and answering silly questions # until somebody says they’re # 999 years old and 999 inches tall. # # There are 3 years left between now and the # year 2012, so the answer is # height + (height/age)*3
.data age_question:
.asciiz “How old are you (in years)?” height_quest:
.asciiz “How tall are you (in inches)?” answer1:
.asciiz “Assuming linear growth,you’ll be” answer2:
.asciiz “ inches in the year 2012.\n” were_done:
.asciiz “We’re outta here.”
# Register Usage # s0 : age # s1 : height # s2 : calculation
.text __start: ask: la $a0,age_question
li $v0,4 syscall # Ask age question
Lecture 15
CS031 15
A Stupid Program li $v0,5
syscall # Read age
move $s0,$v0
la $a0,height_question
li $v0,4
syscall # Ask height question
li $v0,5
syscall # Read height
move $s1,$v0
bne $s0,999,cont # We’re okay
# if age999
beq $s1,999,end # If height =
# 999, exit
cont: mul $s2,$s1,3 # height * 3
div $s2,$s2,$s0 # height * 3 / age
add $s2,$s2,$s1 # height +
#(height * 3/age)
la $a0,answer1
li $v0,4
syscall # Print first part of answer
move $a0,$s2
li $v0,1
Lecture 15
CS031 16
A Stupid Program
syscall # Print number
la $a0,answer2
li $v0,4
syscall # Print 2nd part of # answer
j ask # Repeat this loop
end: la $a0,were_done
li $v0,4
syscall # Print end message
done
Lecture 15
CS031 17
Structured Coding
•! Traveling through hyperspace ain't like dusting crops, boy! Without
precise calculations we could fly
right through a star or bounce too
close to a supernova, and that'd
end your trip real quick, wouldn't
it?”
•! !Han Solo, to Luke Skywalker
Lecture 15
CS031 18
Structured Coding
•! JAVA has statements that allow structured control flow
•! One way to write well-structured assembly code is to
1.! design your algorithm in terms of high- level control structures
2.! express those structures in assembly language
Lecture 15
CS031 19
IF Example
if (a > b)
c = a
else
c = b
Version 1:
bgt a,b,lab # if a > b else
move c,b # c = b
j end #
lab: #
move c, a # c = a
end: #
Version 2:
ble a,b,lab # if a > b
move c,a # c = a
j end #
lab: # else
move c,b # c = b
end: #
Lecture 15
CS031 20
IF Example
if ($s1 > $s2)
$s3 = $s1
else
$s2 = $s2
Version 1:
bgt $s1,$s2,lab # if a > b else
move $s3,$s2 # c = b
j end #
lab: #
move $s3, $s1 # c = a
end: #
Version 2:
ble $s1,$s2,lab # if a > b
move $s3,$s1 # c = a
j end #
lab: # else
move $s3,$s2 # c = b
end: #
Lecture 15
CS031 21
Alternative Example
if a > b
c = a
else if a < b
c = b
else
c = 0
ble a,b,l1 # if a > b
move c,a # c = a
j end #
l1: # else
bge a,b,l2 # if a < b
move c,b # c = b
j end #
l2: # else
move c,0 # c = 0
end: #
Lecture 15
CS031 22
FOR Example
sum = 0;
for(count = 1; count
CS031 23
FOR Example
sum = 0;
for(count = 1; count
CS031 24
WHILE we’re at it
remain = dividend; result = 0;
while (remain >= divisor) { remain -= divisor;
result++;
}
while: # WHILE rem >= divis
blt remain,divisor,end
sub remain,remain,divisor # subtract divisor
add result,result,1 # increment result j while #
end: # END WHILE
Lecture 15
CS031
CS31
Pascal Van Hentenryck
Lecture 25T 1
TAL
CS031 Lecture 25T 2
MIPS
MIPS Machine Language
•! TAL
•! Translating MAL to TAL
CS031 Lecture 25T 3
TAL
MAL (Mips Assembly Language) is a convenient, high-level, abstraction of what actually goes on in the MIPS computer.
TAL (True Assembly Language) describes exactly what’s going on.
Why? The main reason is •! MIPS instructions are coded on 32-bits
•! MIPS addresses/values are coded on 32- bits
•! There is no way to fit an arbitrary 32-bit immediate operand into a 32-bit instruction and still have room for anything else!
CS031 Lecture 25T 4
TAL: Arithmetic
opcode Rd,Rs,Rt
All registers:
add 0000 00ss ssst tttt dddd d000 0010 0000
•! last 11 bits encode the operation
•! multiplication and division are different
opcode Rt,Rs,I
Rt and Rs are registers; I is a 16-bit constant
addi 0010 00ss ssst tttt iiii iiii iiii iiii
CS031 Lecture 25T 5
TAL: Arithmetic
What to do with a big constant? •! Put it into a register
•! Use two instructions to do so
lui Rt,I
loads a 16-bit integer into the top half of the register and zeros the bottom half
big_const = 0xFEEDFOOD
high_big_const = 0x0000FEED
low_big_const = 0x0000FOOD
.text
lui $1,high_big_const
ori $s0,$1,low_big_const
CS031 Lecture 25T 6
TAL: Arithmetic
Multiplication is different as well
mult rs, rt •! places the 64-bit result of the multiplication in
the concatenation of registers HI and LO
mflo rs •! copy the contents of LO into rs
mfhi rs •! copy the contents of HI into rs
mul $8, $9, $10
mult $9, $10
mflo $8
Tests overflow if needed by using HI
CS031 Lecture 25T 7
TAL Arithmetic
Division looks the same as multiplication
div rs, rt •! places the remainder of the division in HI and
the quotient in LO
CS031 Lecture 25T 8
TAL: Load and Store
opcode RA, I(RB)
RA and RB are registers; I is a 16-bit quantity
lw 1000 11bb bbba aaaa iiii iiii iiii iiii
lb 1000 00bb bbba aaaa iiii iiii iiii iiii
lbu 1001 00bb bbba aaaa iiii iiii iiii iiii
sw 1010 11bb bbba aaaa iiii iiii iiii iiii
sb 1010 00bb bbba aaaa iiii iiii iiii iiii
CS031 Lecture 25T 9
TAL: Load and Store
la is a virtual instruction
Same trick as for big constants
la $4, address lui $4, high_address
ori $4, low_address
CS031 Lecture 25T 10
TAL: Jump
j address
•! the least 26 significant bits should
contain a word address
•! in fact, the real word address is the 26
bits to which 00 is added to the right
•! This enables the instruction to address
a larger space, since we know that the
last two bits are always 0
What if we need to branch farther?
CS031 Lecture 25T 11
TAL: Branch
TAL has the following branch instructions
BEQ rs,rt,I 0001 00ss ssst tttt iiii iiii iiii iiii
BNE is similar.
Branch Offset •! The address stored is an offset from the
current value of the PC
If the branch is taken,
PC := PC + sex(I*4)
You can only branch to an address 215
words from the current PC.
What if we need to jump farther?
CS031 Lecture 25T 12
But What About BGT?
The other MAL branch instructions have to be implemented with two TAL instructions.
SLT r1,r2,r3 r1 = 1 if r2
CS031 Lecture 25T 13
Signed versus Unsigned
•! Numbers are typically signed (2’s comp)
•! Addresses are unsigned
Is the test for greater-than the same for both?
No. So, even in MAL, we have some more branch instructions
bgtu
bltu
bgeu
bleu
Whenever you are comparing addresses, use unsigned operations.