Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

03 Mips

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Topics

What resources MIPS assembly manipulate Represen:ng informa:on as bits


Binary/Hexadecimal Byte representa:ons numbers characters and strings Instruc:ons

MIPS Architecture

1 !

MIPS MARS simulator


Please download, install, and get familiar with hDp://courses.missouristate.edu/KenVollmar/ MARS/download.htm

2 !

MIPS Architecture

3 !

Assembly Programmer s View


Memory! Addresses " Object Code " Program Data " OS Data " Registers! Data " Instructions " PC! Condition! Codes! CPU !

Stack!

Programmer-Visible State
Program Counter (PC) Address of next instruc:on

Memory
Byte addressable array Code, user data, (some) OS data Includes stack

Loca:on Counter (LOC) Register File


Heavily used program data

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

$t8 - $t9 $k0 - $k1 $gp $sp $s8/$fp $ra

Registers 32-bit 32 Registers


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;

Imm! Addr! Reg! Mem!

Reg! Reg! Mem! Reg!

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)

Register !Variable $a0 xp $a1 yp $t0 $t1

Value! 120 124

0x124 0x120

123 456

11 !

# xp in $a0, yp swap: lw $t0, lw $t1, sw $t1, sw $t0, jr $ra

in $a1 ($a0) ($a1) ($a0) ($a1)

Register !Variable $a0 xp $a1 yp $t0 $t1

Value! 120 124 456

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

in $a1 ($a0) ($a1) ($a0) ($a1)

0x124 0x120

123 456

13 !

# xp in $a0, yp swap: lw $t0, lw $t1, sw $t1, sw $t0, jr $ra

in $a1 ($a0) ($a1) ($a0) ($a1)

Register !Variable $a0 xp $a1 yp $t0 $t1

Value! 120 124 456 123

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

in $a1 ($a0) ($a1) ($a0) ($a1)

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 !

print string 4 Read integer 5

Read string 8 exit 10

Example: Hello World


.data st_hello: .asciiz "Hello, world ! \n # message area .text .globl main main: la $a0, st_hello # get the address of message to $a0 li $v0, 4 # read to display the message syscall li $v0, 10 # quit the program syscall

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

MARS MIPS simulator


hDp://courses.missouristate.edu/KenVollmar/MARS/

22 !

Example: Square an input Number


prinx( Enter an integer to square: \n ); scanf( %d , x); prinx( %4d , (x*x));
.data

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 !

Condi:onal Branch Example


# x in $a0, y in $a1 # return value in $t0 max: bgt $a0, $a1, ret_x or $t0, $a1, $0 jr $ra ret_x: or jr $t0, $a0, $0 $ra

int max(int x, int y) { if (x > y) return x; else return y; }

24 !

Example: Add N-element Array


for (i=0; i<N; i++) sum += A[i]; prinx( Sum of array A is, %4d ,sum);
sum = 0; sum = 0;

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

# 20-element array of ini:al values of 5

.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

move $a0, $t0 # print the value of sum li $v0, 1 syscall

26 !

Common, readable approach: Use array as displacement in indirect addressing


A: main: rept: .word
.data

5:20

# 20-element array of ini:al values of 5

.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 !

You might also like