I am trying to complete an assignment using stacks and I need a little guidance. My task is to make an array of 6 values and pass it to VIA stack to a subroutine where I need to increment through it and pass each value of the array to another subroutine VIA the stack. From that last subroutine I am to add 8 to the value and pass it back to the previous subroutine to continue to go through the loop until there are no more values. After there are no more values to increment through I am supposed to send back VIA the stack to the main routine and sum the values from the array. I am a bit lost, see my code below:
First File (main file)
.text
.global _start
.extern _incArr
_start:
movia sp, 0x007FFFFC
movia r2, ARR
addi sp, sp, -4
stw r2, 0(sp)
call _incArr
ldw r2, 0(sp)
addi r1, r0, 0
addi r3, r0, 0
addi r4, r0, 5
addi r6, r0, 0
addi sp, sp, 4
LOOP:
bgt r6, r4, END
ldw r5, 0(r2)
add r1, r1, r5
addi r2, r2, 4
addi r6, r6, 1
br LOOP
END:
br END
.data
ARR: .word 1, 2, 3, 4, 5, 6
.end
First Sub routine
.text
.global _incArr
.extern _increment
_incArr:
addi sp, sp, -16
stw ra, 12(sp)
stw r2, 0(sp)
stw r3, 4(sp)
stw r4, 8(sp)
ldw r2, 16(sp)
addi r4, r0, 224
LOOP:
ldw r3, 0(r2)
stw r3, 0(sp)
call _increment
ldw r3, 0(sp)
stw r3, 0(r2)
addi r2, r2, 4
blt r2, r4, LOOP
stw r3, 16(sp)
ldw r2, 0(sp)
ldw r3, 4(sp)
ldw r4, 8(sp)
ldw ra, 12(sp)
addi sp,sp,12
ret
Second Subroutine:
.text
.global _increment
_increment:
addi sp, sp, -8
stw ra, 4(sp)
stw r2, 0(sp)
ldw r2, 8(sp)
addi r2, r2, 8
stw r2, 8(sp)
ldw r2, 0(sp)
ldw ra, 4(sp)
addi sp, sp, 8
ret
I THING my problem is passing back the address of the array and storing it from the first subroutine to the main function. The reason I believe this to be my problem is because if I load the value of r2
(what is supposed to be the array) I will get garbage values when I use ldw r5, 0(r2)
. I don't know if I am freeing the sp
too early or not passing the address back correctly. I have tried for quite a few hours messing with all sorts of things trying to figure it out by changing to reading from sp (ldw r5, 0(sp)
) and many other things. It LOOKS like my first subroutine passing to the second subroutine and back is working, it does pass the right integer value, add 8 and pass back but I am not sure if storing it after that point is correct. My storing "function" is below:
stw r3, 16(sp)
ldw r2, 0(sp)
ldw r3, 4(sp)
ldw r4, 8(sp)
ldw ra, 12(sp)
addi sp,sp,12
If you could be of some help I would appropriate it. I really want to understand this because it is a very important part of learning assembly. Thank you!