Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!ucsd!ucbvax!ADMIN.HumberC.ON.CA!GRAY From: GRAY@ADMIN.HumberC.ON.CA (Kelly Gray) Newsgroups: comp.sys.apple2 Subject: Re: relocatable code Message-ID: <90Nov13.183431est.57621@ugw.utcs.utoronto.ca> Date: 13 Nov 90 22:50:07 GMT References: Sender: daemon@ucbvax.BERKELEY.EDU Organization: The Internet Lines: 33 One way to locate a program in memory is to do some playing with the stack. The following bit of code will locate itself wherever it may be. The only restriction is that the motherboard ROM must be enabled since it needs to use an RTS instruction at a known location. IORTS EQU $FF58 ;Address of known RTS instruction in ROM ; LOCATE SEI ;Disable interrupts for a moment JSR IORTS ;This subroutine simply returns back, TSX ;Leaving the return address above the stack DEX ;Alter stack pointer to recover old return address DEX TXS ;Put altered pointer into stack pointer register PLA ;Get PCL of old return address STA LOC ;Save it PLA ;Get PCH of old return address STA LOC+1 ;Save it CLI ;Restore interrupts again After this program fragment is done, the pointer at LOC will point to the TSX instruction that follows the JSR to IORTS. From there it sould be an easy matter to add an offset to this value to point it at your lookup table. This code itself is completely relocatable. There is only one byte that must be at a known address, and that is the RTS instruction at IORTS _________________________ ________________________________________ / \ / \ | Kelly Gray | The opinions expressed in the preceding | | | message are not guaranteed to represent | | GRAY@ADMIN.HumberC.ON.CA | any form of rational thought whatsoever | \_________________________/ \_________________________________________/