;redcode
;name CloackTechnologyV4Unrolled
;author Skybuck Flying
;strategy cloack/decloack demonstration
;version 4
;date 26 january 2008
; I am a bit bored right now, so let's do something usefull and write a
; little bit of code which I might re-use later or just for demonstration
purposes.
; this piece of code / demonstration / demo / routines / CODE LOL
; will show how to completely cloack some instruction and complete de-cloack
; them by using some routines and some loops and some counters and some
p-space.
; ok v2 working, now let's try to optimize it a little bit by getting rid of
the extra
; counter
; extra counter illiminated, counter is now reset by subtracting the
instruction length of it.
; code repeats it self, cloack/decloack/cloack/decloack/cloack/decloack
;
; further optimizations in version 4: post increment operators used to
increment the parameter counters
; for p-space, instead of storing a,b,a,b,a,b,a,b,a,b the parameters are now
stored:
; a,a,a,a,a,a,a,a,b,b,b,b,b,b,b,b,b thereby the counters only need to be
incremented with 1
; making the post increment operators possible with only one instruction
; nop >a, >b (cloack incrementation)
; nop }a, }b (decloack incrementation)
;
; loops could be seperated:
;
;Drawback is it would make the code a bit slower because of double looping
;and counters and such.
;
;Actually it could even be seperated into multiple loops:
;
;Cloack:
;
;1. One loop to cloack parameter A
;2. One loop to cloack parameter B
;3. One loop to zero parameters
;
;Decloack:
;
;4. One loop to decloack parameter A
;5. One loop to decloack parameter B
;
;I just also had an alternative idea:
;
;The loops could be unrolled.
;
;So all instructions could simply be hard code like so:
;
;DeCloackParameterB ldp.ab #1, InstructionsBegin + 0
;DeCloackParameterB ldp.ab #2, InstructionsBegin + 1
;DeCloackParameterB ldp.ab #3, InstructionsBegin + 2
;DeCloackParameterB ldp.ab #4, InstructionsBegin + 3
;DeCloackParameterB ldp.ab #5, InstructionsBegin + 4
;
;Then the real program is stored as such:
;
;DeCloackParameterB ldp.ab #0, 0
;DeCloackParameterB ldp.ab #0, 0
;DeCloackParameterB ldp.ab #0, 0
;DeCloackParameterB ldp.ab #0, 0
;DeCloackParameterB ldp.ab #0, 0
;
;Then the only thing which needs to happen is to use a counter to fill the
;correct parameters.
;
;And then the parameters will be loaded into the final program/destination.
;
;Cool idea.
;
;Extremely fast too.
;
;No looping required, loops can remain small.
;
;Drawbacks:
;
;Easier to kill by bombers, bigger code so maybe easier to spot.
;
;So it has adventages and disadventages.
;
;I think I will try both in the near future.
;
jmp Cloack
; Instructions to be cloacked/decloacked:
InstructionsBegin
InstructionsLength equ InstructionsEnd - InstructionsBegin
nop $555, $666
mov $777, $888
add #999, #1010
spl @1111, @1212
jmp #1313, #1414
dat $1515, $1616
jmz $1717, $1818
sub $1919, $2020
InstructionsEnd
Cloack
; Store a-parameter of instructions into p-space
StoreAParameters
stp.ab $InstructionsBegin+0, #1
stp.ab $InstructionsBegin+1, #2
stp.ab $InstructionsBegin+2, #3
stp.ab $InstructionsBegin+3, #4
stp.ab $InstructionsBegin+4, #5
stp.ab $InstructionsBegin+5, #6
stp.ab $InstructionsBegin+6, #7
stp.ab $InstructionsBegin+7, #8
; Store b-parameter of instructions into p-space
StoreBParameters
stp.b $InstructionsBegin+0, #9
stp.b $InstructionsBegin+1, #10
stp.b $InstructionsBegin+2, #11
stp.b $InstructionsBegin+3, #12
stp.b $InstructionsBegin+4, #13
stp.b $InstructionsBegin+5, #14
stp.b $InstructionsBegin+6, #15
stp.b $InstructionsBegin+7, #16
; Clear a and b parameters of instructions
ClearParameters
sub $InstructionsBegin+0, $InstructionsBegin+0
sub $InstructionsBegin+1, $InstructionsBegin+1
sub $InstructionsBegin+2, $InstructionsBegin+2
sub $InstructionsBegin+3, $InstructionsBegin+3
sub $InstructionsBegin+4, $InstructionsBegin+4
sub $InstructionsBegin+5, $InstructionsBegin+5
sub $InstructionsBegin+6, $InstructionsBegin+6
sub $InstructionsBegin+7, $InstructionsBegin+7
; Clear above instruction parameters with loop
; if instructions first copied to some place else this might not be
required
; not done yet/unfinished alternative solutions thinkable
; like storing parameters on the first round and then dieing
; and then load parameters on second run when appriorate
DeCloack
; Restore a-parameter of instructions from p-space
RestoreAParameters
ldp.a #1, $InstructionsBegin+0
ldp.a #2, $InstructionsBegin+1
ldp.a #3, $InstructionsBegin+2
ldp.a #4, $InstructionsBegin+3
ldp.a #5, $InstructionsBegin+4
ldp.a #6, $InstructionsBegin+5
ldp.a #7, $InstructionsBegin+6
ldp.a #8, $InstructionsBegin+7
; Restore b-parameter of instructions from p-space
RestoreBParameters
ldp.ab #9, $InstructionsBegin+0
ldp.ab #10, $InstructionsBegin+1
ldp.ab #11, $InstructionsBegin+2
ldp.ab #12, $InstructionsBegin+3
ldp.ab #13, $InstructionsBegin+4
ldp.ab #14, $InstructionsBegin+5
ldp.ab #15, $InstructionsBegin+6
ldp.ab #16, $InstructionsBegin+7
jmp Cloack