1. Engineering
  2. Computer Science
  3. 1 modify largers to let the user to enter three...

Question: 1 modify largers to let the user to enter three...

Question details

1. Modify larger.s to let the user to enter three numbers and output the largest value

# larger.s-- prints the larger of two numbers specified
# at runtime by the user.
# Registers used:
# $t0 - used to hold the first number.
# $t1 - used to hold the second number.
# $t2 - used to store the larger of $t1 and $t2.
# $v0 - syscall parameter and return value.
# $a0 - syscall parameter.
.text

main:
## Get first number from user, put into $t0.
li $v0, 5            # load syscall read_int into $v0.
syscall            # make the syscall.
add $t0, $v0, $zero       # move the number read into $t0.

## Get second number from user, put into $t1.

li $v0, 5            # load syscall read_int into $v0.
syscall            # make the syscall.
add $t1, $v0, $zero       # move the number read into $t1.

## put the larger of $t0 and $t1 into $t2.

slt $t2, $t0, $t1      # If $t0 < $t1, $t2 is 1,
bne $t2, $zero,First       # if $t2 is 1, go to First
add $t2, $t0, $zero
j Print               # copy $t0 into $t2
First:
add $t2, $t1, $zero

## Print out $t2.

Print:
add $a0, $t2, $zero       # move the number to print into $a0.
li $v0, 1            # load syscall print_int into $v0.
syscall            # make the syscall.

## exit the program.

li $v0, 10            # syscall code 10 is for exit.
syscall            # make the syscall.

# end of larger.s

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution