I have the current code of : .dataI Weights: .word 0:5 Prompt: .asciiz \"Enter t
ID: 3565129 • Letter: I
Question
I have the current code of :
.dataI
Weights: .word 0:5
Prompt: .asciiz "Enter the weight of Player "
Winner: .asciiz "Winning Player: "
WinWeight: .asciiz " Winning Weight: "
endl: .asciiz " "
.text
# Take Input
li $t0, 5
la $t1, Weights
Input_Loop:
bge $t2, $t0, Input_Exit
# Prompt Syscall
li $v0, 4
la $a0, Prompt
syscall
# Print Player Number
li $v0, 1
move $a0, $t2
syscall
# Print Endline
li $v0, 4
la $a0, endl
syscall
# Take input
li $v0, 5
syscall
# Save Input
sll $t3, $t2, 2 # Mult Index by 4
add $t3, $t3, $t1 # Calc Word Address
sw $v0, 0($t3)
addiu $t2, $t2, 1 # Increment Counter
j Input_Loop
Input_Exit:
lw $t4, 0($t1) # Init Max Weight
move $t6, $zero # Init Winning Player
# Loop over weights 1 through 4, comparing.
move $t2, $zero # Init Counter = 0
Max_Loop:
bge $t2, $t0, Max_Exit
# Retrieve Weight
sll $t3, $t2, 2 # Mult Index by 4
add $t3, $t3, $t1 # Calc Word Address
lw $t5, 0($t3)
# Compare and Assign Max
ble $t5, $t4, Next
move $t4, $t5 # Set Winning Weight
move $t6, $t2 # Set Winning Player
Next: addiu $t2, $t2, 1 # Increment Counter
j Max_Loop
Max_Exit:
# Print Winning Weight and Player
# Print Winning Player
li $v0, 4
la $a0, Winner
syscall
li $v0, 1
move $a0, $t4
syscall
li $v0, 4
la $a0, endl
syscall
# Print Winning Weight
li $v0, 4
la $a0, WinWeight
syscall
li $v0, 1
move $a0, $t6
syscall
li $v0, 4
la $a0, endl
syscall
and I need to implement the following psudocode in the current code:
if (W[i] > Max)
{
Max = W[i];
Winner = "Player " + Convert.ToString(i);
}
Explanation / Answer
.data
Weights: .word 0,0,0,0,0
Prompt: .asciiz "Enter the weight of Player "
Winner: .asciiz "Winning Player: "
WinWeight: .asciiz "Winning Weight: "
endl: .asciiz " "
.text
# Take Input
li $t0, 5
la $t1, Weights
Input_Loop:
bge $t2, $t0, Input_Exit
# Prompt Syscall
li $v0, 4
la $a0, Prompt
syscall
# Print Player Number
li $v0, 1
move $a0, $t2
syscall
# Print Endline
li $v0, 4
la $a0, endl
syscall
# Take input
li $v0, 5
syscall
# Save Input
sll $t3, $t2, 2 # Mult Index by 4
add $t3, $t3, $t1 # Calc Word Address
sw $v0, 0($t3)
addiu $t2, $t2, 1 # Increment Counter
j Input_Loop
Input_Exit:
lw $t4, 0($t1) # Init Max Weight
move $t6, $zero # Init Winning Player
# Loop over weights 1 through 4, comparing.
move $t2, $zero # Init Counter = 0
Max_Loop:
bge $t2, $t0, Max_Exit
# Retrieve Weight
sll $t3, $t2, 2 # Mult Index by 4
add $t3, $t3, $t1 # Calc Word Address
lw $t5, 0($t3)
# Compare and Assign Max
ble $t5, $t4, Next
move $t4, $t5 # Set Winning Weight
move $t6, $t2 # Set Winning Player
Next: addiu $t2, $t2, 1 # Increment Counter
j Max_Loop
Max_Exit:
# Print Winning Weight and Player
# Print Winning Player
li $v0, 4
la $a0, Winner
syscall
li $v0, 1
move $a0, $t6
syscall
li $v0, 4
la $a0, endl
syscall
# Print Winning Weight
li $v0, 4
la $a0, WinWeight
syscall
li $v0, 1
move $a0, $t4
syscall
li $v0, 4
la $a0, endl
syscall
the psuedo code is already implemented. the printing not properly done....i have corrected that.