Architecture Overview
Data Types and Literals
Data types:
byte, halfword (2 bytes), word (4 bytes)
a character requires 1 byte of storage
an integer requires 1 word (4 bytes) of storage
Literals:
numbers entered as is ex 4
characters enclosed in single quotes ex 'b'
strings enclosed in double quotes ex "A string"
Registers
32 general-purpose registers
register preceded by $ in assembly language instruction
two formats for addressing:
using register number ex $0 through $31
using equivalent names ex $t1, $sp
register use conventions
$t0 - $t9 ( = $8 - $15, $24, $25) are general use registers; need not be preserved across procedure calls
$s0 - $s7 ( = $16 - $23) are general use registers; should be preserved across procedure calls
$sp ( = $29) is stack pointer
$fp ( = $30) is frame pointer
$ra ( = $31) is return address storage for subroutine call
$a0 - $a3 ( = $4 - $7) are used to pass arguments to subroutines
$v0, $v1 ( = $2, $3) are used to hold return values from subroutine
special registers Lo and Hi used to store result of multiplication and pision
not directly addressable; contents accessed with special instruction mfhi ("move from Hi") and mflo ("move from Lo")
stack grows from high memory to low memory
--------------------------------------------------------------------------------
MIPS Assembly Language Program Structure
just plain text file with data declarations, program code (name of file should end in suffix .s to be used with SPIM simulator)
data declaration section followed by program code section
Data Declarations
placed in section of program identified with assembler directive .data
declares variable names used in program; storage allocated in main memory (RAM)
Code
placed in section of text identified with assembler directive .text
contains program code (instructions)
starting point for code execution given label main:
ending point of main code should use exit system call (see below under System Calls)
Comments
anything following # on a line
# This stuff would be considered a comment
ex:A template for a MIPS assembly language program# Comment giving name of program and description of function# Template.s# Bare-bones outline of MIPS assembly language program.data# variable declarations here# ....textmain:# indicates start of code (first instruction to execute)# remainder of program code here# ...# ...
--------------------------------------------------------------------------------
Data Declarations
format for declarations:
name:storage_typevalue(s)
create storage for variable of specified type with given name and specified value
value(s) usually gives initial value(s); for storage type .space, gives number of spaces to be allocated
Note: labels always followed by colon ( : )
examplevar1:.word3# create a single integer variable with initial value 3array1:.byte'a','b'# create a 2-element character array with elements initialized# to a and barray2:.space40# allocate 40 consecutive bytes, with storage uninitialized# could be used as a 40-element character array, or a# 10-element integer array; a comment should indicate which!
--------------------------------------------------------------------------------
Instructions
Load / Store Instructions
RAM access only allowed with load and store instructions
all other instructions use register operands
load:
lwregister_destination, RAM_source
copy word (4 bytes) at source RAM location to destination register
lbregister_destination, RAM_source
copy byte at source RAM location to low-order byte of destination register, and sign-extend to higher-order bytes
store word:
swregister_source, RAM_destination
store word in source register into RAM destination
sbregister_source, RAM_destination
store byte (low-order) in source register into RAM destination
load immediate:
liregister_destination, value
load immediate value into destination register
example.datavar1:.word23# declare storage for var1; initial value is 23.text__start:lw$t0, var1# load contents of RAM location into register $t0: $t0 = var1li$t1, 5# $t1 = 5 ("load immediate")sw$t1, var1# store contents of register $t1 into RAM: var1 = $t1done
--------------------------------------------------------------------------------
Indirect and Based Addressing
used only with load and store instructions
load address:
la$t0, var1
copy RAM address of var1 (presumably a label defined in the program) into register $t0
indirect addressing:
lw$t2, ($t0)
load word at RAM address contained in $t0 into $t2
sw$t2, ($t0)
store word in register $t2 into RAM at address contained in $t0
based or indexed addressing:
lw$t2, 4($t0)
load word at RAM address ($t0+4) into register $t2
"4" gives offset from address in register $t0
sw$t2, -12($t0)
store word in register $t2 into RAM at address ($t0 - 12)
negative offsets are fine
Note: based addressing is especially useful for:
arrays; access elements as offset from base address
stacks; easy to access elements at offset from stack pointer or frame pointer
example.dataarray1:.space12# declare 12 bytes of storage to hold array of 3 integers.text__start:la$t0, array1# load base address of array into register $t0li$t1, 5# $t1 = 5 ("load immediate")sw$t1, ($t0)# first array element set to 5; indirect addressingli$t1, 13# $t1 = 13sw$t1, 4($t0)# second array element set to 13li$t1, -7# $t1 = -7sw$t1, 8($t0)# third array element set to -7done
--------------------------------------------------------------------------------
Arithmetic Instructions
most use 3 operands
all operands are registers; no RAM or indirect addressing
operand size is word (4 bytes)
add$t0,$t1,$t2# $t0 = $t1 + $t2; add as signed (2's complement) integerssub$t2,$t3,$t4# $t2 = $t3 D $t4addi$t2,$t3, 5# $t2 = $t3 + 5; "add immediate" (no sub immediate)addu$t1,$t6,$t7# $t1 = $t6 + $t7; add as unsigned integerssubu$t1,$t6,$t7# $t1 = $t6 + $t7; subtract as unsigned integersmult$t3,$t4# multiply 32-bit quantities in $t3 and $t4, and store 64-bit# result in special registers Lo and Hi: (Hi,Lo) = $t3 * $t4p$t5,$t6# Lo = $t5 / $t6 (integer quotient)# Hi = $t5 mod $t6 (remainder)mfhi$t0# move quantity in special register Hi to $t0: $t0 = Himflo$t1# move quantity in special register Lo to $t1: $t1 = Lo# used to get at result of product or quotientmove$t2,$t3# $t2 = $t3
--------------------------------------------------------------------------------
Control Structures
Branches
comparison for conditional branches is built into instruction
btarget# unconditional branch to program label targetbeq$t0,$t1,target# branch to target if $t0 = $t1blt$t0,$t1,target# branch to target if $t0 < $t1ble$t0,$t1,target# branch to target if $t0 <= $t1bgt$t0,$t1,target# branch to target if $t0 > $t1bge$t0,$t1,target# branch to target if $t0 >= $t1bne$t0,$t1,target# branch to target if $t0 <> $t1
Jumps
jtarget# unconditional jump to program label targetjr$t3# jump to address contained in $t3 ("jump register")
Subroutine Calls
subroutine call: "jump and link" instruction
jalsub_label# "jump and link"
copy program counter (return address) to register $ra (return address register)
jump to program statement at sub_label
subroutine return: "jump register" instruction
jr$ra# "jump register"
jump to return address in $ra (stored by jal instruction)
Note: return address stored in register $ra; if subroutine will call other subroutines, or is recursive, return address should be copied from $ra onto stack to preserve it, since jal always places return address in this register and hence will overwrite previous value
--------------------------------------------------------------------------------
System Calls and I/O (SPIM Simulator)
used to read or print values or strings from input/output window, and indicate program end
use syscall operating system routine call
first supply appropriate values in registers $v0 and $a0-$a1
result value (if any) returned in register $v0
ex Print out integer value contained in register $t2
li$v0, 1# load appropriate system call code into register $v0;
# code for printing integer is 1
move$a0, $t2# move integer to be printed into $a0: $a0 = $t2
syscall# call operating system to perform operation
ex Read integer value, store in RAM location with label int_value (presumably declared in data section)
li$v0, 5# load appropriate system call code into register $v0;
# code for reading integer is 5
syscall# call operating system to perform operation
sw$v0, int_value# value read from keyboard returned in register $v0;
# store this in desired location
ex Print out string (useful for prompts)
.data
string1.asciiz"Print this.n"# declaration for string variable
.text
main:li$v0, 4# load appropriate system call code into register $v0;
# code for printing string is 4
la$a0, string1# load address of string to be printed into $a0
syscall# call operating system to perform print operation
Note:
string must be implemented as array of characters, terminated by null ( )data type declaration .asciiz automatically null-terminates string
Note: To indicate end of program, use exit system call; thus last lines of program should be:
li$v0, 10# system call code for exit = 10syscall# call operating system
Table of System Call Codes and Arguments(from SPIM S20: A MIPS R2000 Simulator, James J. Larus, University of Wisconsin-Madison)
Service System Call Code Arguments Result print integer 1 $a0 = value (none) print float 2 $f12 = float value (none) print double 3 $f12 = double value (none) print string 4 $a0 = address of string (none) read integer 5 (none) $v0 = value read read float 6 (none) $f0 = value read read double 7 (none) $f0 = value read read string 8 $a0 = address where string to be stored$a1 = number of characters to read + 1 (none) memory allocation 9 $a0 = number of bytes of storage desired $v0 = address of block exit (end of program) 10 (none) (none)
--------------------------------------------------------------------------------
Examples
Example 1
# Compute the value of the sum 1*2 + 2*3 + 3*4 + ... + 10*11, and store in register $t1.data# variable declaration sectionout_string:.asciiz"The result is:n"# declares a null-terminated string, to "prettify" output.textmain:# indicates start of codeli$t0, 1# $t0 will be a counter; initialize to 1li$t1, 0# $t1 will hold the sumli$t2, 10# $t2 will hold loop limitloop_top:bgt$t0,$t2,loop_end# exit loop if $t0 > 10addi$t3,$t0,1# $t3 = $t0 + 1mult$t0,$t3# special register Lo = $t0 * $t3# (don't need Hi since values are small)mflo$t3# $t3 = Lo (= $t0 * $t3)add$t1,$t1,$t3# $t1 = $t1 + $t3addi$t0, 1# increment counterbloop_top# branch to loop_toploop_end:# print out the result stringli$v0, 4# system call code for printing string = 4la$a0, out_string# load address of string to be printed into $a0syscall# call operating system to perform print operation# print out integer value in $t1li$v0, 1# system call code for printing integer = 1move$a0, $t1# move integer to be printed into $a0: $a0 = $t1syscall# call operating system to perform print# exit programli$v0, 10# system call code for exit = 10syscall# call operating system# blank line at end to keep SPIM happy!
--------------------------------------------------------------------------------
Example 2
# Code with subroutine to compute Fibonacci number recursively# Uses system stack.datain_string:.asciiz"Input a positive integer:nn"out_string:.asciiz"The Fibonacci number is:nn".textmain:# print out promptli$v0, 4# system call code for printing string = 4la$a0, in_string# load address of string to be printed into $a0syscall# call operating system to perform print operation# read integer into $s0li$v0, 5# system call code for read integer = 5syscall# call operating systemmove$s0, $v0# value read from keyboard returned in register $v0; transfer to $s0sw$s0,($sp)# push argument for Fib on stackaddi$sp,$sp,-4# and decrement stack pointerjalFib# jump to subroutineaddi$sp,$sp,4# increment stack pointerlw$s1,($sp)# and pop result from stack# print out promptli$v0, 4# system call code for printing string = 4la$a0, in_string# load address of string to be printed into $a0syscall# call operating system# print out result (stored in $s1)li$v0, 1# system call code for printing integer = 1move$a0, $s1# move integer to be printed into $a0: $a0 = $s1syscall# call operating system to perform print# exit programli$v0, 10# system call code for exit = 10syscall# call operating system# blank line at end to keep SPIM happy!################################################################################### Fibonacci subroutine# input: integer n, on stack# output: Fib(n), nth Fibonacci number# description: recursively computes Fib(n) = Fib(n-1) + Fib(n-2), Fib(1) = Fib(2) = 1.# uses: $t0, $t1##################################################################################Fib:# procedure prologue:sw$ra,($sp)# save return address on stack, since recursive,addi$sp,$sp,-4# and decrement stack pointersw$fp,($sp)# save previous frame pointer on stackaddi$sp,$sp,-4# and decrement stack pointeradd$fp,$sp,12# set frame pointer to point at base of stack framelw$t0,($fp)# copy argument to $t0: $t0 = nli$t1, 2bgt$t0,$t1,do_recurse# if argument n >= 2, branch to recursive sequenceli$t0, 1# else set result to 1 (base cases n = 1 and n = 2)bepilogue# branch to enddo_recurse:addi$t0,$t0,-1# $t0 = n-1sw$t0,($sp)# push argument n-1 on stackaddi$sp,$sp,-4# and decrement stack pointerjalFib# call Fibonacci with argument n-1# leave result on stack for nowlw$t0,($fp)# re-copy argument to $t0: $t0 = naddi$t0,$t0,-2# $t0 = n-2sw$t0,($sp)# push argument n-2 on stackaddi$sp,$sp,-4# and decrement stack pointerjalFib# call Fibonacci with argument n-2addi$sp,$sp,4# increment stack pointerlw$t0,($sp)# and pop result of Fib(n-2) from stack into $t0addi$sp,$sp,4# increment stack pointerlw$t1,($sp)# and pop result of Fib(n-1) from stack into $t1add$t0,$t0,$t1# $t0 = Fib(n-2) + Fib(n-1); have resultepilogue:# procedure epilogue: $t0 holds resultaddi$sp,$sp,4# increment stack pointerlw$fp,($sp)# and pop saved frame pointer into $fpaddi$sp,$sp,4# increment stack pointerlw$ra,($sp)# and pop return address into $raaddi$sp,$sp,4# increment stack pointer# to pop argument (n) from stack (discard)sw$t0,($sp)# push result onto stackaddi$sp,$sp,-4# and decrement stack pointerjr$ra# return to caller################################################################################### end of Fibonacci##################################################################################