Programming
Assembly Language Changes Your Brain
Have Brain, Will Travel

When I was still a young pup, a friend and I would challenge each other with machine language programs. One of us would set a task and the winner would be whoever met the challenge in the shortest number of bytes.
If my memory is correct, he won more often than did, but it was fun for both of us.
A one byte program
I did write one piece of code that was the shortest useful program ever. It was exactly one byte.
We both had access to Tandy Model II computers because I worked at a Radio Shack Tandy Computer Center store that sold them and he worked for a programming company that used them. These had Z80A processors, and with the TRSDOS operating system they ran you could just put machine language bytes on disk and run ’em — no headers necessary, like the old DOS .COM programs.
TRSDOS was a bit short of “user friendly”. For example, if you wanted to delete a bunch of .DAT files, you’d type “KILL */DAT”. Because the designers of TRSDOS had low expectations for your intelligence, the KILL program would ask you to confirm every single deletion. Every single one.
There were other commands that were similarly annoying, and of course these machines, while speedy for their day, still took a bit of time to do things, especially if it involved disk I/O (the disks were 8 inch floppies, of course). So the natural behavior of most people who had typed such a command was to immediately follow it with a long series of quickly typed Y ENTER’s or N ENTER’s and then walk away. The command would read its responses from the filled up keyboard buffer. That was computer automation, circa 1982.
But often the command would run out of questions before the keyboard would empty its buffer. The Y’s or N’s were then fed to TRSDOS itself, which would take great offense and respond with an “ERROR 31” (File not found) message. You’d see the screens filled with these things anywhere Model II’s were unattended.
Well, I didn’t like that message. It annoyed me, and I didn’t think it was good for potential customers to see screens full of errors. Computers were scary enough to most people back then. Now, I could have written something that would feed answers to other programs, but that would have been complicated and no one would have used it. People were used to filling up that keyboard. So I wrote a little program. A very little program. One byte.
That single byte was a hex C9, which was a RETURN in Z80 language. I named it Y, and copied it to N. Now when the keyboard buffer asked TRSDOS to do “Y”, it could. The program did nothing — just returned to TRSDOS, but no error messages were produced.
I later wrote a longer and more complicated version that flushed the keyboard buffer of all input before returning. That was sometimes useful for the case of fat fingered typists or little kids banging away on the keyboard. I’ve forgotten everything I ever knew about Z80 machine language and TRSDOS, but I do remember C9 — the shortest useful program I ever wrote.
I liked raw coding
I don’t do any machine or assembly language coding any more, but I still have tender feelings in that direction. Assembly is fun: you have total control of the machine, and it runs so darn fast you can’t blink or you’ll miss it. You can write programs that are smaller and faster than is ever possible with any other language. Although it looks horribly daunting at first, there are really only a dozen or so basic instructions that you use over and over again. I think there is something quite pretty about listings of assembly code, and I truly miss seeing them in magazines.
Some of the fun things I’ve done with Assembly language include:
- Locking up the machine
- Turning on every light on the keyboard, but not on purpose
- Having the machine reboot
- Locking up the machine totally
- Making awful noises come from the speaker
- Wiping out sections of my hard drive
- Filling up the screen with junk
- Damaging BIOS data
- Locking up the machine totally and completely.
- Locking up the machine totally and completely and without recourse.
- Locking up the machine so totally and completely that I wondered if it would ever work again.
- And every now and then, creating a useful program.
The Z80
My experience with machine language went back to before I worked at Tandy. I was the proud owner of a Radio Shack Model I, a 4K fire-breathing 2 MHz Z-80. Yes, that’s two, not twenty, and certainly not 200. Two MHz. And it was FAST! You may think I’m joking, but this was the days of slide rules and mechanical adding machines. This puppy was a whole lot faster than any of those tools.
The 4K memory was a bit of a lie: it was really 1.9K because more than half of it was BASIC-In-ROM (a mantra from the early days: basic-in-rom, basic-in-rom, basic-in..). Basic was fun, but I wanted more power.
Radio Shack tempted me with ads:
With T-Bug, it is possible to program in machine language on a level I BASIC TRS-80
That was a 1978 blurb for a $14.95 tape you could load into your TRS-80. Although fifteen bucks was worth a lot more then, it was still cheap enough that I popped for it. I also bought myself a copy of How to Program the Z80 by Rodney Zaks. I still have that: it’s dog eared and falling apart but still here. It makes me smile to look at it.
Look carefully at the phrasing of that ad. It said that it’s “possible” to program in machine language. Indeed it is. If you agree that entering “ED 00” for a “LOAD BC” instruction is covered under the “possible” umbrella, then indeed it was possible to write programs. Painful, slow, frustrating, error-prone, but certainly possible. I think it could single-step, but that’s about it. I gave up on T-Bug and wrote myself a really awful assembler in Basic. It didn’t do much, but it let me use mnemonic codes like “LOAD BC, STUFF” and it would figure out where STUFF was.
My next brush with bare metal programming was those Radio Shack Model II’s. Big, 64K, 4 MHz Z-80’s. The OS had a limited batch file language, but you had to type “DO” ahead of every batch file you wanted to run. I found that annoying, so I figured out how to write simple little programs that could run these without having to type “DO”. These had a debugger that was enough better than T-BUG that I could do this kind of thing fairly easily. I did a fair amount of little programs; to this day when I see a hex C9, I think “RET” (Return).
IBM
Then came IBM. Man, when I first ran IBM’s DEBUG, I was in seventh heaven! Imagine: you could disassemble other peoples programs and you could enter mnemonics rather than hex instructions. A good thing that was, because RET was now C3! I was working for myself by then and would take on just about anything. “Have brain, will travel” was my secret motto.
Eventually, I outgrew DEBUG and needed to buy a real assembler. I bought Microsoft’s MASM, which wasn’t all that great at first, but later versions kept getting better and better. Soon you didn’t even have to worry about pushing and popping your registers: you’d just say your subroutine “USES BX” and the MASM took care of the details. Neat.
That was helpful when a client of my consulting business needed to read a punched tape on a DOS machine. He had the reader, but nobody had any code to use it. MASM and I whipped something up and sucked in that data. It was fun watching that punch tape move through the machine.
Zen of Assembly Language
I bought a copy of “Zen of Assembly Language” by Michael Abrash. That was written in 1990 and it’s still a “best seller” on Amazon. It examined the 8088 processor instruction set in mind numbing detail and showed how everything you knew about writing faster code was wrong. I loved that book!
But times change. As fun as all that is (and it really is fun, and mentally stimulating too), it’s also fun to write in Perl, and for the very few times when that was too slow, C was as close as I get to the bare metal. I didn’t have the time to code machine language.
Still, someday, when things slow down and I don’t need to do so much, I’d like to take it up again. Just for fun. Simply because the listings with their perfect columns of repetitive instructions are comforting. The alluring simplicity of the instructions melds with the intense concentration and attention to detail and is almost hypnotic. I remember a conversation with a friend who said “It changes your brain”. I think it does. It can be both relaxing and incredibly stimulating. I’ve programmed in Basic, C, Perl, Cobol: those can be challenging too, but nothing quite has the same kick as raw assembly language.
Here’s one of the last bits of assembly I ever wrote. It was part of an MSDOS C program where C by itself wasn’t quite low level enough. It had something to do with listing a DOS directory way back before Windows.
pusha
call setdta
mov ah,0x4e
mov dx,Wild
mov cx,val
int 0x21
jb no_more
d_loop: mov si,pd
mov ax,[si+30]
cmp al,'.'
jz jfnext
cmp show,1
jz doshow
mov si,pd
mov ax,[si+21]
and ax,Dir_Mask
jz noshow
add si,30
push si
mov ax,Wild
push ax
call savewild
call makename
pop ax
pop ax
push 0
push Any_Mask
push pd
call do_dir
pop pd
pop ax
pop ax
call setdta
call getwild
jmp noshow
no_more: jmp outahere
jfnext: jmp fnext
doshow: push pd
call showit
pop pd
call setdta
jmp fnext
noshow: mov si,pd
mov ax,[si+21]
and ax,Dir_Mask
jnz isadir
add word ptr Total_files,1
adc word ptr Total_files+2,0
mov ax,[si+26]
mov cx,[si+28]
add word ptr Total_bytes,ax;
adc word ptr Total_bytes+2,cx;
jmp fnext
isadir: add word ptr Total_subs,1
adc word ptr Total_subs+2,0
fnext: mov ah,0x4f
int 0x21
jb no_more
jmp d_loop
setdta: mov dx,pd
mov ah,0x1a
int 0x21
ret
showit: push pd
call s_print
pop pd
ret
outahere: popaIsn’t that pretty? Never mind how it works: I can’t remember that now. But it’s still pretty.
I miss it.
If you ever learned assembly, let me know in the comments or, if you have subscribed to receive these posts by email, you can respond privately from your mailbox.
Originally published at https://aplawrence.com. Material also taken from my Some Assemblance of Order. Revised and expanded for Medium
Click to upgrade to full Medium membership. This is an affiliate link. I receive financial incentives for new referrals.
Or you can buy me a coffee here.
More of my posts:






