#********************************************************************** # CS 232: Comp. Arch. I # Written: 12/6/99, JP Mellor # # This file contains procedure: # -- main # # The program dynamically allocates and fills an array, # prints the input array, prompts the user for an integer i, # rotates the array i positions, and then prints the rotated array. # #********************************************************************** .globl main .globl rpt .data prompt: .asciiz "How many elements (1-20)? => " prompt2: .asciiz "element: " prompt3: .asciiz "Input amount to rotate: " message: .asciiz "Input array:\n" message2: .asciiz "Rotated array:\n" 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 $t4 #---------------------------------------------------------------------- # 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 #---------------------------------------------------------------------- move $t0, $t4 # initialize index read: 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 bne $t0, $t1, done # 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 #---------------------------------------------------------------------- # Read amount to rotate #---------------------------------------------------------------------- la $a0, prompt3 # load address of prompt3 li $v0, 4 # use system call to syscall # print prompt3 li $v0, 5 # use system call for reading syscall # an integer i move $t5, $v0 # Copy integer i into $t4 #---------------------------------------------------------------------- # Determine how much to rotate the array #---------------------------------------------------------------------- sub $t0, $t1, $t4 # number of bytes in array srl $t0, $t0, 2 # divide by four to get number of elements div $t5, $t0 # calculate rotate / # mfhi $t5 # get rotate mod # #---------------------------------------------------------------------- # Rotate the array #---------------------------------------------------------------------- rpt: move $t0, $t4 # initialize index lw $t2, 0($t0) # initialize temp1 with the first element add $t0, $t0, 4 # increment the index beq $t0, $t1, fin # check if we've processed the last element loop: lw $t3, 0($t0) # read the next element of the array into temp2 sw $t2, 0($t0) # replace its value with temp1 move $t2, $t3 # move temp2 to temp1 add $t0, $t0, 4 # increment the index bne $t0, $t1, loop # check if we've processed the last element fin: move $t0, $t4 # reset index sw $t2, 0($t0) # store the new first element add $t5, $t5, -1 # decrement the rotation counter bne $t5, $zero, rpt # check if we've rotated enough #---------------------------------------------------------------------- # Print the rotated array #---------------------------------------------------------------------- la $a0, message2 # store pointer to message2 li $v0, 4 # use system call to syscall # print message2 move $t0, $t4 # initialize the index loop2: lw $a0, 0($t0) # store next element li $v0, 1 # use system call to syscall # print the 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, loop2 # 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 # -------------------------------------------------------------------- # 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