| |
midiland Account closed
Registered: Nov 2004 Posts: 4 |
address for changing pointers for rts?
Hey guys
I need a reminder of the addresses used for when I need to change an address for a "rts" so it returns to a new location rather than the old
eg
jsr blah
rts
blah rts
but instead of returning to the jsr'd address to go to a new address
Thanks!!
I cant remember the mem locations to change it :( |
|
| |
Style
Registered: Jun 2004 Posts: 498 |
PLA
PLA
LDA #<new address
PHA
LDA #>new address
PHA
or similar :) During a JSR the return address (PC + 2) is pushed to the stack. I think its in low byte/high byte order.
Obviously during an RTS the address is then pulled from the stack. |
| |
midiland Account closed
Registered: Nov 2004 Posts: 4 |
Ahhh thats it...!!!!
Thanks mate!
|
| |
Graham Account closed
Registered: Dec 2002 Posts: 990 |
Not quite, you must store "address-1" to stack if you want to return to the opcode after the JSR. |
| |
Ninja
Registered: Jan 2002 Posts: 411 |
PLA
PLA
JMP new_adress
is shorter and faster. And as we are at it:
JSR somewhere
RTS
can be replaced with
JMP somewhere
which is also shorter and faster. |
| |
Style
Registered: Jun 2004 Posts: 498 |
He'll figure it out :) |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
ninja, indeed, this question smells me some horrible coding style |
| |
JackAsser
Registered: Jun 2002 Posts: 2014 |
@Oswald: That coding style have saved me quite some times. =) But yes, horrible indeed. |
| |
Zyron
Registered: Jan 2002 Posts: 2381 |
You naughty boy :) |
| |
Monte Carlos
Registered: Jun 2004 Posts: 359 |
If Steve Wozniak used it in its Sweet 16 asm style interpreter
for the Apple II, why should we not use it?
|
| |
Cruzer
Registered: Dec 2001 Posts: 1048 |
I prefer
jmp subroutine
back:
subroutine:
jmp back
for subroutines that are only called from one place, e.g. typically speedcode, since it saves 6 cycles. No coding style is horrible if it saves cycles :) |
| |
Frantic
Registered: Mar 2003 Posts: 1648 |
Cruzer:
But on the other hand you very seldom need the concept of a "subroutine" at all if it's only called from one place. (The only case I could think of is when trying to fit in some code within a page or something.)
I'm smartass number 1. |
| |
Bastet
Registered: Jul 2005 Posts: 88 |
Look over here, i wrote a nice snippet some time ago about that one: Playing around with the return pointer ;) |
| |
Cruzer
Registered: Dec 2001 Posts: 1048 |
@Frantic: Yes I do, for calling speedcode generated by the main code. |
| |
hannenz Account closed
Registered: Nov 2002 Posts: 24 |
well, i think this is a very smart coding style. Espoecially if you want to jump to different routines depending on some value (similar to ON... GOTO):
let's say you want to jump to routine0,routine1,routine2...,routinen depending on the accus's value:
asl ;*2
tax ;to .X
lda adresstab+1,x ;push hi byte of adress
pha ;on stack
lda adresstab,x ;and lo-byte...
pha
rts ;now RTS will get the adress from the stack and jump to it
adresstab .word routine0-1,routine1-1,routine2-2,routine3-1... ;you'll need the adresses MINUS 1 here...!
btw: tmp/style (turbo macro pro assembler) features a handy pseudo-op for this, called ".rta" which assembles the given adresses as "adresses-1" as needed for this technique...
e.g.:
.rta routine0,routine1,routine2,routine3...
i like this technique!
|
| |
TNT Account closed
Registered: Oct 2004 Posts: 189 |
Using rts to branch to different routines is worth the trouble only if you can't spare two memory locations to do "jmp ($nnnn)"...
I've used return address changing for passing arguments to routines, similar to Primm in C128 ROM. Printing strings becomes easy when you can do just
jsr Primm
dc.b "Text to print",0
blah blah ;code continues here
instead of first loading the star address to registers and then calling the routine. It's bitch to disassemble, tho.
If you really want to get creative with it, use BRK interrupt followed with function ID and have functions with different number of data bytes following BRK+ID ;)
|
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
also using the stack is slooow |
| |
Ninja
Registered: Jan 2002 Posts: 411 |
and it destroys the table put there :) |
| |
Monte Carlos
Registered: Jun 2004 Posts: 359 |
Stack is sometimes like local variables.
So doing a lda desttablo,x pha lda desttabhi,x pha rts
is a far better style than lda desttab,x sta jumpdest lda desttab,x sta jumpdest+1 jmp(jumpdest).
|
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
unreadable style is a good style ? dont think so
|
| |
Monte Carlos
Registered: Jun 2004 Posts: 359 |
If you introduce a variable jumpdest and than do a jmp(jumpdest) you need the 2 bytes as long as your program is running. if you use the stack like in the example above, you only reserve memory as long as you need to, the code is shorter and it is faster.
lda #0..51
jsr mul5
....
mul5:
tsx
pha
asl
asl
adc $0100,x
txs
rts
Monte
|
| |
Monte Carlos
Registered: Jun 2004 Posts: 359 |
Just another example for locals. ;)
monte
|