#********************************************************************** # CS 232: Comp. Arch. I # Written: 12/6/99, JP Mellor # # This file contains procedure: # -- main # # The program dynamically allocates and fills an array and # then prints the array and the minimum element. # #********************************************************************** .globl main .data prompt: .asciiz "How many elements (1-20)? => " prompt2: .asciiz "element: " message: .asciiz "Input array:\n" message2: .asciiz "Minimum element -> " sep: .asciiz " " error: .asciiz "Illegal number of elements!\n" newline: .asciiz "\n" #********************************************************************** .text main: #---------------------------------------------------------------------- # Read the number of elements #---------------------------------------------------------------------- la $a0, prompt # load address of prompt li $v0, 4 # use system call to syscall # print prompt li $v0, 5 # use system call for reading syscall # an integer i move $t1, $v0 # Copy integer i into $t1 #---------------------------------------------------------------------- # Check if the number of elements is in bounds #---------------------------------------------------------------------- slt $t0, $t1, 1 # if # lt 1 bne $t0, $zero, exit# exit slt $t0, $t1, 21 # if # gt 20 beq $t0, $zero, exit# exit sll $t1, $t1, 2 # multiply # by to get total number of bytes #---------------------------------------------------------------------- # Allocate space for the array #---------------------------------------------------------------------- move $a0, $t1 # store number of bytes needed li $v0, 9 # use system call for syscall # allocating memory move $t4, $v0 # base address of array add $t1, $t1, $t4 # end of array #---------------------------------------------------------------------- # Read the elements and find the minimum element #---------------------------------------------------------------------- move $t0, $t4 # initialize index la $a0, prompt2 # load address of prompt2 li $v0, 4 # use system call to syscall # print prompt2 li $v0, 5 # use system call for reading syscall # an integer i move $t2, $v0 # initialize min sw $t2, 0($t0) # store first element add $t0, $t0, 4 # increment index beq $t0, $t1, done # check if we've processed the last element loop: la $a0, prompt2 # load address of prompt2 li $v0, 4 # use system call to syscall # print prompt2 li $v0, 5 # use system call for reading syscall # an integer i move $t3, $v0 # move new element sw $t3, 0($t0) # store next element slt $t5, $t3, $t2 # check if it's less than the min beq $t5, $zero, skip# if not skip move $t2, $t3 # if it is update min skip: add $t0, $t0, 4 # increment the index bne $t0, $t1, loop # check if we've processed the last element #---------------------------------------------------------------------- # Print the array #---------------------------------------------------------------------- done: la $a0, message # store pointer to message li $v0, 4 # use system call to syscall # print message move $t0, $t4 # initialize index write: lw $a0, 0($t0) # store next element li $v0, 1 # use system call to syscall # print the next element la $a0, sep # store pointer to sep li $v0, 4 # use system call to syscall # print sep add $t0, $t0, 4 # increment the inex bne $t0, $t1, write # check if we've printed all the elements la $a0, newline # store pointer to a new line li $v0, 4 # use system call to syscall # print new line #---------------------------------------------------------------------- # Print the answer #---------------------------------------------------------------------- la $a0, message2 # store pointer to message2 li $v0, 4 # use system call to syscall # print message2 move $a0, $t2 # store min li $v0, 1 # use system call to syscall # print the min la $a0, newline # store pointer to a new line li $v0, 4 # use system call to syscall # print new line # --------------------------------------------------------------------- # Exit # --------------------------------------------------------------------- li $v0, 10 # use system call to syscall # exit # --------------------------------------------------------------------- # Error # --------------------------------------------------------------------- exit: la $a0, error # store pointer to error li $v0, 4 # use system call to syscall # print error li $v0, 10 # use system call to syscall # exit