Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Only allowed to use these instructions and, andi, add, addi, addiu, sub, la, lw,

ID: 3555218 • Letter: O

Question

Only allowed to use these instructions

and, andi, add, addi, addiu, sub, la, lw, lh, lb, nor, or, ori, sll, srl, sra, sw, sh, sb, slt, slti, bne, beq, j, jr, syscall, xor, xori, mult, multu, div, divu, mflo, mfhi, jal, mtc1, mfc1,

I wish I could give more points for this..

average Function:

You will put your code in a function named average. The average function will be passed 7parameters. The first parameter, in $a0, will have a 1, 2, or 3. For a 1, you are to compute and return the batting average. For a 2, you are to compute and return the slugging percentage. for a3, you are to compute and return the on-base percentage. $a1 will contain the number of walks,$a2 will contain the number of singles, $a3 the number of doubles. The 5th, 6th and 7th parameters will be found on the stack, and will be the number of triples, home runs and outs, respectively.

Your function will print the statistics for the player by calling the provided printStatsprocedure. printStats takes sixarguments: $a0 is the number of walks, $a1 singles, $a2doubles, $a3 triples, the fifth argument is the number of home runs, and the sixth argument is the number of outs.

Your function will then compute either the batting average or the slugging percentage. Compute each using single-precision floating-point calculations. Return the result in $v0 (as is normally done for functions).

Label naming conventions:

Labels in MIPS programs are global. This presents a problem when you want to use a label that is already in use elsewhere in the program. To avoid conflicts in labels between your code and the test cases, we will adopt the following practice:

Labels will have the name of the procedure or function at the start of the label. For example, if I want to use the labelLoopBegin: in main, then I will name the label mainLoopBegin:. If you also want to use the label LoopBegin: in youraverage function, you will name itaverageLoopBegin:.

You can see examples of this naming scheme in the labels used in the provided main andprintStats procedure.

Note that every label you use in the code for your average function will have average as the first part of the label.

We will check that you are following these labeling conventions when we grade your program!

Example:

We will provide test data, main: and printStats: in each test case.

You are encouraged to:

Explanation / Answer

# test with two batters
# computes batting average, slugging percentage and on-base percentage
# for each batter
# This is the test case from the program description
.data
mainNumBatters:
.word 2
mainBatter1:
.word 27 # walks
.word 101 # singles
.word 22 # doubles
.word 4 # triples
.word 10 # home runs
.word 423 # outs
mainBatter2:
.word 0, 0, 0, 0, 0, 0 # player with no atBats
.data
mainNewline:
.asciiz " "
mainBatterNumber:
.asciiz "Batter number: "
mainBattingAverage:
.asciiz "Batting average: "
mainSluggingPercentage:
.asciiz "Slugging percentage: "
mainOnbasePercentage:
.asciiz "On-base percentage: "
.text
main:
# Function prologue -- even main has one
addiu $sp, $sp, -24 # allocate stack space -- default of 24 here
sw $fp, 0($sp) # save frame pointer of caller
sw $ra, 4($sp) # save return address
addiu $fp, $sp, 20 # setup frame pointer of main
  
# for (i = 0; i < mainNumBatters; i++)
# compute batting average
# compute slugging average
  
la $s0, mainNumBatters
lw $s7, 0($s0) # $s7 = number of batters
addi $s6, $zero, 0 # $s6 = i = 0
la $s0, mainBatter1 # $s0 = addr of current batter's stats
mainLoopBegin:
slt $t0, $s6, $s7 # $t0 = i < number of batters
beq $t0, $zero, mainDone
la $a0, mainBatterNumber
addi $v0, $zero, 4
syscall
addi $a0, $s6, 1
addi $v0, $zero, 1
syscall
la $a0, mainNewline
addi $v0, $zero, 4
syscall
# Compute the batting average
addi $a0, $zero, 1 # $a0 = 1 = compute batting average
lw $a1, 0($s0) # $a1 = walks
lw $a2, 4($s0) # $a2 = singles
lw $a3, 8($s0) # $a3 = doubles
lw $s1, 12($s0) # $s1 = triples
lw $s2, 16($s0) # $s2 = home runs
lw $s3, 20($s0) # $s3 = outs
  
sw $s3, -4($sp) # put outs at top of average's stack
sw $s2, -8($sp) # put homeruns 2nd fm top of average's stack
sw $s1, -12($sp) # put triples 3rd fm top of average's stack
jal average
  
# Print the batting average
mtc1 $v0, $f12 # get result fm $v0 before we print string
la $a0, mainBattingAverage
addi $v0, $zero, 4
syscall
addi $v0, $zero, 2 # print the average
syscall
la $a0, mainNewline
addi $v0, $zero, 4
syscall
syscall
  
# do it for the slugging percentage
addi $a0, $zero, 2 # $a0 = 2 = compute slugging average
lw $a1, 0($s0) # $a1 = walks
lw $a2, 4($s0) # $a2 = singles
lw $a3, 8($s0) # $a3 = doubles
lw $s1, 12($s0) # $s1 = triples
lw $s2, 16($s0) # $s2 = home runs
lw $s3, 20($s0) # $s3 = outs
sw $s3, -4($sp) # put outs at top of average's stack
sw $s2, -8($sp) # put homeruns 2nd fm top of average's stack
sw $s1, -12($sp) # put triples 3rd fm top of average's stack
jal average
  
# Print the slugging percentage
mtc1 $v0, $f12 # get result fm $v0 before we print string
la $a0, mainSluggingPercentage
addi $v0, $zero, 4
syscall
addi $v0, $zero, 2 # print the percentage
syscall
la $a0, mainNewline
addi $v0, $zero, 4
syscall
syscall
# do it for the on-base percentage
addi $a0, $zero, 3 # $a0 = 3 = compute slugging average
lw $a1, 0($s0) # $a1 = walks
lw $a2, 4($s0) # $a2 = singles
lw $a3, 8($s0) # $a3 = doubles
lw $s1, 12($s0) # $s1 = triples
lw $s2, 16($s0) # $s2 = home runs
lw $s3, 20($s0) # $s3 = outs
sw $s3, -4($sp) # put outs at top of average's stack
sw $s2, -8($sp) # put homeruns 2nd fm top of average's stack
sw $s1, -12($sp) # put triples 3rd fm top of average's stack
jal average
  
# Print the on-base percentage
mtc1 $v0, $f12 # get result fm $v0 before we print string
la $a0, mainOnbasePercentage
addi $v0, $zero, 4
syscall
addi $v0, $zero, 2 # print the percentage
syscall
la $a0, mainNewline
addi $v0, $zero, 4
syscall
syscall
addi $s6, $s6, 1 # i++
addi $s0, $s0, 24 # $s0 = addr of next batter's stats
j mainLoopBegin
  
mainDone:
# Epilogue for main -- restore stack & frame pointers and return
lw $ra, 4($sp) # get return address from stack
lw $fp, 0($sp) # restore frame pointer for caller
addiu $sp, $sp, 24 # restore frame pointer for caller
jr $ra # return to caller
.data
printStatsOuts:
.asciiz "Outs: "
printStatsWalks:
.asciiz "Walks: "
printStatsSingles:
.asciiz "Singles: "
printStatsDoubles:
.asciiz "Doubles: "
printStatsTriples:
.asciiz "Triples: "
printStatsHomeruns:
.asciiz "Home runs: "
printStatsNewline:
.asciiz " "
.text
printStats:
# Function prologue
addiu $sp, $sp, -32 # allocate stack space
sw $a3, 20($sp) # save $a0 thru $a3
sw $a2, 16($sp)
sw $a1, 12($sp)
sw $a0, 8($sp)
sw $ra, 4($sp) # save return address
sw $fp, 0($sp) # save frame pointer of caller
addiu $fp, $sp, 28 # setup frame pointer of average
# printStats expects to find the following:
# $a0 = walks
# $a1 = singles
# $a2 = doubles
# $a3 = triples
# 5th argument = homeruns
# 6th argument = outs
  
# print the outs
la $a0, printStatsOuts
addi $v0, $zero, 4
syscall
lw $a0, 0($fp) # the outs are at the top of our stack
addi $v0, $zero, 1
syscall
la $a0, printStatsNewline
addi $v0, $zero, 4
syscall
# print the walks
la $a0, printStatsWalks
addi $v0, $zero, 4
syscall
lw $a0, 8($sp) # the walks were passed in $a0
addi $v0, $zero, 1
syscall
la $a0, printStatsNewline
addi $v0, $zero, 4
syscall
# print the singles
la $a0, printStatsSingles
addi $v0, $zero, 4
syscall
addi $a0, $a1, 0 # the singles were passed in $a1
addi $v0, $zero, 1
syscall
la $a0, printStatsNewline
addi $v0, $zero, 4
syscall
  
# print the doubles
la $a0, printStatsDoubles
addi $v0, $zero, 4
syscall
addi $a0, $a2, 0 # the doubles were passed in $a2
addi $v0, $zero, 1
syscall
la $a0, printStatsNewline
addi $v0, $zero, 4
syscall
  
# print the triples
la $a0, printStatsTriples
addi $v0, $zero, 4
syscall
addi $a0, $a3, 0 # the doubles were passed in $a3
addi $v0, $zero, 1
syscall
la $a0, printStatsNewline
addi $v0, $zero, 4
syscall
  
# print the homeruns
la $a0, printStatsHomeruns
addi $v0, $zero, 4
syscall
lw $a0, -4($fp) # home runs are 4 bytes below top of our stack
addi $v0, $zero, 1
syscall
la $a0, printStatsNewline
addi $v0, $zero, 4
syscall
printStatsDone:
# Epilogue for printStats -- restore stack & frame pointers & return
lw $ra, 4($sp) # get return address from stack
lw $fp, 0($sp) # restore frame pointer for caller
addiu $sp, $sp, 32 # restore frame pointer for caller
jr $ra # return to caller
# Your code goes below this line
.data


.text
average:
# Function prologue
addiu $sp, $sp, -40 # allocate stack space
sw $a3, 20($sp) # save $a0 thru $a3
sw $a2, 16($sp)
sw $a1, 12($sp)
sw $a0, 8($sp)
sw $ra, 4($sp) # save return address
sw $fp, 0($sp) # save frame pointer of caller
addiu $fp, $sp, 36 # setup frame pointer of average

# average expects to find the following:
# $a0 = option
# $a1 = walks
# $a2 = singles
# $a3 = doubles
# 5th argument = triples
# 6th argument = homeruns
# 7th argument = outs

lw $t0,0($fp) #outs - top of stack
lw $t1,-4($fp) # home runs
lw $t2,-8($fp) # triples

#inputs for printstats
move $a0,$a1 # $a0 = walks
move $a1,$a2 # $a1 = singles
move $a2,$a3 # $a2 = doubles
move $a3,$t2 # $a3 = triples
sw $t0, -4($sp) # put outs at top of average's stack 6th argument
sw $t1, -8($sp) # put homeruns 2nd fm top of average's stack 5th argument
jal printStats

lw $a1,16($sp) # singles
lw $a2,20($sp) # doubles
lw $a3,-8($fp) #triples
lw $t1,-4($fp) #homeruns
add $t3,$a1,$a2 # t3 = singles + double
add $t3,$t3,$a3 # t3 = singles + double + triples
add $t3,$t3,$t1 # t3 = singles + double + triples + homeruns = hits
add $t4,$t3,$t0 # t4 = hits + outs = atBats
li $v0,0
beq $t4,$0,averageEnd
lw $t5,8($sp) # option in $t5
addi $t6,$0,1
beq $t5,$t6,averageBatting
addi $t6,$t6,1
beq $t5,$t6,averageSlugging
addi $t6,$t6,1
beq $t5,$t6,averageOnbase

averageEnd:
# Epilogue
lw $a3, 20($sp) # restore $a0 thru $a3
lw $a2, 16($sp)
lw $a1, 12($sp)
lw $a0, 8($sp)
lw $ra, 4($sp) # get return address from stack
lw $fp, 0($sp) # restore frame pointer for caller
addiu $sp, $sp, 40 # restore frame pointer for caller
jr $ra # return to caller

averageBatting:
# hits / atBats
b averageFPdiv   
averageSlugging:
#(singles + doubles*2 + triples*3 + home runs*4) / atBats
mul $a2,$a2,2
mul $a3,$a3,3
mul $t1,$t1,4
add $t3,$a1,$a2 # t3 = singles + double*2
add $t3,$t3,$a3 # t3 = singles + double*2 + triples*3
add $t3,$t3,$t1 # t3 = singles + double*2 + triples*3 + homeruns*4
b averageFPdiv
averageOnbase:
#(hits + walks) / (atBats + walks)
lw $a0,12($sp)
add $t3,$t3,$a0 #(hits + walks)
add $t4,$t4,$a0 #(atBats + walks)
b averageFPdiv

averageFPdiv:
#this routine performs the floating point division of $t3/$t4 and the result is stored in $v0
mtc1 $t4,$f0
cvt.s.w $f0,$f0
mtc1 $t3,$f1
cvt.s.w $f1,$f1
div.s $f2,$f1,$f0
mfc1 $v0,$f2
b averageEnd