Find limit of recursion: Difference between revisions
Content added Content deleted
m (Moved x86 Assembly entry into correct alphabetical order.) |
(Add Uxntal) |
||
Line 3,210: | Line 3,210: | ||
recurse 1</syntaxhighlight> |
recurse 1</syntaxhighlight> |
||
=={{header|Uxntal}}== |
|||
Uxn has a known stack size of 256 bytes, which allows 128 function calls. However, assuming we don’t know this, we can find the stack size with a program anyway. In older versions of Uxn, it was possible to detect stack overflow with the System vector, which would make this task easier, but the current Uxn stacks are circular, with no overflow and underflow checks, which means that we have to get a bit more creative. Calling a recursive function enough times will cause the return stack pointer to wrap around and overwrite the first return address, which means execution will be trapped in the recursive function forever. By detecting when the function has run more times than expected, the recursion limit can be found. |
|||
<syntaxhighlight lang="Uxntal">|00 @System &vector $2 &expansion $2 &wst $1 &rst $1 &metadata $2 &r $2 &g $2 &b $2 &debug $1 &state $1 |
|||
|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1 |
|||
|00 @calls $1 |
|||
|0100 |
|||
#01 |
|||
&loop |
|||
DUP .calls STZ |
|||
recurse |
|||
INC !&loop |
|||
@recurse |
|||
( keep calling recurse until stack value is 00 ) |
|||
#01 SUB DUP #00 EQU ?&done |
|||
recurse |
|||
( as we walk back up the stack, increment counter ) |
|||
&done INC |
|||
( if we go above the original call count, the stack was corrupted ) |
|||
DUP .calls LDZ GTH ?&fail |
|||
JMP2r |
|||
&fail |
|||
;msg1 print-str |
|||
.calls LDZ print-hex |
|||
;msg2 print-str |
|||
#80 .System/state DEO BRK |
|||
@print-str |
|||
&loop |
|||
LDAk .Console/write DEO |
|||
INC2 LDAk ?&loop |
|||
POP2 |
|||
JMP2r |
|||
@print-hex |
|||
DUP #04 SFT print-digit #0f AND print-digit |
|||
JMP2r |
|||
@print-digit |
|||
DUP #09 GTH #27 MUL ADD #30 ADD .Console/write DEO |
|||
JMP2r |
|||
@msg1 "Stack 20 "overflow 20 "at 20 "# 00 |
|||
@msg2 20 "calls. 0a00</syntaxhighlight> |
|||
=={{header|Vala}}== |
=={{header|Vala}}== |