So I'm writing a program is assembly , and I'm trying to use sub-routines, but I have a problem. I'v written a routine that resembles a switch case . It reads an input , and based on it's value , it writes to a reserved address in the stack the address of the following sub-routine.
It looks something like this:
1000 jsr r5,switchcase
// let the return address be 1004
1004 jsr r5,@0(sp)
the first jsr goes to the switch case, which writes to the first address in the stack. the second one jumps to that address.
I'm using a simulator , and every time it reaches that line it simply stops. I don't know what goes wrong :/
any help would be appreciated.
The instruction jsr r5,@0(sp)
pushes the old r5
onto the stack and puts the current R7
(PC) into r5
. Therefore your program does not jump to the address on the stack, but to the address stored in r5
, wharever this is.
So in your example the 1st jsr
instruction writes r5
onto the stack, and then assigns 1004
to r5
.
EDIT: When the program returns with rts
, it restores the old value of r5
from the stack.
The 2nd jsr
instruction pushes thus again this value onto the stack, and then jumps to this address, since it is on top of the stack (distance 0).
If the subroutine called with the 1st jsr
indeed leaves a subroutine address on top of the stack, and the 2nd jsr
should jump there, one had to use jsr r5,@2(sp)
instead. But I would consider this as everything else than good programming style.
Hope I got it right this time...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.