| Colour Theme | Font Size Options | |
In this lab, we are going to explore how to do arithmetic operations in SPIM. SPIM is a simulator for the MIPS. MIPS processor contains 32 general purpose registers that are numbered from 0 to 31. The registers are used in the SPIM instructions, particularly, in arithmetic operations. The next section provides a summary of the 32 registers that will be used in our labs.
Here is a summary of the MIPS register file for your reference.
Register Name Number Usage =============================================================== Zero $0 Constant 0 --------------------------------------------------------------- $at $1 Reserved for assembler --------------------------------------------------------------- $v0 $2 Used for the expression evaluation $v1 $3 and results of a function --------------------------------------------------------------- $a0 $4 Used to pass arguments to functions $a1 $5 $a2 $6 $a3 $7 --------------------------------------------------------------- $t0 - $t7 $8-$15 Temporary (not preserved across call) --------------------------------------------------------------- $s0 - $s7 $16-$23 Saved temporary (preserved across call) --------------------------------------------------------------- $t8 - $t9 $24-$25 Temporary (not preserved across call) --------------------------------------------------------------- $k0 - $k1 $26-$27 Reserved for OS kernel --------------------------------------------------------------- $gp $28 Pointer to global area --------------------------------------------------------------- $sp $29 Stack Pointer --------------------------------------------------------------- $fp $30 Frame Pointer --------------------------------------------------------------- $ra $31 Return address (used by function call) ---------------------------------------------------------------
NOTE: The MIPS processor also has 16 floating point registers $f0, $f1, ..., $f15 to hold numbers in floating point form, such as 3.1415.
Arithmetic instructions are very basic and frequently used in your
SPIM programming.
Here is a table that summarizes the usage of the MIPS
processor's arithmetic instructions with examples.
Instruction Mnemonic Meaning
========================================================================
Add add $t1,$t2,$t3 # $t1=$t2+$t3
------------------------------------------------------------------------
Add immediate addi $t1,$t2,50 # $t1=$t2+50
------------------------------------------------------------------------
Subtract sub $t1,$t2,$t3 # $t1=$t2-$t3
------------------------------------------------------------------------
Multiply mul $t1,$t2,$t3 # $t1=$t2*$t3
------------------------------------------------------------------------
Multiply mult $t2,$t3 # Hi,Lo=$t2*$t3
------------------------------------------------------------------------
Divide div $t1,$t2,$t3 # $t1=$t2/$t3
------------------------------------------------------------------------
Divide div $t2,$t3 # Lo=$t2/$t3
# Hi=$t2 mod $t3
------------------------------------------------------------------------
Move from Hi mfhi $t1 # $t1=Hi
# get copy of Hi
------------------------------------------------------------------------
Move from Lo mflo $t1 # $t1=Lo
# get copy of Lo
------------------------------------------------------------------------
##
## The program --- convertC2F.s
##
## - will ask the user for a temperature in Celsius,
## - convert it to Fahrenheit, and
## - print the result.
##
## Here is the formula of the conversion:
## F = (9*C/5)+32
##
## $v0 - reads in Celsius
## $t0 - holds Fahrenheit result
## $a0 - points to output strings
##
#################################################
# #
# text segment #
# #
#################################################
.text
.globl __start
__start: # execution starts here
la $a0,prompt # print prompt on terminal
li $v0,4 # system call to print
syscall # out a string
li $v0,5 # syscall 5 reads an integer
syscall
mul $t0,$v0,9 # to convert, multiply by 9,
div $t0,$t0,5 # divide by 5, then
addi $t0,$t0,32 # add 32
la $a0,ans1 # print string before result
li $v0,4
syscall
move $a0,$t0 # print result
li $v0,1
syscall
la $a0,endl # syscal to print out
li $v0,4 # a new line
syscall
li $v0,10
syscall # Bye!
#################################################
# #
# data segment #
# #
#################################################
.data
prompt: .asciiz "Enter temperature (Celsius): "
ans1: .asciiz "The temperature in Fahrenheit is "
endl: .asciiz "\n"
##
## end of file convertC2F.s
Here is a program that should produce the answer of 180. Let's have a look and see what it gives us.
##
## Program Name: math1.s
##
## - will calculate the value of
## - A*X^2+B*X+C and
## - print the result in format "Answer = 180".
##
##
## $t4 - holds the result of the calculation
## $a0 - points to output strings
##
#################################################
# #
# text segment #
# #
#################################################
.text
.globl __start
__start: # execution starts here
lw $t0,X
lw $t1,A
lw $t2,B
lw $t3,C
mul $t4,$t0,$t0 # t4 = X^2
mul $t4,$t4,$t1 # t4 = A*X^2
mul $t5,$2,$t0 # t5 = B*X
add $t4,$t4,$t5 # t4 = A*X^2+B*X
addi $t4,$t4,$t3 # t4 = A*X^2+B*X+C
la $a0,ans1 # print string before result
li $v0,4
syscall
move $a0,$t4 # print result
li $v0,1
syscall
la $a0,endl # syscal to print out
li $v0,4 # a new line
syscall
li $v0,10 # Exit
syscall # Bye!
#################################################
# #
# data segment #
# #
#################################################
.data
X: .word 7
A: .word 3
B: .word 4
C: .word 5
ans1: .asciiz "Answer = "
endl: .asciiz "\n"
##
## end of file math1.s
In SPIM programming, syntax errors are easy to locate and fix
because the assembler will automatically tell you which line
has a problem. To find a logical error, you can single step
through the program watching the values in the registers as each
instruction is executed. Single stepping is extremely important
because it is the best way to find a logical error in your program
by pinpointing the precise line which caused the error.
To step through your program, you must either declare a break point
or start stepping from __start label line by line.
Your lab instructor will demonstrate the debugging process
with the above example in the lab.