Commit 5973f3b6 authored by Alan Marchiori's avatar Alan Marchiori

added lec14

parent 13d7ba46
####################################################
#Author: Dean Liu
#Contact: deanliu@cs.ucsb.edu
# deanliu@umail.ucsb.edu
#Date: May 1st,2005
#Description: MIPS Assembly version of
# 99 bottles
# of beer on the wall
####################################################
.data #data portion
strng: .asciiz " bottles of beer on the wall, "
strng2: .asciiz " bottles of beer "
strng3: .asciiz "\ntake one down and pass it around, "
strng4: .asciiz " bottle of beer on the wall "
strng5: .asciiz " bottle of beer "
strng6: .asciiz " bottles of beer on the wall.\n "
one: .word 1
.text #code section
main: #main
li $a2, 1 #
li $a3, 99 #start with 99
loop: jal PRNTB #prnt bottle count
la $a0, strng #print strng
li $v0, 4 #
syscall #
jal PRNTB #
la $a0, strng2 #print strng2
li $v0, 4 #
syscall #
la $a0, strng3 #print strng3
li $v0, 4 #
syscall #
sub $a3, $a3, 1 #subtract one
jal PRNTB #
la $a0, strng6 #print strng6
li $v0, 4 #
syscall #
bne $a3, $a2, skip #handles one
#bottle on wall
jal ONEBOT #
skip: bnez $a3, loop #Loop if not equal
#to 0
li $v0, 10 #exit
syscall #
####################################################
# Routine: PRINTB($a0)
# Description: Prints out current bottle count
####################################################
PRNTB: move $a0, $a3 #prnt bottle count
li $v0, 1 #
syscall #
jr $ra #return from method
####################################################
# Routine: ONEBOT($a0)
# Description: Handles one bottle count.
####################################################
ONEBOT: addi $sp,$sp,-4 #allocate
sw $ra,0($sp) #store rtrn
#address
jal PRNTB #
la $a0, strng4 #print strng4
li $v0, 4 #
syscall #
jal PRNTB #
la $a0, strng5 #print strng5
li $v0, 4 #
syscall #
la $a0, strng3 #print strng3
li $v0, 4 #
syscall #
sub $a3, $a3, 1 #subtract one
jal PRNTB #
la $a0, strng6 #print strng6
li $v0, 4 #
syscall #
lw $ra,0($sp) #load address
addi $sp,$sp,4 #pop
jr $ra #rtrn from method
\ No newline at end of file
.data
prompt: .asciiz "N = "
sep: .asciiz "\n"
.text
main:
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
move $a0, $v0
jal fact
move $s0, $v0
li $v0, 4
la $a0, sep
syscall
li $v0, 1
move $a0, $s0
syscall
li $v0, 10
syscall
fact:
addi $sp, $sp, -8
sw $ra, 4($sp)
sw $a0, 0($sp)
beq $a0, 0, fact_1
addi $a0, $a0, -1
jal fact
#restore my ra and a0 values from the stack
lw $ra, 4($sp)
lw $a0, 0($sp)
addi $sp, $sp, 8
mul $v0, $a0, $v0
jr $ra
fact_1:
li $v0, 1
addi $sp, $sp, 8
jr $ra
.data
prompt: .asciiz "How many? "
sep: .asciiz ", "
.text
main:
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
move $s0, $v0 # store input in $s0
loop:
move $a0, $s0
addi $s0, $s0, -1
jal fib
li $v0, 1
move $a0, $t0
syscall
beq $s0, 0, exit
li $v0, 4
la $a0, sep
syscall
j loop
exit:
li $v0, 10
syscall
fib:
beq $a0, 0, exit0
beq $a0, 1, exit1
addi $a0, $a0, -1
jal fib
move $v0, $t0
addi $a0, $a0, -1
jal fib
add $v0, $v0, $t0
jr $ra
exit1:
li $v0, 1
jr $ra
exit0:
li $v0, 0
jr $ra
\ No newline at end of file
.data
prompt: .asciiz "How many? "
sep: .asciiz ", "
.text
main:
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
move $s0, $v0 # store input in $s0
loop:
move $a0, $s0
addi $s0, $s0, -1
jal fib
li $v0, 1
move $a0, $t0
syscall
beq $s0, 0, exit
li $v0, 4
la $a0, sep
syscall
j loop
exit:
li $v0, 10
syscall
fib:
li $v0, 0
jr $ra
\ No newline at end of file
.data
prompt: .asciiz "Enter temp in F: "
result: .asciiz "In C that is: "
.text
main:
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
move $a0, $v0
jal f_to_c
move $t0, $v0
li $v0, 4
la $a0, result
syscall
li $v0, 1
move $a0, $t0
syscall
exit:
li $v0, 10
syscall
f_to_c:
addi $a0, $a0, -32
mul $a0, $a0, 5
div $v0, $a0, 9
jr $ra
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment