Upload
mr741333836
View
213
Download
1
Embed Size (px)
Citation preview
Wahyu Fadli Satrya13211135
Pak Yudi GondokaryonoEL3011 Arsitektur Komputer PR SPIM3
SPIM Exception Handler & Subroutine Linkage
Part I. SPIM Exception Handler# Given positive integers , assume that miles identified with miles traveled and “gallon” is gallons of gasoline consumed, output will be a gallon/miles named with a/b.
.datagallons:.asciiz "Enter the amount of gallons consumed (a): "miles: .asciiz "Enter the number of miles traveled(b): "adivb: .asciiz "a/b = " #First of all I use miles/gallons name, but when I am simulating it in PCSpim, it rejects my code due to “/” therefore I change it to “div” I don’t know why did spim still read the / as operator in that line.unit : .asciiz “miles per gallons”newline: .asciiz "\n"
.textmain: addu $s0, $ra, $0 # save $31 in $16li $v0, 4 # system call code for print_stringla $a0, miles # address of milessyscall # print miles
#get the value of gallons from user, put it into $s0li $v0, 5 # system call code for read_intsyscall # read an integer into $v0 from console
move $t1, $v0 # $v0 di $t1#read print_string for milesli $v0, 4 # system call code for print_stringla $a0, gallons # address of gallonssyscall # print gallons
# get the number of miles traveled from user, put it into $t1li $v0, 5 # load syscall for read_intsyscall # make the syscall
#DO THE CALCULATIONSdiv $t1 , $v0 #diving $s0 by $s1#read print_string for adivbli $v0, 4 # system call code for print_stringla $a0, adivb # address of gallons/milessyscall # print gallons/miles
mflo $a0 # storing value of lo(quotient) in# register $t0li $v0,1 # miles/gallonsyscall
li $v0,4 # print unitla $a0,unit # "miles per gallon"syscall
# restore now the return address in $ra and return from mainaddu $ra, $0, $s0 # return address back in $31jr $ra # return from main# end of file
Wahyu Fadli Satrya13211135
Pak Yudi GondokaryonoEL3011 Arsitektur Komputer PR SPIM3
Part II. Subroutine Linkage# multiplication --- read an integer U V, and print its result## This program uses simple linkage.## Settings: Load delays ON; Branch delays ON# Trap file ON; Pseudoinstructions ON#
.data 0x10010000valueu: .asciiz "Enter u value: "valuev: .asciiz "Enter v value: "hasilout: .asciiz "the result of 5u^2 - 12uv = "
.text
.globl mainmain: addu $s0, $ra, $0 # save $31 in $16
# Read input u from userli $v0,4 # service 4, to print stringla $a0,valueu # print “Enter u value:”syscall
li $v0,5 # read in u typed by user, named withcode 5syscall
move $a1, $v0 # $v0 di $a1move $a2, $v0 # $v0 di $a2li $t1, 5 # integer 5
# do the calculationjal uv # compute process, in this case,literally uv subroutine takes argument u and u in address $a1 and$a2nopmove $t3, $v0 # save it in $t3
# Read input v from userli $v0,4 # service 4la $a0,valuev # print “Enter value of v:”syscall
li $v0,5 # read the value of v inputted by usersyscall
move $a2, $v0 # $v0 di $a2li $t1, 12 # angka 12
# do the calculationjal uv # compute processnopmove $a3,$v0 # save it in $a3
# print its resultli $v0,4 # printla $a0,hasilout # "then the value of 5u^2 - 12uv="syscall
sub $v0, $t3, $a3move $a0,$v0 # save it in $a0li $v0,1 # print hasilsyscall
Wahyu Fadli Satrya13211135
Pak Yudi GondokaryonoEL3011 Arsitektur Komputer PR SPIM3
# return addressaddu $ra, $0, $s0 # return address back in $31jr $ra # return from main
# SUBROUTINE PROGRAM!-------------------------------------------------------############################################################## uv -- compute u*v## on entry:# $ra -- return address# $a0 -- x## on exit:# $v0 -- x^2
.text
.globl uv# print its result
li $v0,4 # printla $a0,hasilout # "then the value of 5u^2 - 12uv="syscall
sub $v0, $t3, $a3move $a0,$v0 # save it in $a0li $v0,1 # print hasilsyscall
# return addressaddu $ra, $0, $s0 # return address back in $31jr $ra # return from main
# SUBROUTINE PROGRAM!-------------------------------------------------------############################################################## uv -- compute u*v## on entry:# $ra -- return address# $a0 -- x## on exit:# $v0 -- x^2
.text
.globl uv