# Author: Heidi Ellis # Date: 09/13/05 # Given an array of integers, this program # prompts the user for an integer and prints # all numbers greater than or equal to the input # number in correct order and the numbers lower # than the input number in reverse order. # Note: Try entering 2147483645! .data # Define array size in memory. size: .word 4 # Define array in memory. arrayA: .word 12 .word 15 .word 23 .word 45 # Data for prompts and user feedback. newln: .asciiz "\n" # Newline string prompt: .asciiz "Enter an integer: " larger: .asciiz "Numbers larger than input: \n" smaller: .asciiz "Numbers smaller than input: \n" goodbye: .asciiz "Good-bye! \n" .text main: la $t1, arrayA # Put start address of array into $t1 # $t1 will hold array index for loop lw $s0, size # Put size in $s0 li $s1, 1 # Set $s1 as loop counter li $v0, 4 # Prompt user to enter integer la $a0, prompt syscall li $v0, 5 # Read in integer syscall move $s2, $v0 # Store in $s2 li $v0, 4 # Print "larger" heading la $a0, larger syscall # Beginning of loop to print if number # is greater than the input integer. aloop: bgt $s1, $s0, next # $s0 is loop bound, $s1 is counter lw $s3, 0($t1) # Retrieve array element blt $s3, $s2, inc # Branch to loop end if not greater than li $v0, 1 # Else print array element move $a0, $s3 syscall li $v0, 4 # Print newline character la $a0, newln syscall inc: addi $t1, 4 # Increment address counter by 4! addi $s1, 1 # Increment loop counter j aloop # Jump to check if at end of loop # Loop to print numbers less than input number in reverse order next: li $v0, 4 # Print "smaller" heading la $a0, smaller syscall # $t1 holds last address in array loop2: blt $s0, 0, theend # $s0 size, decremented to zero lw $s3, 0($t1) # Retrieve array element blt $s2, $s3, inc2 # Branch to loop end if not greater than # I.e., don't print. li $v0, 1 # Else print array element move $a0, $s3 syscall li $v0, 4 # Print newline character la $a0, newln syscall # On first time through the loop, $t1 contains # end address of array. Therefore we'll decrement our # way back up through the array. inc2: addi $t1, -4 # Decrement address counter by 4! addi $s0, -1 # Decrement loop counter j loop2 # Jump to check end of second loop theend: li $v0, 4 # Print good bye! la $a0, goodbye syscall li $v0, 10 # Appropriate way to end assembly code syscall