Code Query with OSWORD Call

discussion of beeb/electron applications, languages, utils and educational s/w
Post Reply
User avatar
KenLowe
Posts: 308
Joined: Mon Oct 18, 2004 4:35 pm
Location: Scotland
Contact:

Code Query with OSWORD Call

Post by KenLowe » Wed May 30, 2018 5:52 pm

The following pieces of code appear to be the reverse of each other. I can see that a lot of data is being pushed onto the stack, and then the stack pointer is being used to directly read data from PAGE &100 (the stack). It's code that is being executed in response to an OSWORD service type 08 call. Anyone want to hazard a guess as to what this code is actually trying to do???

Code: Select all

.code1      LDX #&07
.loop1      LDA &00A8,X
            PHA
            DEX
            BPL loop1
            PHA
            PHA
            TSX
            LDA &010B,X
            STA &0101,X
            LDA &010C,X
            STA &0102,X
            RTS

Code: Select all

.code2      TSX
            LDA &0101,X
            STA &010B,X
            LDA &0102,X
            STA &010C,X
            PLA
            PLA
            LDX #&00
.loop2      PLA
            STA &00A8,X
            INX
            CPX #&08
            BCC loop2
            RTS

User avatar
ctr
Posts: 145
Joined: Wed Jul 16, 2014 2:53 pm
Contact:

Re: Code Query with OSWORD Call

Post by ctr » Wed May 30, 2018 8:13 pm

Code: Select all

.code1      LDX #&07
.loop1      LDA &00A8,X   ; Copy 8 values from &AF-&A8 to the stack.
            PHA
            DEX
            BPL loop1
            PHA           ; Create space on the stack
            PHA           ; for a return address.
            TSX
            LDA &010B,X   ; Copy the original return address
            STA &0101,X   ; into the space just created.
            LDA &010C,X
            STA &0102,X   
            RTS           ; Return to the caller.
When the original caller resumes the top of the stack contains the values read from &A8-&AF.

I've no idea why. OSWORD 8 in OS 1.2 just copies the envelope definition into one of the buffers starting at &8C0. What OS is this from?

Edit: Just realised, &A8-&AF is scratch space for OS calls. If one OS call wants to invoke another OS call this is a mechanism for saving the scratch space on the stack.

User avatar
KenLowe
Posts: 308
Joined: Mon Oct 18, 2004 4:35 pm
Location: Scotland
Contact:

Re: Code Query with OSWORD Call

Post by KenLowe » Wed May 30, 2018 10:52 pm

Thanks for the feedback.
ctr wrote:
Wed May 30, 2018 8:13 pm
OSWORD 8 in OS 1.2 just copies the envelope definition into one of the buffers starting at &8C0
Sorry, I don't think I was very clear. This code is in a paged ROM and is executed on a Service Entry, type 08 (Unrecognised OSWORD call).
ctr wrote:
Wed May 30, 2018 8:13 pm

Code: Select all

            LDA &010B,X   ; Copy the original return address
            STA &0101,X   ; into the space just created.
I don't think I'm understanding the stack correctly. Where do the addresses &0101/2 and &10B/C come from, and how do they relate to the space created earlier?

Edit 1:
Ah. I think I get it. I was getting confused since the stack being filled from higher memory location (&1FF) to lower memory location (&100), with the Stack Pointer (copied to X) reducing as more is added to the stack, and we're manipulating it from the lower memory end (&101+X).
So if I understand it correctly, the original return address was at &10B/C+X. I then copied 8 bytes from &A8 to the stack, then created another two entries on the stack for the new return address (total additional bytes = &A). The newest return address will now be at &10B/C - &A + X = &101/2+X.

Edit 2:
Right. Understand what's happening now. Quite simple:

Call subroutine 'code1' (save 8 bytes from &A8-&AF to stack & manipulate stack so that return address is still valid)
Run other code, which may overwrite &A8-&AF
Call subroutine 'code2' (restore 8 bytes from stack back to &A8-&AF. manipulate stack so that return address is still valid)
Last edited by KenLowe on Wed May 30, 2018 11:43 pm, edited 1 time in total.

User avatar
ctr
Posts: 145
Joined: Wed Jul 16, 2014 2:53 pm
Contact:

Re: Code Query with OSWORD Call

Post by ctr » Wed May 30, 2018 11:41 pm

Yes, that's right. I find it helpful to remember that the value at the top of the stack is always at &101+S (ignoring when S=&FF).
KenLowe wrote:
Wed May 30, 2018 10:52 pm
I guess the original return address is still sitting there at &10B/C+X, and would need to be removed at some stage.
At the start of .code2 it gets overwritten by the new return address, which is then popped from the stack. Then the value at &10B/C+X is popped by the RTS. It's rather neat.

User avatar
KenLowe
Posts: 308
Joined: Mon Oct 18, 2004 4:35 pm
Location: Scotland
Contact:

Re: Code Query with OSWORD Call

Post by KenLowe » Wed May 30, 2018 11:44 pm

ctr wrote:
Wed May 30, 2018 11:41 pm
At the start of .code2 it gets overwritten by the new return address, which is then popped from the stack. Then the value at &10B/C+X is popped by the RTS. It's rather neat.
Yes. I just worked that out, and edited my post as you were replying.

Thanks for helping me out. It's been a few years since I got this deep into coding!

Post Reply