#********************************************************************** # CSSE 232: Computer Architecture I # # File: p7.asm # Written by: J.P. Mellor, 6 Sep. 2004 # # This file contains a MIPS assembly language program that uses only the # instructions introduced in p1.asm, p2.asm, p3.asm, p4.asm and p5.asm. # # It also takes advantage of several spim syscalls and the assembly # directive .asciiz # #********************************************************************** .globl main .globl V .globl N .data V: .word 20, 56, -90, 37, -2, 30, 10, -66, -4, 18 N: .word 10 message1: .asciiz "The input array:\n" message2: .asciiz "The sorted array:\n" sep: .asciiz " " newline: .asciiz "\n" #********************************************************************** .text main: sub $sp, $sp, 8 # Create a 2-word frame. sw $ra, 4($sp) # Save $ra #---------------------------------------------------------------------- # Print the unsorted array #---------------------------------------------------------------------- li $a0, 10 # pass the length of V la $a1, V # pass address of V la $a2, message1 # pass address of message1 jal print # call print #---------------------------------------------------------------------- # Sort the array by repeatedly calling SwapMaxWithLast #---------------------------------------------------------------------- # # Insert your code here # #---------------------------------------------------------------------- # Print the sorted array #---------------------------------------------------------------------- li $a0, 10 # pass the length of V la $a1, V # pass address of V la $a2, message2 # pass address of message2 jal print # call print # --------------------------------------------------------------------- # Exit the main procedure. # --------------------------------------------------------------------- ExitMain: lw $ra, 4($sp) # Restore $ra add $sp, $sp, 8 # Undo the 2-word frame. jr $ra # Return .globl print # --------------------------------------------------------------------- # Procedure: print # # No frame, none is needed. # # Arguments: # $a0 = number of elements in V # $a1 = pointer to V # $a2 = pointer to message # # Returns: # none # # Register allocations: # none: # # Prints the array. # --------------------------------------------------------------------- print: #---------------------------------------------------------------------- # Save arguments #---------------------------------------------------------------------- move $t0, $a1 # initialize the ptr (start of array) sll $t1, $a0, 2 # index*4 add $t1, $t1, $t0 # ptr + index*4 = end address #---------------------------------------------------------------------- # Print prompt #---------------------------------------------------------------------- move $a0, $a2 # store pointer to message li $v0, 4 # use system call to syscall # print message #---------------------------------------------------------------------- # Print array #---------------------------------------------------------------------- loop2: lw $a0, 0($t0) # store next element li $v0, 1 # use system call to syscall # print the min la $a0, sep # store pointer to sep li $v0, 4 # use system call to syscall # print sep add $t0, $t0, 4 # increment the index bne $t0, $t1, loop2 # check if we've printed all the elements #---------------------------------------------------------------------- # Print final return #---------------------------------------------------------------------- la $a0, newline # store pointer to a new line li $v0, 4 # use system call to syscall # print new line # --------------------------------------------------------------------- # Exit to the main procedure. # --------------------------------------------------------------------- jr $ra # Return .globl SwapMaxWithLast # --------------------------------------------------------------------- # Procedure: SwapMaxWithLast # # No frame, none is needed. # # Arguments: # $a0 = address of the array # $a1 = number of elements in the array # # Returns: # none # # Register allocations: # none: # # Swaps the maximum element with the last element of the array. # --------------------------------------------------------------------- SwapMaxWithLast: # # Insert your code here #