Least lines of code contest? okay!
I was taking a computer science class on computer hardware organization, taught by a professor who used to work on the AMD chip architecture. He was a tenured professor, set in his old ways, and felt a bit entitled because of his past career prestige. Even in 2009, he demanded that *all* coding assignments are turned in on 3.5 inch floppy drives. You really had to scrounge to find an old working floppy drive, even in 2009. He was very strict and by the book on his policies.
Our professor had been teasing us with an upcoming assignment, saying that there had been a multi-year contest to see who could write the assignment program in the least lines of code possible. Historically, the brightest and smartest student he ever taught accomplished the assignment in 13 lines of code. Typically, students needed about 100 lines of assembly code, and the best can usually do it in about 30 lines of code. I *had* to win, for bragging rights and all.
We got to writing programs in 6800 assembly code. I had already turned in a few assignments and gotten the hang of assembly, and spent a bit of extra time really getting to understand how programs really work under the hood.
So, every program has "registers" which are working blocks of very small memory on the processor, usually storing a single value. There's also a "program counter", which is responsible for executing the next instruction in memory. A program counter doesn't always read instructions in sequential order, it might bounce around in memory as you call different functions. I discovered that when we wrote our programs in assembly, they get converted into binary code, and then the binary code is what gets executed by the program counter. The program counter was a special register which could be manipulated to point at a different block of memory to execute next. Interesting.
What was even more interesting is that the computer didn't care where the byte data was stored. It just blindly ran whatever byte data you gave it, without checking whether it was valid memory space for executable instructions or even if it was a valid instruction. On normal operating systems, memory gets divided into several regions: stack memory, heap memory, and instruction space. If you try to run instruction code in heap or stack memory, usually your operating system pukes and rejects it. But... not in 6800 assembly. This was a very interesting finding for me.
Finally, the day of the infamous assignment arrives. We are supposed to write a memory tester. Basically, you have a big block of memory filled with garbage, you write all zeros to the memory in one pass, verify the memory values are all zero, then write all ones, verify they're all ones, and if all memory values are verified, the memory is considered "good" and the assignment is complete. The professor specifically said, "There is no cheating here, get creative, use whatever trick in the book you can imagine." Okay, challenge accepted! I pride myself on thinking far more creatively than my peers, I have a hacker mindset. No rules? Perfect. He's going to regret that.
Even more interesting were the few rules the professor gave: Precisely what did not count as a line of code. Three lines did not count: "Start, End, and DCB." DCB stands for "Declare Constant Block", which just means you can store some constant values in memory, such as the digits of PI, a few words, or whatever data you wanted. I immediately saw how I could hack his assignment.
"Does anyone have any questions?" he asked the class. I had a question, but if I asked it, I would give away the hack and the professor would revise the rules. So, I kept my mouth shut.
"Good. In all the classes I have taught, the record has been 13 lines of code. I expect the best of you to get this in about 30 lines of code." He had been telling this same story for years, to all the classes he taught. Now, it's time for me to update his story.
So, I wrote my application in about 20-25 lines of assembly code. I spent many hours shaving off instructions, trying to be as clever as I could. It was already pretty impressive. But now it was time to "hack" his assignment.
When you write assembly code, it gets converted into constant byte data. Remember the 'declare constant block' which doesn't count? What if I just... copy/paste my compiled byte code and put it into the DCB? And then I put the DCB as the first instruction that gets run? So, I did. And it worked! The program counter started at the first byte in memory, didn't care that instruction code was in stack memory, and it just executed it. My whole assignment was just an unreadable block of byte code pasted into a DCB and it worked perfectly. Zero lines of code.
I announced on our student message board that I had broken the record for least lines of code. I had accomplished the impossible, by completing the assignment in zero lines of code. Other students were astounded and in disbelief, guessing at how I could have done this. I told them that once the assignment due date had passed, I would reveal my secret.
We turned in our assignments. No late turn ins, as per professors policy. All floppy disks had been placed. Finally, the professor asked me to explain how I had done it. I was a bit sleep deprived (having been awake until 5am), so I wasn't super coherent.
"I took my program and wrote it as small as I could, then I took the resulting binary and converted it into a DCB, set that as the first block of data, and the program ran flawlessly." (to paraphrase)
"Wait, you can't do that! That's cheating!" the professor replied in shock. I was ready for this reaction.
"Nope, you specifically said that there were no rules and that DCB did not count as a line of code. So I hacked your assignment on a technicality and accomplished it in zero lines of code!" I said triumphantly.
I got 'em. The professor couldn't refute me and had to admit that what I did was clever and he'd have to think about it, and acknowledged I had won his contest.
A week later, he announces to the class that he was riding his bike home from work, thinking about what I did and he was revoking my 'victory' by changing the rules of the assignment. Therefore, my entry was 'invalid'. I was too sleep deprived and unprepared to argue, I didn't care anymore, I won his silly little game and he's just a sore loser for changing the rules post-hoc after the game is over. He can't take that away from me. Even if I counted my actual lines of code, I still outperformed all of my peers.