93.3k views
4 votes
Write a program that stores the maximum of three values. The values are stored in \$ s 0 , \$s1, and \$s2. Store the result in \$s3 Ex: if the values of \$ s 0, \$ s 1 and \$ s 2 are

User Ofer Zelig
by
7.4k points

1 Answer

2 votes

This program loads the values from $50, $s1, and $s2 into temporary registers, compares them to find the maximum, and stores the maximum value in $$3. You can run this program in a MIPS simulator or assembler.

Here's a MIPS assembly program to achieve this:

assembly

.data

.text

main:

# Load values from $50, $s1, and $s2 into temporary registers

lw $t0, 0($s0) # Load $50 into $t0

lw $t1, 0($s1) # Load $s1 into $t1

lw $t2, 0($s2) # Load $s2 into $t2

# Find the maximum value among $t0, $t1, and $t2

move $$3, $t0 # Assume $t0 is the maximum initially

# Compare $t1 with the current maximum in $$3

bgt $t1, $$3, set_max_t1

# Compare $t2 with the current maximum in $$3

bgt $t2, $$3, set_max_t2

# If no updates needed, store the maximum value in $$3

j store_max

set_max_t1:

# Set $$3 to $t1 as it's the new maximum

move $$3, $t1

j store_max

set_max_t2:

# Set $$3 to $t2 as it's the new maximum

move $$3, $t2

store_max:

# The maximum value is stored in $$3 now

# Exit the program (optional)

li $v0, 10

Write a program that stores the maximum of three values. The values are stored in-example-1
User Ahmad Nadeem
by
8.8k points