asm_setjmp
8086 code.
asm_setjmp:
pop WORD [env]
push WORD [env] ; get and put back the return address
push cx
push dx
push bx
push bp
push si
push di ; push all registers except ax
push WORD [env] ; return address stacked for asm_longjmp
mov [env], sp ; save sp for asm_longjmp to restore
add WORD [env], 2 ; adjust sp to point to di on the stack
; which also clears the carry flag for
ret ; the initial return to asm_setjmp
asm_longjmp:
push WORD [env]
pop sp ; retore sp
pop di
pop si
pop bp
pop bx
pop dx
pop cx ; pop all registers except ax
stc
ret ; return to asm_setjmp with the carry flag set
env dw 0
Use the functions like this:
call asm_setjmp
jnc .1
; handle errors here, any error code in ax
; even use 0 for a no error exit for example
.1:
; ...
; handle errors here
mov ax, -1 ; any error code in ax
jmp asm_longjmp ; no need to "call ahead" since "you ain't comin' back"