While just going ahead and implementing a mutual recursive odd/even subroutine, I encountered even more weirdness.
I defined a macro to pop the stack and return, but then NBC gave an error that a subroutine needs to end with a return statement.
The odd/even code seems to break out of the loop midway, for no obvious reason. The code blow prints 4 on the screen. It could be just a bug in my code, but I doubt it.
Code: Select all
dseg segment
TStack struct
data word[]
offset word
TStack ends
returnstack TStack
nextword word
datastack TStack
val word
dseg ends
#define push(stack, val) \
replace stack.data stack.data stack.offset val \
add stack.offset stack.offset 1
#define pop(stack, val) \
sub stack.offset stack.offset 1 \
index val stack.data stack.offset
#define callword(theword) \
subcall theword nextword
myseg segment
number word
myseg ends
subroutine odd
push(returnstack, nextword)
pop(datastack, number)
brtst EQ NotOdd number
sub number number 1
push(datastack, number)
callword(even)
jmp RetOdd
NotOdd:
push(datastack, 0)
RetOdd:
pop(returnstack, nextword)
subret nextword
ends
subroutine even
push(returnstack, nextword)
pop(datastack, number)
brtst EQ Even number
sub number number 1
push(datastack, number)
callword(odd)
jmp RetEven
Even:
push(datastack, 1)
RetEven:
pop(returnstack, nextword)
subret nextword
ends
thread main
arrinit datastack.data 0 32
arrinit returnstack.data 0 32
push(datastack, 1)
push(datastack, 2)
push(datastack, 9)
push(datastack, 7)
callword(even)
pop(datastack, number)
NumOut(0,0,number)
wait 5000
endt