collapseos/apps/forth/stack.asm

42 lines
1.0 KiB
NASM
Raw Normal View History

2020-03-07 12:13:15 -05:00
; The Parameter stack (PS) is maintained by SP and the Return stack (RS) is
; maintained by IX. This allows us to generally use push and pop freely because
; PS is the most frequently used. However, this causes a problem with routine
; calls: because in Forth, the stack isn't balanced within each call, our return
; offset, when placed by a CALL, messes everything up. This is one of the
; reasons why we need stack management routines below.
;
; This return stack contain "Interpreter pointers", that is a pointer to the
; address of a word, as seen in a compiled list of words.
; Push value HL to RS
pushRS:
ld (ix), l
inc ix
ld (ix), h
inc ix
ret
; Pop RS' TOS to HL
popRS:
dec ix
ld h, (ix)
dec ix
ld l, (ix)
ret
2020-03-07 20:20:11 -05:00
; Verifies that SP is within bounds. If it's not, call ABORT
chkPS:
ld hl, (INITIAL_SP)
; We have the return address for this very call on the stack. Let's
; compensate
dec hl \ dec hl
or a ; clear carry
sbc hl, sp
ret nc ; (INITIAL_SP) >= SP? good
; underflow
ld hl, .msg
call printstr
2020-03-07 22:18:14 -05:00
jp abort
2020-03-07 20:20:11 -05:00
.msg:
.db "stack underflow", 0