#********************************************************************** # CS 232: Comp. Arch. I # Written: 12/6/99, JP Mellor # # This file contains procedure: # -- main # # The program prompts the user for an integer i # and then rotates an in memory array i positions # to the right (with wrap around). # #********************************************************************** .globl main .data V: .word 20, 56, -90, 37, -2, 30, 10, -66, -4, 18 prompt: .asciiz "Input amount to rotate: " message: .asciiz "Rotated array:\n" sep: .asciiz " " newline: .asciiz "\n" #********************************************************************** .text main: #---------------------------------------------------------------------- # Read amount to rotate #---------------------------------------------------------------------- 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 $t4, $v0 # Copy integer i into $t4 #---------------------------------------------------------------------- # Determine how much to rotate the array #---------------------------------------------------------------------- li $t1, 10 # load end - the number of elements in V div $t4, $t1 # calculate rotate / # mfhi $t4 # get rotate mod # #---------------------------------------------------------------------- # Rotate the array #---------------------------------------------------------------------- sll $t1, $t1, 2 # multiply end by 4 to get word addresses rpt: li $t0, 0 # initialize index lw $t2, V($t0) # initialize temp1 with the first element add $t0, $t0, 4 # increment the index beq $t0, $t1, exit # check if we've processed the last element loop: lw $t3, V($t0) # read the next element of the array into temp2 sw $t2, V($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 exit: li $t0, 0 # reset index sw $t2, V($t0) # store the new first element add $t4, $t4, -1 # decrement the rotation counter bne $t4, $zero, rpt # check if we've rotated enough #---------------------------------------------------------------------- # Print the rotated array #---------------------------------------------------------------------- la $a0, message # store pointer to message li $v0, 4 # use system call to syscall # print message li $t0, 0 # initialize the index loop2: lw $a0, V($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