Shop OBEX P1 Docs P2 Docs Learn Events
PC Stack and PC Shadow Stack, PushPC Secret instruction??? — Parallax Forums

PC Stack and PC Shadow Stack, PushPC Secret instruction???

dkemppaidkemppai Posts: 315
edited 2005-07-14 14:46 in General Discussion
OK, I'm trying to sort out a few ideas.

Are·the PC Shadow Stack (accessed via the secret instructions) and normal PC stack different?·I find references·on·www.sxlist.com·that says the·PC Shadow·stack used by PushPC·is 2·levels deep, while the SK-Key·Manual makes reference to the PC Stack being 8 levels deep (with the stack extended).

If I·pushPC ($04B) the contents to M and W to the stack, is this the same stack as used by the interrupt call instructions? Are they different? Any implications of me using PushPC and possible gotcha's you guys are aware of.



Thanks,
Dan

Comments

  • dkemppaidkemppai Posts: 315
    edited 2005-07-13 21:27
    Ok, I think I just figured out the answer.

    Here is what I was up to.

    I was trying to write a psudo call routine to be used in the upper half of a page of memory. I have a routine that needs to be called, and then calls additional routines multiple times (all residing in the upper half of the page). Because of the size of my program, the program overflows into the upper half of the page and will not work. The solution is to segregrate code into the upper half of the page.·The·standard call routines won't work. I can't use a jump table, because I need to call routines in the upper half of the page from other routines existing in the upper half of the page.

    With a few extra instructions, one can effectivley use the 'Secret instructions' and RETI to 'call' and 'return' from sub routines in the upper half of a page. I'm certian that this could be expanded to include calls to and from anywhere, with a few extra bytes of memory/instructions per routine.·Another possibility is to not use the JMPLOC variable, by replacing it with a PushW, PopW...··

    I'm sure macros could simplify this goobly gook...· ...but I just don't do marcos (Unless forced to!!!)

    Finally, a way to usefully use the secret instructions!!! (grin)

    Calls in the upper half of a page, to the upper half of the page are done in the format:· (JMPLOC is a byte or ram)
    ··MOV ·JMPLOC,#$+3
    ··JMP·RoutinetoCall

    Returns are:
    ··PushW···;Load W into W Shadow Stack
    ··MOV·W,STATUS·;MOVE STAT to W
    ··PushSTAT··;Load W into STAT Shadow Stack
    ··MOV·W,FSR··;Move the FSR to W
    ··PushFSR···;Load W into FSR Shadow Stack
    ··MOV·M,#$>>8··;Load Current Page/Half Page into W
    ··MOV·W,JMPLOC·;Load W with "Next" Addr on this 1/2 Page2
    ··PushPC···;Push W and M to PC Stack·
    ··RETI···;Pop the All Registers back...


    ·Anyway, here's the file.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-13 21:34
    Dan, that was my first thought when I saw the secret instructions, but I never followed through on implementing it. Peter (pjv) used the secret instructions in his RTOS submitted for the SX contest, he used them to force switching between processes (using the call stack if I remember correctly). So go look at his code, it may give you some ideas. I am contemplating using the secret FIFO in my current project, I need to make some calculations to see if it will be large enough for my needs.

    Post Edited (Paul Baker) : 7/13/2005 9:37:34 PM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-07-13 22:41
    Maybe I am misunderstanding the issue,

    but using 3 words to call a subroutine in upper half of a page

    from a subroutine in upper half of another page is unneccessary. It only requires

    a single word per subroutine, by putting a jump to that

    subroutine in the lower half of the page where the subroutine

    resides. See the adapted attachement.

    regards peter




    Post Edited (Peter Verkaik) : 7/13/2005 10:51:10 PM GMT
  • pjvpjv Posts: 1,903
    edited 2005-07-14 00:14
    Hi Dan, Paul, Peter;
    dkemppai said...
    Are·the PC Shadow Stack (accessed via the secret instructions) and normal PC stack different?·I find references·on·www.sxlist.com·that says the·PC Shadow·stack used by PushPC·is 2·levels deep, while the SK-Key·Manual makes reference to the PC Stack being 8 levels deep (with the stack extended).

    If I·pushPC ($04B) the contents to M and W to the stack, is this the same stack as used by the interrupt call instructions? Are they different? Any implications of me using PushPC and possible gotcha's you guys are aware of.
    Dan, the 8 level call stack is a totally different and separate animal from the 2 level shadow stacks used by the interrupt service. And yes, you can do what you are suggesting,·but as Peter Verkaik pointed out, there are simpler ways to accomplish·it,·provided you are prepared to have a jump table in some lower half segment.

    There are some very complicated "gotcha" issues dealing with what appears to be a simple pre-loaded RETI. In particular when interrupts are active, and interrupts in the middle of a SKIP instruction. Too hard to explain in simple terms.
    Paul said... he used them to force switching between processes (using the call stack if I remember correctly).
    Paul; The call stack itself was not used, but the call instruction was. I have since made conceptual changes as well as corrections to the code.

    Peter;·I agree with your approach in general, but for coding convenience I would put all the "called subroutine jumps" together at one low half segment of memory. Realizing of course that @ (page) instructions are likely required as part of the jumps in that table.

    Cheers,

    Peter (pjv)
  • dkemppaidkemppai Posts: 315
    edited 2005-07-14 14:46
    Peter Verkaik said...

    Maybe I am misunderstanding the issue, but using 3 words to call a subroutine in upper half of a page from a subroutine in upper half of another page is unneccessary. It only requires a single word per subroutine, by putting a jump to that subroutine in the lower half of the page where the subroutine resides. See the adapted attachement. regards peter

    Yep, that works too...·· ...funny, I must have been doing something wrong yesterday. My jump table calls wouldn't work...·· ....They seem to be working now...

    I am using pjv's suggestion of calls @routine...

    -Dan
Sign In or Register to comment.