https://github.com/akkartik/mu/blob/main/315stack-debug.subx
 1 # The stack shouldn't grow into the code area.
 2 
 3 == code
 4 
 5 check-stack:
 6     # . prologue
 7     55/push-ebp
 8     89/<- %ebp 4/r32/esp
 9     # . save registers
10     50/push-eax
11     #
12     89/<- %eax 4/r32/esp
13     81 7/subop/compare %eax 0x48600/imm32
14     {
15       7f/jump-if-> break/disp8
16       (abort "stack overflow")
17     }
18 $check-stack:end:
19     # . restore registers
20     58/pop-to-eax
21     # . epilogue
22     89/<- %esp 5/r32/ebp
23     5d/pop-to-ebp
24     c3/return
25 
26 show-stack-state:
27     # . prologue
28     55/push-ebp
29     89/<- %ebp 4/r32/esp
30     # . save registers
31     50/push-eax
32     51/push-ecx
33     52/push-edx
34     #
35     89/<- %edx 4/r32/esp
36     # save old cursor position
37     (cursor-position 0)  # => eax, ecx
38     # print at top-right
39     (set-cursor-position 0 0x70 0)
40     (draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0 %edx 0xf 0xc)
41     # restore cursor position
42     (set-cursor-position %eax %ecx)
43 $check-stack:end:
44     # . restore registers
45     5a/pop-to-edx
46     59/pop-to-ecx
47     58/pop-to-eax
48     # . epilogue
49     89/<- %esp 5/r32/ebp
50     5d/pop-to-ebp
51     c3/return
52 
53 # Helper for debugging deeply recursive calls without logs or traces.
54 # Turn it on, insert calls in the right places, and you get a terse sense of
55 # important parts of the call stack. A poor sophont's stack trace.
56 debug-print:  # x: (addr array byte), fg: int, bg: int    # x is very short; usually a single character
57     # . prologue
58     55/push-ebp
59     89/<- %ebp 4/r32/esp
60     #
61     {
62       81 7/subop/compare *Really-debug-print 0/imm32/false
63       74/jump-if-= break/disp8
64       (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 *(ebp+8) *(ebp+0xc) *(ebp+0x10))
65     }
66 $debug-print:end:
67     # . epilogue
68     89/<- %esp 5/r32/ebp
69     5d/pop-to-ebp
70     c3/return
71 
72 == data
73 Really-debug-print:
74   0/imm32/false