So i\'ve written the MIPS assembly code below which takes in the weight of 4 pla
ID: 3629539 • Letter: S
Question
So i've written the MIPS assembly code below which takes in the weight of 4 players of a game and then decides who the winner is based on who's weight is the largest. The only problem is I need to use a procedure method that should look something like this:main: ...
... jal cp # procedure call
...
cp: ... jr $ra # return to the caller
Here's the code ive written:
.data
# Create null terminated strings to use
strPromptFirst: .asciiz "Enter the weight of player 0: "
strPromptSecond: .asciiz "Enter the weight of player 1: "
strPromptThird: .asciiz "Enter the weight of player 2: "
strPromptFourth: .asciiz "Enter the weight of player 3: "
strResult0: .asciiz "Player 0 wins with weight = "
strResult1: .asciiz "Player 1 wins with weight = "
strResult2: .asciiz "Player 2 wins with weight = "
strResult3: .asciiz "Player 3 wins with weight = "
##########################################
# The text segment -- instructions go here
##########################################
.text
.globl main
main:
################# STEP 1 -- get the first weight #####################
# Print a prompt asking user for input
li $v0, 4 # syscall number 4 will print string whose address is in $a0
la $a0, strPromptFirst # "load address" of the string
syscall # actually print the string
# Now read in the first weight
li $v0, 5 # syscall number 5 will read an int
syscall # actually read the int
move $s0, $v0 # save result in $s0 for later
################ STEP 2 -- get the second weight #####################
li $v0, 4
la $a0, strPromptSecond
syscall
# read in the second weight
li $v0, 5
syscall
move $s1, $v0
################ STEP 3 -- get the third weight #####################
li $v0, 4
la $a0, strPromptThird
syscall
# read in the third weight
li $v0, 5
syscall
move $s2, $v0
################ STEP 4 -- get the fourth weight ####################
li $v0, 4
la $a0, strPromptFourth
syscall
# read in the fourth weight
li $v0, 5
syscall
move $s3, $v0
bge $s0,$s1,label1 #if player0>player1 go to label1
b label4
label1:
bge $s0,$s2,label2 #if player0>player2 go to label2
b label3
label2:
bge $s0,$s3,label00 #if player0>Player3 go to label00, if not player 3 wins
li $v0, 4
la $a0, strResult3
syscall
li $v0,1
move $a0, $s3
syscall #result is printed
li $v0,10
syscall #exit
label3:
bge $s2, $s3, label02 #if player2>3 then go to label02, if not player 3 wins
li $v0, 4
la $a0, strResult3
syscall
li $v0,1
move $a0, $s3
syscall #result is printed
li $v0,10
syscall #exit
label4:
bge $s1, $s2, label5 #if player1>player2 go to label 5
b label6
label5:
bge $s1, $s3, label01 #if player2>player3 go to label01, if not player3 wins
li $v0, 4
la $a0, strResult3
syscall
li $v0,1
move $a0, $s3
syscall #result is printed
li $v0,10
syscall #exit
label6:
bge $s2, $s3, label02 #if player2>player3 go to label02, if not player 3 win
li $v0, 4
la $a0, strResult3
syscall
li $v0,1
move $a0, $s3
syscall #result is printed
li $v0,10
syscall #exit
label00:
#Player 0 is the winner
li $v0, 4
la $a0, strResult0
syscall
li $v0,1
move $a0, $s0
syscall #result is printed
li $v0,10
syscall #exit
label01:
#Player 1 is the winner
li $v0, 4
la $a0, strResult1
syscall
li $v0,1
move $a0, $s1
syscall #result is printed
li $v0,10
syscall #exit
label02:
#Player 2 is the winner
li $v0, 4
la $a0, strResult2
syscall
li $v0,1
move $a0, $s2 #
syscall #result is printed
li $v0,10
syscall #exit
Explanation / Answer
wow, this is so complicated!
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.