This is a MIPS assembly program that finds the maximum number in an array of 8 non-negative integers. MIPS is a register based architecture, meaning the CPU uses registers to perform operations on. The MIPS architecture is a Reduced Instruction Set Computer (RISC). This program uses a function call. All you need to know is that “jal maximum” is the function call. It jumps to the code at label “maximum”. Then, “jr $ra” jumps back to the instruction just after the function call (in this case, the “j finish”). This program displays the result 10 in this case. Because array in data section (at the bottom of this program) contains the maximum value 10.

# Finds the maximum number in an array of 8 non-negative 1-word integers and stores it in $a2
# index = 0
# maxSoFar = 0
# while not (index == number of elements in the array):
# if array1[index] > maxSoFar:
# maxSoFar = array1[index]
# index = index + 1
 la $a0, array1 # $a0 pointer to the array
 addi $a1, $zero, 8 # $a1 = number of elements in the array
 jal maximum # "maximum" function call
 j finish

## maximum function. The arguments are passed in $a0 and $a1
# $a0 contains a pointer to the array (the address of the first element)
# $a1 contains the number of elements in the array
# Remember, each element is 4 bytes long, so the addresses of the
# array elements are 4 bytes apart

 addi $t7, $zero, 0 # $t7 index = 0
 addi $t2, $zero, 0 # $t2 maxSoFar = 0 
 # if index == number of elements in the array, exit loop 
 beq $t7, $a1, maximum_finish 
 lw $t3, 0($a0) # $t3 = array1[index]
 slt $t1, $t3, $t2 # if array1[index] < maxSoFar then $t1 = 1
 # o/w, $t1 = 0 
 bne $t1, $zero, notmore # if $t1 is not equal to $zero, then jump to notmore
 # o/w ($t1 is 0), stay here
 # Basically, you stay here if the
 # above test was false ---
 # array1[index] >= maxSoFar. So, we
 # update maxSoFar
 addi $t2, $t3, 0 # maxSoFar = array1[index]
 addi $t7, $t7, 1 # index = index + 1
 addi $a0,$a0, 4 # point to the next element of the array
 j maximum_loop
 jr $ra 

 addi $a2, $t2, 0 # $a2 now has maximum element
 move $a0, $a2
 li $v0, 1
# Now we'll set up the array
# .data tells the assembler we are storing things in the data segment
# of memory. As we know, that will store them starting at 0x10010000.
# But because we use the label "array1", we don't have to use the 
# actual number in our assembly code.
 .word 3,5,7,9,10,8,6,4
Tagged with: MIPS AssemblyProgrammingSource Code

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.