Files
cs2810/ArrayReverse.asm
T
2026-08-04 06:59:21 -06:00

70 lines
1.9 KiB
NASM

.data
array: .word 1, 2, 3, 4, 5, 6
orig: .asciiz "Original array: "
rev: .asciiz "\nReverse array: "
.text
main:
jal printOriginalArray
jal printReverseArray
ori $v0, $0, 10 # system call code 10 for exit
syscall # exit the program
reverse:
bge $a0, $a1, done # if the start index is greater or equal to the end index, we are done
lw $t2, array($a0) # load the element at start index
lw $t3, array($a1) # load the element at end index
sw $t3, array($a0) # store the element from the end at the start index
sw $t2, array($a1) # store the element from the start at the end index
addi $a0, $a0, 1 # increment the start index
subi $a1, $a1, 1 # decrement the end index
b reverse # recursively reverse the remaining elements
done:
jr $ra # return to the caller
printOriginalArray:
li $v0, 4
la $a0, orig
syscall
li $s0, 21 # limit of offsets for 6 elements in the array
li $v0, 1 # syscall 1 to print an int
li $t0, 0
origWhile:
bge $t0, $s0 origDone # while the offset is less than 21
lw $a0, array($t0) # load the next word in the array
syscall # print
addi $t0, $t0, 4 # increment the offset by 4
b origWhile # return to start of while loop
origDone:
jr $ra # return to caller
printReverseArray:
li $v0, 4
la $a0, rev
syscall
li $s0, -1 # lower limit of 6 offsets
li $v0, 1 # syscall 1 to print an int
li $t0, 20
revWhile:
bge $s0, $t0 revDone # while the offset is less than 21
lw $a0, array($t0) # load the next word in the array
syscall # print
subi $t0, $t0, 4 # increment the offset by 4
b revWhile # return to start of while loop
revDone:
jr $ra # return to caller