Tuesday, 6 January 2009

Call / Return Macros in Redcode, a Possible Solution

Here's one solution to the call / ret problem in Redcode. Unfortunately the offsets in multi-line-equates can be slightly out. In this solution, the offsets are adjusted the first time the call is executed.  Here's how it works:

call routine compiles as follows:
        mov    #2-return, <stack ; save return address
jmp callsub, 1+routine ; jump to callsub which adjusts the address
; b-field pointer is either correct or out by 1
callsub then adjusts the pointer if necessary and moves it to the a-field of the jmp:
         mov    #2-return, <stack ; save return address
jmp routine, 1+routine ; jump to routine, a-field is correct
The jmp now contains the correct offset and jumps directly to routine in future.

Here's the code:
stack   dat    0

call equ mov #2-return, <stack
equ jmp callsub, 1+

ret equ jmp return

return mov.b >stack, #0
jmp @return

callsub mov.b @stack, #0
add #return-callsub, callsub
mov.ba <callsub, @callsub
slt.b @callsub, #CORESIZE/2
jmp @callsub
djn.a @callsub, @callsub

No comments: