Bastet
Registered: Jul 2005 Posts: 88 |
Playing around with the return pointer ;)
Just some snippet.
;This little snippet demonstrates how to fool around with the RTS command
;Usefull for fast coding, if you know what you are doing
;
;Most "Pros" will say: Ohh, we did that onemillion years before you told us
;this, but this snippet is not for you, its for the beginners.
;Like me they are hunting down interesting speed up snippets but wont find any.
;So this is for all the beginners that search for them, if i have the time i
;will code more and collect them somewhere for you to find.
;If you happen to be one of the pros and want to add your snippets, do it.
;
;
;So what is this usefull for... you have more parts of a programm seperated into
;different files that all share the same start address. (maybe they should be
;runnable on their own) and you are simply loading with the kernel.
;With this you wont need looking after your loading routine, you can simply
;overwrite it as it is not used anymore.
;You might come up with other uses, but thats what i use it for.
;
;Consider this snippet public domain
;BastetFurry of Dienstagstreff
!to "fakedjsr.prg",cbm
!convtab scr
*=$0800
!byte $00,$0c,$08,$0a,$00,$9e,$32,$31,$32,$38,$00,$00,$00,$00
*=$0850
;Clear the screen first.
lda #$20
ldx #$00
clearscreenloop:
sta $0400,x
sta $0500,x
sta $0600,x
sta $06e7,x
dex
bne clearscreenloop
;Output the first text
;I dont think i need to explain a text output routine
ldx #$ff
loop1:
inx
lda textstart,x
beq exitloop1
sta $0400,x
jmp loop1
exitloop1:
;We manipulate the return pointer by making our own.
;Just push the address, highbyte first, of the return address onto the stack,
;decrement the lowbyte as the return address is saved minus one and do a jump to
;your subroutine. The RTS will send the PC into the right direction.
;If you happen to have a page wrap remember that you need to manipulate the
;high byte too!
;Explained step by step
lda #>alteredreturnpointer ;We load the high byte of the return adress
pha ;Into the stack with it
lda #<alteredreturnpointer ;We load the low byte of the return adress
tax ;Substract one as it is saved in return address minus
dex ;one on the stack
txa ;SBC runs in BCD mode so we cant use it here or we
;would need to set the decimal flag accordingly
pha ;Into the stack with it
jmp subroutine ;We jump to our subroutine
subroutine:
;Output the second text
ldx #$ff
loop2:
inx
lda textsub,x
beq exitloop2
sta $0428,x
jmp loop2
exitloop2:
rts ;This will retrive our modified return pointer from
;the stack, add one to it and jumps to that address.
alteredreturnpointer:
;Output the third text
ldx #$ff
loop3:
inx
lda textalt,x
beq exitloop3
sta $0450,x
jmp loop3
exitloop3:
endless2:
inc $d020
jmp endless2
textstart:
!text "hello from start!"
!byte $00
textsub:
!text "hello from subroutine!"
!byte $00
textalt:
!text "hello from altered entrypoint!"
!byte $00
Hope some newbee, like me, will find this usefull |