Skip to main content
2 of 5
deleted 283 characters in body
seequ
  • 1.7k
  • 1
  • 11
  • 21

##Redcode All this is because of Ilmari Karonen's self terminating program. So, in Redcode looping always requires some kind of jumping. Because of that, I decided to create a program which copies itself completely, and jumps to the start of the new program after that, thus replicating itself infinitely.

Edit: The old version was convoluted.

; Set some constants
length equ 8              ; The program is 8 instructions long
step   equ length         ; When step equals the length, the program copies itself after one empty space
jump   equ (step+start+1) ; This points to the start location of the copy

        org    start
ptr     dat    #1        ,    #0
targ    dat    #1        ,    #0
leaf1   mov.ab #step     ,    targ  ; Reset the target
leaf2   mov.i  >ptr      ,    >targ ; Copy *ptr++ -> *targ++
        slt    #length   ,    ptr   ; Jump back if ptr < length
        jmp    leaf2
        jmp    jump                 ; When done, jump to the start of the new program.
start   ; The loopable code can be placed here
        ; ...all the way until
        jmp leaf1
        end

So, if this code is run, the execution starts from the label start (big surprise). The leaf subprogram does the copying. That subprogram can handle any size of a program, as long as it's the first part of it. I'll show you what the program looks like in the memory (hand compiled for brevity):

Iteration 1
0000    dat    #1        ,    #0
0001    dat    #1        ,    #0
0002    mov.ab #8        ,    -1
0003    mov.i  >-3       ,    >-2
0004    slt    #8        ,    -4
0005    jmp    -2
0006    jmp    10
0007    jmp    -5  (execution starts here)

Iteration 2
0000    dat    #1        ,    #0
0001    dat    #1        ,    #0
0002    mov.ab #8        ,    -1
0003    mov.i  >-3       ,    >-2
0004    slt    #8        ,    -4
0005    jmp    -2
0006    jmp    10
0007    jmp    -5  (execution starts here)
0008    <empty>
0009    dat    #1        ,    #0
0010    dat    #1        ,    #0
0011    mov.ab #8        ,    -1
0012    mov.i  >-3       ,    >-2
0013    slt    #8        ,    -4
0014    jmp    -2
0015    jmp    10
0016    jmp    -5  (execution continues here)
seequ
  • 1.7k
  • 1
  • 11
  • 21