EXAMPLE 1 - SIMPLE ARITHMETIC and LOAD/STORE VALUES
###########################################################
# This program will ask for two integers and then it
# adds and subtracts them.
# It demostrates how to use arithmetic operations, and
# how to load and store values.
###########################################################
.text
.globl main
main: li $v0,4 # system call to print MSG1
la $a0, MSG1
syscall
li $v0,5 # system call to get integer1
syscall
la $t5, INPUT1
sw $v0, 0($t5) # store integer in t5
li $v0,4 # system call to print MSG2
la $a0, MSG2
syscall
li $v0,5 # system call to get integer2
syscall
la $t6, INPUT2
sw $v0, 0($t6) # store integer2 in $t6
lw $t0, 0($t5) # load values in $t0 and $t1
lw $t1, 0($t6)
li $v0,4 # system call to print SUM
la $a0, SUM
syscall
add $t3, $t0, $t1 # add $t0 and $t1
li $v0,1 # system call to print result
move $a0, $t3 # copy value to $a0
syscall
li $v0,4 # system call to print SUB
la $a0, SUB
syscall
sub $t4, $t0, $t1 # subtract $t1 from $t0
li $v0,1 # system call to print result
move $a0, $t4 # copy value to $a0
syscall
li $v0,10 # system call 10; exit
syscall
.data
MSG1: .asciiz "Enter the 1st number => "
MSG2: .asciiz "Enter the 2nd number => "
SUM: .ascciz "Integer1 + Integer2 = "
SUB: .ascciz "Integer1 - Integer2 = "
# a word boundary alignment
.align 2
# reserve a word space
INPUT1: .space 4
INPUT2: .space 4
EXAMPLE 2 - LOOPS
###########################################################
# THIS PROGRAM WILL ASK YOU FOR A NUMBER, AND THEN IT WILL
# COMPUTE THE SUM 1 TO N.
# IT DEMOSTRATES HOW TO IMPLEMENT LOOPS
###########################################################
.text
.globl main
main:
li $v0,4 # mesg1 asking for a number
la $a0, msg1
syscall
li $v0,5 # system call that reads an integer
syscall
move $t0,$v0
li $t1, 0 # initialize loop counter
li $t2, 0 # initialize total sum to 0
loop: addi $t1, $t1, 1 # increment loop counter by 1
add $t2, $t2, $t1 # update value of sum
beq $t0, $t1, exit # if if counter = N, goto exit label
j loop # otherwise loop again
exit: li $v0, 4 # print mesg2
la $a0, msg2
syscall
li $v0,1 # print sum
move $a0, $t2
syscall
li $v0,4 # print an end of line
la $a0, cr
syscall
li $v0,10 # exit
syscall
.data
msg1: .asciiz "Emter a integer number: "
msg2: .asciiz "The sum = "
cr: .asciiz "\n"
###########################################################
# This program calculates the factorial of N
# It demostrates how to implement function calls.
###########################################################
.text
.globl main
main:
subu $sp, $sp, 32 # 32-byte stack frame
sw $ra, 20($sp) # save return address
sw $fp, 16($sp) # save frame pointer
addu $fp, $sp, 32 # set new frame pointer
START:
la $a0, str_1 # system call to print str_1
li $v0, 4
syscall
li $v0, 5 # system call to read an integer
syscall
beq $v0, $0, EXIT # check if integer > 0
move $a0, $v0 # $v0 keeps the input value
jal FACT # call fact(), ra <- next PC
la $a1, result # result is stored in $v0
sw $v0, 0($a1) # store the value into memory
la $a0, str_2 # system call to print str_2
li $v0, 4 # call number 4
syscall
la $a1, result # read the value from memory
lw $a0, 0($a1) # move fact result to $a0
li $v0, 1 # print the result
syscall
j START
EXIT:
la $a0, str_3
li $v0, 4 # system call to print str_3
syscall
lw $ra, 20($sp) # restore return address
lw $fp, 16($sp) # restore frame pointer
addu $sp, $sp, 32 # adjust stack pointer
jr $ra # return to caller 'main'
jr $ra
FACT:
subu $sp, $sp, 32 # 32-byte stack frame
sw $ra, 20($sp) # save return address
sw $fp, 16($sp) # save frame pointer
addu $fp, $sp, 32 # set up a new frame pointer
sw $a0, 0($fp) # save fact(n) function argument n
lw $2, 0($fp) # read fact(n) function argument n
bgtz $2, Loop2 # branch if n>0
li $2, 1 # fact(0) = 1
j Loop1
Loop2: lw $3, 0($fp) # read fact(n) function argument n
subu $2, $3, 1 # n <- n-1
move $a0, $2
jal FACT
lw $3, 0($fp) # read fact(n) function argument n
mul $2, $2, $3 # compute fact(n-1)*n
Loop1: lw $ra, 20($sp)
lw $fp, 16($sp)
addu $sp, $sp, 32
jr $ra
.data
str_1: .asciiz "Enter the number for factorial(0 to exit) => "
str_2: .asciiz "The factorial result is "
str_3: .asciiz "\n"
.align 2
result: .space 4