03 Mips
03 Mips
03 Mips
MIPS Architecture
1 !
2 !
MIPS Architecture
3 !
Stack!
Programmer-Visible
State
Program
Counter
(PC)
Address
of
next
instruc:on
Memory
Byte
addressable
array
Code,
user
data,
(some)
OS
data
Includes
stack
Condi:on
Codes
Store
status
informa:on
about
most
recent
arithme:c
opera:on
Used
for
condi:onal
branching
4 !
Registers
Reg.
#
0
1
2-3
4-7
8-15
16-23
Name
zero
$at
$v0
-
$v1
$a0
-
$a3
$t0
-
$t7
$s0
-
$s7
Descrip0on
the
value
0
(assembler
temporary)
reserved
by
the
assembler
(values)
from
expression
evalua:on
and
func:on
results
(arguments)
First
four
parameters
for
subrou:ne.
Not
preserved
across
procedure
calls
(temporaries)
Caller
saved
if
needed.
Subrou:nes
can
use
w/ out
saving.
Not
preserved
across
procedure
calls
(saved
values)
-
Callee
saved.
A
subrou:ne
using
one
of
these
must
save
original
and
restore
it
before
exi:ng.
Preserved
across
procedure
calls
(temporaries)
Caller
saved
if
needed.
Subrou:nes
can
use
w/ out
saving.
These
are
in
addi:on
to
$t0
-
$t7
above.
Not
preserved
across
procedure
calls.
reserved
for
use
by
the
interrupt/trap
handler
global
pointer.
Points
to
the
middle
of
the
64K
block
of
memory
in
the
sta:c
data
segment.
stack
pointer
Points
to
last
loca:on
on
the
stack.
saved
value
/
frame
pointer
Preserved
across
procedure
calls
return
address
5 !
24-25 26-27 28 29 30 31
Zero: always 0, and cannot be modied $t0 -- $t9: General purpose $a0 -- $a3: General purpose (arguments) $s0 -- $s7: General purpose $sp: stack pointer $ra: return address
6 !
Moving
Data
Moving
Data
lw
Dest,
Source:
Move
4-byte
( long )
word
Constant
or
from
memory
address
To
Dest
register
Operand
Types
Immediate:
Constant
integer
data
0x
for
hex
constant
Otherwise,
decimal
Memory:
4
consecu:ve
bytes
of
memory
Various
address
modes
Register:
One
of
32
integer
registers
7 !
Operand
Addressing
Source! Destination!
C Analog!
$t1, 0x4 temp = 0x4;
li
la
$t1, A
temp2 = &A;
sw
$t1,A($t2)
A[n] = temp;
lw
$t1,A($t2)
temp = A[n];
No
instruc:on
for
reg-to-reg
transfer
Cannot
do
memory-memory
transfers
with
single
instruc:on
sw
instruc:on
violates
(dest,
source)
spec
of
operands
8 !
Memory
Accesses
Addressing
Modes
Indirect
Indexed
(R)
D(R)
Mem[Reg[R]]
Mem[Reg[R]+D]
Register
R
species
memory
address
lw $t1, ($t2)
Register
R
species
start
of
memory
block
Constant
displacement
D
species
oset
lw $t1, 8($t2)
9 !
Example
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } # xp in $a0, yp in $a1 swap: lw $t0,($a0) # t0=*xp lw $t1,($a1) # t1=*yp sw $t1,($a0) # *xp=t1 sw $t0,($a1) # *yp=t0 jr $ra
10 !
Understanding
Swap
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } # xp in $a0, yp swap: lw $t0, lw $t1, sw $t1, sw $t0, jr $ra in $a1 ($a0) ($a1) ($a0) ($a1)
0x124 0x120
123 456
11 !
0x124 0x120
123 456
12 !
# xp in $a0, yp swap: lw $t0, lw $t1, sw $t1, sw $t0, jr $ra Register !Variable $a0 xp $a1 yp $t0 $t1 Value! 120 124 456 123
0x124 0x120
123 456
13 !
0x124 0x120
123 123
14 !
# xp in $a0, yp swap: lw $t0, lw $t1, sw $t1, sw $t0, jr $ra Register !Variable $a0 xp $a1 yp $t0 $t1 Value! 120 124 456 123
0x124 0x120
456 123
15 !
MIPS
resources
Instruc0on
Reference hDp://www.mrc.uidaho.edu/mrc/people/j/ digital/MIPSir.html
Architecture
and
Assembly
Language
Overview
hDp://logos.cs.uic.edu/366/notes/mips %20quick%20tutorial.htm
16 !
Arithme:c
Opera:ons
add
$t0,
$t1,
$t2
$t0
=
$t1
+
$t2;
add
as
signed
(2's
complement)
integers
(addi
$t1,
$t2,
0x32)
sub $t1, $t2, $t2 mul $t1, $t2, $t2 div $t1, $t2, $t2 and (andi), or (ori), xor (xori) sll $t1, $t2, 2 (shio leo logical) sllv (sll variable) sra (shio right arithema:c) srl
17 !
Branch
Instruc:ons
b
(uncondi:onal)
Condi:onal
beq,
bne
bgez,
bgtz,
blez,
bltz
bgezal
bltzal
Set
Condi:on
Sltz
18 !
I/O
Instruc:ons
Service
print
integer
Call
Arguments
(input)
code
($v0)
1
$a0
=
integer
$a0
=
address
of
string
(none)
$a0=address
to
store
$a1=
length
limit
(none)
Results
signed
decimal
integer
printed
in
console
window
string
printed
in
console
window
$v0
holds
integer
that
was
entered
characters
are
stored
Ends
the
program
19 !
20 !
Direc:ves
Direc:ves
(Establish
ini:al
data
structure)
.ascii
(store
string
in
memory
with
no
null-termina:on)
.asciiz
(store
string
in
memory
with
null
termina:on)
.byte
b1,..,bn
.word
w1,..,wn
.word
w:n
(store
w
into
n
successive
words)
.space
n
data
segment
assembly
instruc:ons
!
.data .text
21 !
Resources
!
MIPS
instruc:ons
hDp://www.mrc.uidaho.edu/mrc/people/j/digital/ MIPSir.html
www.cs.uml.edu/~kim/203/mips_instr.xls
MIPS
Reading
www.cs.uml.edu/~kim/203/mips_ref.pdf
22 !
prompt: .asciiz Enter an integer to square:\n # message area .text .globl main main: # prinx( Enter an integer to square: \n ); la $a0, prompt # get the address of the message to $a0 li $v0, 4 # read to display the message syscall # scanf( %d , x); li $v0, 5 # read an integer into $a0 syscall mul $a0, $v0, $v0 # squared input value, save in $a0 # prinx( %4d , (x*x)); li $v0, 1 # print the squared value syscall 23 !
24 !
Rept:
i
=
0;
sum
+=
A[i];
i++;
if
(i
<
N)
goto
Rept
prinx( Sum
of
array
A
is,
%4d ,sum);
sum
=
0;
OR
i
=
0;
Rept:
if
(I
>=
N)
goto
Done
sum
+=
A[i];
i++;
goto
Rept
Done:
prinx( Sum
of
array
A
is,
%4d ,sum);
25 !
Rept:
i
=
0;
sum
+=
A[i];
i++;
if
(i
<
N)
goto
Rept
prinx( Sum
of
array
A
is,
%4d ,sum);
.word
.data
sum = 0;
A: main: rept:
5:20
.text .globl main li $t0, 0 # $t0 <- sum li $a0, 0 # $a0 <- array index li $t9, 80 # array index upper bound in bytes la add lw add addi blt $t8, A # $t8 <- array beginning address $t1, $t8, $a0 # $t1 <- address of current array element $t2, ($t1) # $t2 <- value in the array $t0, $t0, $t2 # sum += A[i] $a0, $a0, 4 # i++ in bytes $a0, $t9, rept
26 !
5:20
.text .globl main li $t0, 0 # $t0 <- sum li $a0, 0 # $a0 <- array index li $t9, 80 # array index upper bound in C context lw $t2, A($a0) # $t2 <- value in the array add $t0, $t0, $t2 # sum += A[i] addi $a0, $a0, 4 # i++ in bytes blt $a0, $t9, rept move $a0, $t0 # print the value of sum li $v0, 1 syscall
27 !