This is a simple MIPS assembly program that using a loop asks the user to enter positive integer numbers. The loop will exit once a negative integer is entered. After that, the program will output the minimum value, the maximum value and the average of all numbers.

MIPS (originally an acronym for Microprocessor without Interlocked Pipeline Stages) is a reduced instruction set computer (RISC) instruction set (ISA) developed by MIPS Technologies (formerly MIPS Computer Systems, Inc.).

.data
txt1:   .asciiz   "Please enter apositive integer (negative integer to exit) : "
txt2:   .asciiz   "\nThe maximum value is : "
txt3:   .asciiz   "\nThe minimum value is : "
txt4:   .asciiz   "\nThe average is : "
txt5:   .asciiz   " with remainder : "

.text
.globl main
main:
    addi $s0, $zero, 0
loop:
    la $a0, txt1
    li $v0, 4
    syscall
    
    li $v0, 5
    syscall
    
    bltz $v0, average
    add  $s0, $v0, 0
    add  $t0, $t0, 1
    add  $t3, $t3, $s0
    
    beq  $t0, 1, first 
    blt  $s0, $t1, min
    bgt  $s0, $t2, max
first:
     add $t1, $s0, 0
     add $t2, $s0, 0
     j   loop
min: 
    add $t1, $s0, 0
    j   loop
max:
    add $t2, $s0, 0
    j   loop    

average:
    div  $t3, $t0
    mflo $t7
    mfhi $t6
    
print:
    la $a0, txt2
    li $v0, 4
    syscall
    
    add $a0, $t2, 0
    li  $v0, 1
    syscall
    
    la $a0, txt3
    li $v0, 4
    syscall
    
    add $a0, $t1, 0
    li $v0, 1
    syscall
    
    la $a0, txt4
    li $v0, 4
    syscall
    
    add $a0, $t7, 0
    li $v0, 1
    syscall
             
 exit:
    li $v0, 10 
    syscall
Tagged with: MIPS AssemblySource Code
 

One Response to Finding Minimum, Maximum and Average using MIPS Assembly

  1. Sam W says:

    You’re missing a line at the end of the loop

    .data
    txt1: .asciiz “Please enter apositive integer (negative integer to exit) : ”
    txt2: .asciiz “\nThe maximum value is : ”
    txt3: .asciiz “\nThe minimum value is : ”
    txt4: .asciiz “\nThe average is : ”
    txt5: .asciiz ” with remainder : ”

    .text
    .globl main
    main:
    addi $s0, $zero, 0
    loop:
    la $a0, txt1
    li $v0, 4
    syscall #prints out txt1

    li $v0, 5
    syscall #takes in an int

    bltz $v0, average #if the int is negative branch
    add $s0, $v0, 0 #otherwise, put $v0 in $s0
    add $t0, $t0, 1 #increment $t0
    add $t3, $t3, $s0 #add $s0 to $t3

    beq $t0, 1, first #if $t0 is 1, meaning its the first loop, branch to first
    blt $s0, $t1, min #if $s0 is less than $t1, branch to min
    bgt $s0, $t2, max #if $s0 is greater than $t2, branch to max
    j loop #they were missing a loop here, otherwise if its not min, max, or first it would fall through
    first:
    add $t1, $s0, 0 #put the int in min and max
    add $t2, $s0, 0
    j loop
    min:
    add $t1, $s0, 0 #put $s0 in $t1
    j loop
    max:
    add $t2, $s0, 0 #put $s0 in $t2
    j loop

    average:
    div $t3, $t0 #divide the total $t3 by the number of ints $t0
    mflo $t7 #store the quotient in $t7
    mfhi $t6 #store the remainder in $t6

    print: #prints the results
    la $a0, txt2
    li $v0, 4
    syscall

    add $a0, $t2, 0
    li $v0, 1
    syscall

    la $a0, txt3
    li $v0, 4
    syscall

    add $a0, $t1, 0
    li $v0, 1
    syscall

    la $a0, txt4
    li $v0, 4
    syscall

    add $a0, $t7, 0
    li $v0, 1
    syscall

    la $a0, txt5
    li $v0, 4
    syscall

    add $a0, $t6, 0
    li $v0, 1
    syscall

    exit:
    li $v0, 10
    syscall

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.