P. 1


|Views: 29|Likes:
Published by Jeremie

More info:

Published by: Jeremie on Aug 08, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less





MIPS Assembly Language

CPSC 321 Computer Architecture Andreas Klappenecker

MIPS Assembly Instructions 

add $t0, $t1, $t2 sub $t0, $t1, $t2 lw $t1, a_addr sw $s1, a_addr

# $t0=$t1+$t2 # $t0=$t1-$t2 # $t1=Mem[a_addr] # Mem[a_addr]=$t1 

Assembler directives 

.text .data .globl

assembly instructions follow data follows globally visible label = symbolic address

Hello World!
.text .globl main main: li $v0, 4 la $a0, str syscall li $v0, 10 syscall .data str: .asciiz Hello world!\n # NUL terminated string, as in C # system call for print string # load address of string to print # print the string # system call for exit # exit # code section

Addressing modes
lw $s1, addr lw $s1, 8($s0) # load $s1 from addr # $s1 = Mem[$s0+8]

register $s0 contains the base address access the address ($s0) possibly add an offset 8($s0)

Load and move instructions
la $a0, addr li $a0, 12 lb $a0, c($s1) lh $a0, c($s1) lw $a0, c($s1) move $s0, $s1 # load address addr into $a0 # load immediate $a0 = 12 # load byte $a0 = Mem[$s1+c] # load half word # load word # $s0 = $s1

Control Structures
Assembly language has very few control structures: 
Branch instructions  Jump instructions

if cond then goto label goto label

We can build while loops, for loops, repeat-until loops, if-then-else structures from these primitives

Branch instructions
beqz $s0, label bnez $s0, label bge $s0, $s1, label ble $s0, $s1, label blt $s0, $s1, label if $s0==0 if $s0!=0 goto label goto label

if $s0>=$s1 goto label if $s0<=$s1 goto label if $s0<$s1 goto label

beq $s0, $s1, label bgez $s0, $s1, label

if $s0==$s1 goto label if $s0>=0 goto label

if-then-else structures
if ($t0==$t1) then /* blockA */ else /* blockB */

beq $t0, $t1, blockA j blockB blockA: instructions of then block

j exit blockB: exit: instructions of else block subsequent instructions

repeat-until loop
repeat until $t0>$t1


instructions of loop ble $t0, $t1, loop # if $t0<=$t1 goto loop

Other loop structures are similar Exercise: Derive templates for various loop structures

System calls 

load argument registers load call code syscall li $a0, 10 li $v0, 1 syscall # load argument $a0=10 # call code to print integer # print $a0

SPIM system calls
procedure code $v0 print int 1 print float 2 print double 3 print string 4 argument $a0 contains number $f12 contains number $f12 contains number $a0 address of string

SPIM system calls
procedure read int read float read double read string code $v0 5 6 7 8 result res returned in $v0 res returned in $f0 res returned in $f0

Example programs 

Loop printing integers 1 to 10
1 2 3 

Increasing array elements by 5
for(i=0; i<len; i++) { a[i] = a[i] + 5; }

Print numbers 1 to 10
main: li $s0, 1 li $s1, 10 loop: move $a0, $s0 li $v0, 1 syscall li $v0, 4 la $a0, linebrk syscall addi $s0, $s0, 1 ble $s0, $s1, loop li $v0, 10 syscall # increase counter by 1 # if ($s0<=$s1) goto loop # exit # print \n # linebrk: .asciiz \n # $s0 = loop counter # $s1 = upper bound of loop # print loop counter $s0

Increase array elements by 5
.text .globl main main: la lw sll add loop: lw $t0, Aaddr $t1, len $t1, $t1, 2 $t1, $t1, $t0 $t2, 0($t0) # $t0 = pointer to array A # $t1 = length (of array A) # $t1 = 4*length # $t1 = address(A)+4*length # $t2 = A[i] # $t2 = $t2 + 5 # A[i] = $t2 # i = i+1 # if $t0<$t1 goto loop

addi $t2, $t2, 5 sw $t2, 0($t0)

addi $t0, $t0, 4 bne $t0, $t1, loop

.data Aaddr: len: .word 0,2,1,4,5 .word 5 # array with 5 elements

Increase array elements by 5
.text .globl main main: la $t0, Aaddr lw $t1, len sll $t1, $t1, 2 add $t1, $t1, $t0 # $t0 = pointer to array A # $t1 = length (of array A) # $t1 = 4*length (byte addr.) # $t1 = beyond last elem. A

Increase array elements by 5
Loop: lw $t2, ($t0) # $t2 = A[i] # $t2 = $t2 + 5 # A[i] = $t2 # i = i+1 addi $t2, $t2, 5 sw $t2, ($t0) addi $t0, $t0, 4

bne $t0, $t1, loop # if $t0<$t1 goto loop li $v0, 10 syscall # exit

Increase array elements by 5
.data Aaddr:.word 0,2,1,4,5 len: .word 5

Idiosyncratic: Byte addressing => loop in steps of 4 Describe meaning of registers in your documentation!


jal addr 

store address + 4 into $ra jump to address addr allows subroutine to jump back care must be taken to preserve $ra! more work for non-leaf procedures 

jr $ra 


one of the few means to structure your assembly language program small entities that can be tested separately can make an assembly program more readable recursive procedures

Write your own procedures
# prints the integer contained in $a0 print_int: li $v0, 1 syscall jr $ra # system call to # print integer # return

main: . . . li $a0, 10 jal print_int # we want to print 10 # print integer in $a0

Write your own procedures
.data linebrk: .asciiz ³\n´ .text print_eol: li $v0, 4 la $a0, linebrk syscall jr $ra main: . . . jal print_eol # printf(³\n´) # # # # return # prints "\n"

Write your own procedures
.data main: li $s0, 1 li $s1, 10 loop: move $a0, $s0 jal print_int jal print_eol addi $s0, $s0, 1 ble $s0, $s1, loop # $s0 = loop ctr # $s1 = upperbnd # print loop ctr # # print "\n" # loop ctr +1 # unless $s0>$s1«

Non-leaf procedures   

Suppose that a procedure procA calls another procedure jal procB Problem: jal stores return address of procedure procB and destroys return address of procedure procA Save $ra and all necessary variables onto the stack, call procB, and retore

The stack can be used for 
parameter passing  storing return addresses  storing result variables  stack pointer $sp

high address

8($sp) 4($sp)

0($sp) stack pointer $sp -->
low address

$sp = $sp - 12

fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2)

0, 1, 1, 2, 3, 5, 8, 13, 21,

li $a0, 10 jal fib move $s0, $v0 # call fib(10) # # $s0 = fib(10)

fib is a recursive procedure with one argument $a0 need to store argument $a0, temporary register $s0 for intermediate results, and return address $ra


sub $sp,$sp,12 sw $a0, 0($sp) sw $s0, 4($sp) sw $ra, 8($sp) bgt $a0,1, gen move $v0,$a0 j rreg

# save registers on stack # save $a0 = n # save $s0 # save return address $ra # if n>1 then goto generic case # output = input if n=0 or n=1 # goto restore registers # param = n-1 # compute fib(n-1) # save fib(n-1) # set param to n-2 # and make recursive call # $v0 = fib(n-2)+fib(n-1) # restore registers from stack # # # decrease the stack size


sub $a0,$a0,1 jal fib move $s0,$v0 sub $a0,$a0,1 jal fib add $v0, $v0, $s0


lw lw lw

$a0, 0($sp) $s0, 4($sp) $ra, 8($sp)

add $sp, $sp, 12 jr $ra

Practice, practice, practice!!! 

Read Chapter 3 and Appendix A Write many programs and test them Get a thorough understanding of all assembly instructions Study the register conventions carefully

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->