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

MIPS Assembly Language Programming problem: Prompt the user for a series of 10 i

ID: 3564327 • Letter: M

Question

MIPS Assembly Language Programming problem:

Prompt the user for a series of 10 integers and hold them in an integer array. Then, after the entries are
complete, pass through the array finding and printing the largest and smallest entries. Do not track the
largest and smallest values as they are being entered. For example, if the user enters this series (prompt
for these on separate input lines):
12 8 11 32 20 1 29 6 19 12
print this:
Smallest: 1
Largest: 32

Here is the C++ equivalent:

Here is my translation to assembly language:

.text
   .globl   main

main:
   li   $t0, 0       # i = 0
   li   $t1, 10 # Read 10 integers
   la   $t2, array   # $t2 points to address of array

loop:
   la   $a0, prompt
   li   $v0, 4
   syscall

   li   $v0, 4
   li   $v0, 5       # Read an integer
   syscall  

   li   $v0, 1       # Print the number of the input
   move   $a0, $t0   # $a0 = i
   syscall

   li   $v0, 4       # Print a new line
   la   $a0, endl
   syscall

   move   $t3, $v0   # $t3 = $v0 = i integer
   addi   $t4, $t0, 4   # $t4 = offset of array[i]
   add   $t4, $t4, $t2   # $t4 = address of array[i],
               # $t2 = address of array
   sw   $t3, array   # Store i integer in array[i]
   addi   $t0, $t0, 1   # i = i + 1
   blt   $t0, $t1, loop   # If (i < 10) then go to the loop
               # ($t0 = 0, $t1 = 10)
   la   $a0, array   # a0 = address of array
   addi   $a1, $0, 10 # $a1 = n = 10
   lw   $a0, 4($sp)   # Allocate 4 bytes for the stack
   sw   $ra, 0($sp)   # Push return address into the stack

   jal   largesmall   # Call largesmall

largesmall:
   lw   $t0, array   # int smallest = array[0]
   lw   $t1, array   # int largest = array[0]
   addi   $t2, $0, 1   # int i = 1

loop2:
   bge   $t2, $a1, end   # While (i < n)
   lw   $t4, array   # $t4 = array[i]
   bge   $t4, $t0, large   # If (smallest > $t4)
   move   $t0, $t4   # smallest = $t4

large:
   ble   $t4, $t1, next   # If (largest < $t4)
   move   $t1, $t4   # largest = $t4

next:
   addi   $t2, $t2, 1   # i = i + 1
   j loop2           # Go back to loop2

end:
   la   $a0, smallest
   li   $v0, 5
   syscall

   li   $v0, 1       # Print the smallest value of the array
   move   $a0, $t0   # $a0 = $t0 = smallest
   syscall
   li   $v0, 4       # Print a new line
   la   $a0, endl
   syscall
   la   $a0, largest
   li   $v0, 5
   syscall
   li   $v0, 1       # Print the largest value of the array
   move   $a0, $t1   # $a0 = $t1 = largest
   syscall
   move   $v0, $t0   # v0 = smallest($t0)
   move   $v1, $t1   # v1 = largest($t1)
   li   $v0, 4       # Print a new line
   la   $a0, endl
   syscall
   li   $v0, 10
   syscall

.data
array: .space 40
prompt: .asciiz "Enter a number: "

largest: .asciiz "Smallest: "
smallest: .asciiz "Largest: "
endl: .asciiz " "

I am not getting the output for the smallest and largest number. Can someone post the correct Assembly Language code for this problem?

Explanation / Answer

.text
.globl main

main:
li $t0, 0 # i = 0
li $t1, 10 # Read 10 integers
la $t2, array # $t2 points to address of array

loop:
la $a0, prompt
li $v0, 4
syscall

li $v0, 5 # Read an integer
syscall

add $t4,$t0,$t0
add $t4,$t4,$t4
add $t4,$t4,$t2 # address of array[i] = address of array[0] + 4 * i because each integer is 4 byte
sw $v0, 0($t4) # Store i integer in array[i]
addi $t0, $t0, 1 # i = i + 1
blt $t0, $t1, loop # If (i < 10) then go to the loop
# ($t0 = 0, $t1 = 10)
la $a0, array # a0 = address of array
addi $a1, $0, 9 # $a1 = n = 9
#$t0 has min, $t1 has max
#initialise $t0 & $t1 with array[0]
lw $t0,0($a0)
lw $t1,0($a0)
addi $a0,$a0,4 # move to array[1]
  
loop1:   
lw $t2,0($a0)
blt $t2,$t0,changemin
returnmin:
bgt $t2,$t1,changemax
returnmax:
subi $a1,$a1,1
addi $a0,$a0,4 # move to array[1]
bne $a1,$0,loop1
  
la $a0, smallest
li $v0, 4
syscall

li $v0, 1 # Read an integer
move $a0,$t0
syscall

la $a0, largest
li $v0, 4
syscall

li $v0, 1 # Read an integer
move $a0,$t1
syscall

#exit
li $v0,10
syscall

changemin:
move $t0,$t2
b returnmin

changemax:
move $t1,$t2
b returnmax
.data
array: .space 40
prompt: .asciiz "Enter a number: "

smallest: .asciiz "Smallest: "
largest: .asciiz " Largest: "
endl: .asciiz " "