Svensk – English post. Hur man INTE skriver poesi / How NOT to program the Commodore 64

2018 May 3
by mackan

OK – Dark Arts of Nerdery ahead.

The first part – the source code – is fairly well documented. It is a joke on how not to write code. The result of the program is another program – 13 bytes in length, starting in a safe place in memory.

But the way to get there…


;
; EXAMPLE / JOKE CODE - How NOT to write
; for the Commodore 64.
; The code, when run, results in the
; following code with start from $c000
; SYS 49152 from BASIC to start.
;
; *=$C000
; LDA #$00
; STA $D020
; STA $D021
; LDA #$03
; STA #0286
;
; If you think that the code below is
; hard to read, you are correct. It is
; an example of how a machine would
; write the code.
;
; The first 10 (0a) instructions are
; basically poking the LDA and STA
; instructions directly into the
; RAM adresses.
;
; The last three instructions here
; are actually the high byte(s) for
; the instruction in $C002 (STA)
;
; Then comes the BRK instruction,
; but it is not a real instruction,
; only the data byte to the
; instruction in $C000 (see above).
;
; The start-pointer is then moved
; around to add the low bytes of
; the adresses for the $c002
; instruction (the STA from above)
; but of course written in binary.
;
; For clarity.
;
; The last block of code pokes the
; lowbyte and the highbyte of the
; adress for the instruction stored
; in $C00A - again an STA.
; The last instruction is an
; obfuscated
; LDA #$03
; STA $0286
; What the LDA #$03 does here is
; simply poking the value directly
; into memory. So sure - it makes
; and LDA #$03 but stores it as
; the second part of the instruction
; LOCATED at $C00A
;
; So there you have it. A terrible
; program, writing another program
; by performing stupid and mind-
; boggling, pessimal memory
; operations / manipulations.
;
; Just a stupid joke, really.
;
; Mackan Andersson, May 2, 2018
;

LDA #$A9
STA $C000
STA $C008
LDA #$8D
STA $C002
STA $C005
STA $C00A
LDA #$D0
STA $C003
STA $C007

*=C001
BRK

*=C003
.bin 00100000 ; .byte $20

*=C006
.bin 00100001 ; .byte $21

; The Last Block of Code!

*=C00B
.byte $86 $02 ; #400

LDA #$03
STA $C009

; Run this program, reset and sys 49152 in BASIC

I have several friends that looked upon the code and really tried to understand it, to see the funny, but they just left in frustration.

So here’s the explanation, in layman’s terms.

What the program does is
1) A literal reading of code. It does not treat instructions as instructions, adresses as adresses and data as data, but treats _everything_ the same (as data). This is the first level of obfuscation.

And teaches us the dangers of reading ANYTHING literal, and out of context, and leaving no regards to genre…

2) It “optimises” the code in all the wrong places.

When running, instead of following the natural flow of the program, it parses the text for the most common “words” (but literally: bytes) and writes them into the program, leaving the spaces in between, then taking the second most used “word” (byte…), etc.

And teaches us that over-optimising often leaves us confused. And makes it sometimes hard to fill in the gaps, in a logical way.

The program certainly does not gain anything, but bulk (!) by doing this. It just adds confusion and extra ballast in the code.

(on a technical note – this is how most programmers program today. They write somewhat “efficient” code in a high level programming language, that the compiler then translates into bad machine code and then tries to “optimise”, kind of exactly in these steps…

Applied to this example – the hand made code is 13 bytes in size (dec), the “optimised” code is 35 bytes – almost three times the original size! Having added nothing but confusion)

3) The program ends with a correct (!) instruction, but implemented in the wrong way. (The last LDA #$03).

The result is that a reader might THINK that they “get it”, but is thrown off. It is a matter of context – doing the right thing but in the wrong place still just brings you trouble.

And thus, the joke is explained. Sorry.

—-
Huvuddelen av programmet – den första tredjedelen – skulle ha skrivit den lilla sången

“I Medelhavet sardiner simmar
a-puh, a-puh,
a-puh, a-puh
men i mitt hjärta, där simmar du
a-puh, a-puh
a-puh, a-puh”

till

“a-puh – sätt in som ord 5, 6, 7, 8, 16, 17, 18, 19.
simmar – sätt in som ord 4, 14
i – sätt in som ord 1, 10 …”

Det tar alltså inte hänsyn till att orden står i en specifik ordning – att det är ordningen som ger mening.

På andra ställen i koden gör programmet motsvarigheten till fulingen

“I Xdelhavet sardiner simmar…
Xn i mitt hjärta, där simmar du.
X=me”

Det sista som sker, med LDA – STA-instruktionen, går inte riktigt att förklara i språkmässiga termer.

Kanske som ett citat, där man byter ut ett ord eller något, så att det helt ändrar mening. “Det gäller att stryka medan järnet är varmt”, typ.

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS