From fe7bfb12e6e4204c81362bd3ae4e0f4a3d18646b Mon Sep 17 00:00:00 2001 From: Russell-S-Harper Date: Tue, 7 Aug 2018 20:14:24 -0400 Subject: [PATCH 1/2] Adding protection for PSH and POP. --- common/common.asm | 9 +++++++-- common/common.h | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/common/common.asm b/common/common.asm index 808e002..f607661 100644 --- a/common/common.asm +++ b/common/common.asm @@ -118,7 +118,9 @@ _1 RTS ; done _POP .( ; POP r 2r Rr <- RS - pop from stack LDY _RSI ; get register stack index - DEY ; transfer four bytes over + BNE _1 ; all good, something can be popped off the stack + BRK ; next pop will cause a stack underflow, abort and call exception handler (TODO) +_1 DEY ; transfer four bytes over LDA _RS,Y STA _R0+3,X DEY @@ -136,7 +138,10 @@ _POP .( ; POP r 2r Rr <- RS - pop from stack _PSH .( ; PSH r 3r RS <- Rr - push onto stack LDY _RSI ; get register stack index - LDA _R0,X ; transfer four bytes over + CPY #_RSS ; compare against limit + BCC _1 ; still room, all okay + BRK ; next push will cause a stack overflow, abort and call exception handler (TODO) +_1 LDA _R0,X ; transfer four bytes over STA _RS,Y INY LDA _R0+1,X diff --git a/common/common.h b/common/common.h index fa732bb..c91bb2b 100644 --- a/common/common.h +++ b/common/common.h @@ -89,11 +89,12 @@ _IDX = _ACC + 1 ; saved index X to restore _IDY = _IDX + 1 ; saved index Y to restore _PS = _IDY + 1 ; saved processor status to restore -; 256 bytes of page two +; 224 bytes of page two _RS = $200 ; register stack +_RSS = (FN_FX - _RS) ; register stack size -; 64 bytes of page three -FN_FX = $300 ; list of system and user functions +; last 32 bytes of page two +FN_FX = $2E0 ; list of system and user functions ; function constants _ESC_C = $00 From bb3b6cd6838bb76ea611621f76588c4203decc31 Mon Sep 17 00:00:00 2001 From: Russell-S-Harper Date: Tue, 7 Aug 2018 20:19:52 -0400 Subject: [PATCH 2/2] Assigning PSH before POP. --- common/common.asm | 42 +++++++++++++++++++++--------------------- common/common.h | 8 ++++---- common/macros.h | 2 +- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/common/common.asm b/common/common.asm index f607661..c8bbed9 100644 --- a/common/common.asm +++ b/common/common.asm @@ -116,27 +116,7 @@ _SET .( ; SET r aabbcc.dd 1r dd cc bb aa Rr <- aabbcc.dd - set register _1 RTS ; done .) -_POP .( ; POP r 2r Rr <- RS - pop from stack - LDY _RSI ; get register stack index - BNE _1 ; all good, something can be popped off the stack - BRK ; next pop will cause a stack underflow, abort and call exception handler (TODO) -_1 DEY ; transfer four bytes over - LDA _RS,Y - STA _R0+3,X - DEY - LDA _RS,Y - STA _R0+2,X - DEY - LDA _RS,Y - STA _R0+1,X - DEY - LDA _RS,Y - STA _R0,X - STY _RSI ; update register stack index - RTS -.) - -_PSH .( ; PSH r 3r RS <- Rr - push onto stack +_PSH .( ; PSH r 2r RS <- Rr - push onto stack LDY _RSI ; get register stack index CPY #_RSS ; compare against limit BCC _1 ; still room, all okay @@ -157,6 +137,26 @@ _1 LDA _R0,X ; transfer four bytes over RTS .) +_POP .( ; POP r 3r Rr <- RS - pop from stack + LDY _RSI ; get register stack index + BNE _1 ; all good, something can be popped off the stack + BRK ; next pop will cause a stack underflow, abort and call exception handler (TODO) +_1 DEY ; transfer four bytes over + LDA _RS,Y + STA _R0+3,X + DEY + LDA _RS,Y + STA _R0+2,X + DEY + LDA _RS,Y + STA _R0+1,X + DEY + LDA _RS,Y + STA _R0,X + STY _RSI ; update register stack index + RTS +.) + _EXC .( ; EXC r 4r Rr <-> RS - exchange Rr with stack LDY _RSI ; RS to RD LDA _RS-1,Y diff --git a/common/common.h b/common/common.h index c91bb2b..432002c 100644 --- a/common/common.h +++ b/common/common.h @@ -14,8 +14,8 @@ ; Instructions ; SET r aabbcc.dd 1r dd cc bb aa Rr <- aabbccdd - set register -; POP r 2r Rr <- RS - pop from stack -; PSH r 3r RS <- Rr - push onto stack +; PSH r 2r RS <- Rr - push onto stack +; POP r 3r Rr <- RS - pop from stack ; EXC r 4r Rr <-> RS - exchange Rr with stack ; INR r 5r Rr <- Rr + 1.0 - increment register ; DCR r 6r Rr <- Rr - 1.0 - decrement register @@ -115,8 +115,8 @@ _SVI_C = $0e _CMR_C = $0f _SET_C = $10 -_POP_C = $20 -_PSH_C = $30 +_PSH_C = $20 +_POP_C = $30 _EXC_C = $40 _INR_C = $50 _DCR_C = $60 diff --git a/common/macros.h b/common/macros.h index fc2ad78..e3d8951 100644 --- a/common/macros.h +++ b/common/macros.h @@ -75,8 +75,8 @@ #define SVI(p, q) .BYTE _SVI_C, p * 16 + q #define CMR(p, q) .BYTE _CMR_C, p * 16 + q #define SET(r, v) .BYTE _SET_C + r, _SET_V(#v) -#define POP(r) .BYTE _POP_C + r #define PSH(r) .BYTE _PSH_C + r +#define POP(r) .BYTE _POP_C + r #define EXC(r) .BYTE _EXC_C + r #define INR(r) .BYTE _INR_C + r #define DCR(r) .BYTE _DCR_C + r