#********************************************************************** # CS 232: Comp. Arch. I # Written: 12/6/99, JP Mellor # # This file contains procedure: # -- main # # The program rotates an in memory array # one position to the right. The last # element becomes the first. # #********************************************************************** .globl main .data V: .word 20, 56, -90, 37, -2, 30, 10, -66, -4, 18 message: .asciiz "Rotated array:\n" sep: .asciiz " " newline: .asciiz "\n" #********************************************************************** .text main: #---------------------------------------------------------------------- # Rotate the array #---------------------------------------------------------------------- li $t1, 10 # load end - the number of elements in V sll $t1, $t1, 2 # multiply end by 4 to get word addresses 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 skip: 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 #---------------------------------------------------------------------- # 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 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 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