Data inside code

Long time no post… oops… ho hum…

I recently came across some old Z80 code (I have access to extremely little of any of my old Z80 code) and found this little trick… This is one way to annoy automatic disassemblers by the way…

00490 CALL DMESS
00500 DEFB 14,2
00510 DEFM “PRESS SPAC”
00520 DEFM “E TO CONTI”
00530 DEFM “NUE…..”
00540 DEFB 0
00550 LD HL,MHELLO
00560 LD (MPOS),HL
00570 CALL CRAWL

So what is the old code trick? So basically the trick is calling a subroutine (DMESS in this case) and putting the data it needs directly after the call, followed by more code. A disassembler would try to decode the data as code, and make a mess.

In this case the DMESS routine pulls the “return address” the Z80 would normally use when the routine was done, takes two bytes as co-ordinates and then a string of characters to print on screen until it find a zero byte, then pushes the address of the byte after the zero as the new return address as the last thing it does. Voila! Data inside code!

The last two instructions in the DMESS routine are “PUSH HL” and “RET”. Pretty much any time these two instructions are found together in that order there is some sort of programming trick going on.

P.S. The code looks a little bit odd here, particularly the broken words in the message but there is a reason. I always used to code using tools on the machine itself, a ZX Spectrum in this case with a 32 column display. The space between the line number and instructions and assembler directives has been swallowed but the source was set with maximum 30 character lines.

0 comments