; Stacks.asm - Internal use by the NXDOS kernel
;            - 2005, 2006 Christopher Robert Evans; tekno1911@yahoo.com

stacksize   EQU 128

StackCode:  

int_stackoverflow proc far
          mov  si, offset intstackofmsg
iso_ml:
          mov  al, byte ptr cs:[si]
          inc  si
          cmp  al, 0
          je   iso_x
          cmp  al, 0ffh
          jne  iso_x1
          mov  ah, 0eh
          mov  al, 0ah
          int  10h
          mov  al, 0dh
iso_x1:
          mov  ah, 0eh
          int  10h
          jmp  iso_ml
iso_x:
          cli
          hlt
          ret
intstackofmsg db 'Internal Stack Error.',0ffh
              db 'SYSTEM HALTED.',07h,0ffh,0
int_stackoverflow endp

spush proc far
; AX is stored in stack
          push ds
          push bx
          push cs
          pop  ds
          mov  bx, stackptr
          cmp  bx, (stacksize*2)-2
          jae  stackoverflow_1
          mov  word ptr ds:[stack_data][bx], ax
          inc  bx
          inc  bx
          mov  stackptr, bx
          jmp  stkp_x
stackoverflow_1:
          call int_stackoverflow
stkp_x:   
          pop  bx
          pop  ds
          ret
spush endp

spop  proc far
; last word stored is returned in AX
          push ds
          push bx
          push cs
          pop  ds

          mov  bx, stackptr
          cmp  bx, 2
          jb   stackoverflow_2
          dec  bx
          dec  bx
          mov  ax, word ptr ds:[stack_data][bx]
          mov  stackptr, bx
          jmp  stkp_xx
stackoverflow_2:
          call int_stackoverflow
stkp_xx:   
          pop  bx
          pop  ds
          ret
spop  endp

ssize proc far
          mov  ax, word ptr cs:[stackptr]
          ret
ssize endp

sitems proc far
           mov  ax, word ptr cs:[stackptr]
           shr  ax, 1
           ret
sitems endp

; dynamically build stack CB's here
Stackdata:
stackptr    dw 0
stack_data  dw stacksize dup(1)        

STACK_RELOCATE_SIZE equ ($ - StackCode)
