The code that lived in my head rent free for 30 years
I have a piece of code that has been living, rent-free, in my head for the past 30 years or so.
In middle school (I was 12 - 13 at the time), I was taught Pascal as the entry-level programming language. I found it to be a really fascinating topic, as you can imagine.
One of the things I did was try to read other people’s code and see what sense I could make out of it. That was way before the Internet was a thing, though. Even at that time, Pascal was mostly on its way out, and there wasn’t much code that a kid could access.
To give some context, at the time, if you wanted to move data from one place to the next, the only real option was to physically disassemble your computer, take out the hard disk, wrap it in a towel for protection, and carry it to your destination. I recall doing that and then spending some hours at a friend’s house, trying to figure out the right jumper configuration so the BIOS would recognize two drives at once.
Because of this, I had a great interest in disk parking, a technique that helps ensure that taking the hard drive from one location to the next wouldn’t ruin it. I remember running into a particular piece of code to handle disk parking in Pascal and being quite excited by this. Here I had something that I needed to do, and also in a language that I was familiar with.
I remember staring at the code and trying to make sense of it for a while. I couldn’t figure it out. In fact, I couldn’t even begin to understand what was going on there. I remember feeling both frustrated and stupid because I could understand the syntax but not what it was doing.
In fact, it was so far above my head that I was upset about it for days, to the point that decades later, it still pops into my head. When it came back for a visit this week, I decided to try to figure it out once and for all.
That led to the first problem, which is that I cannot actually recall the code. I remember it was in Pascal, that it dealt with parking the disk needles, and it was full of weird numbers that I couldn’t grasp. Turning to Google didn’t help much, I found some code samples, but nothing that really jived with what I recalled. I ended up cobbling something that more or less matches what I had in my head.
program DiskPark;
function ParkHead(drive: char): Boolean;
var
regs: Registers;
begin
with regs do
begin
AH := $13;
AL := Ord(drive) - Ord('A') + $80;
DL := AL;
Intr($13, regs);
ParkHead := (Flags and FCarry) = 0;
end;
end;
begin
if ParkHead('A') then
WriteLn('Success: parked')
else
WriteLn('Failure, no idea why!');
end.
The actual code that I remember was beefier, and I think that it handled a bunch of additional scenarios, but it had been 30 years, I can’t recall it.
Amusingly enough, I actually had to update my publishing software, since I never thought I would be publishing Pascal code snippets 🙂.
What is interesting is that, as I look at this code and try to view it through the glasses of my 12-year-old self, I can understand exactly the frustration.
Look at this code, it is filled with random numbers and letters. What is AH or the Intr() call? I mean, looking at this, there is no place to get started understanding this.
Let’s look at this line:
AH := $13;
There is no definition of AH anywhere, I can’t tell where it is coming from. And $13, what is that about? A bad luck omen because I can’t figure it out, I think…
The problem was that my 12-year-old self wrote programs like “Guess the number”, and really advanced stuff was bubble sort. I approached the code above with the same mindset. There is a logic to this that I can figure out if I can just stare at this long enough.
The problem is that the code above is basically arbitrary, there is nothing intrinsic about it.
The weird variables that come from nowhere (AL, DL, and AH) are registers (at the time, I don’t believe I was even aware that those existed). The weird numbers are just arguments that we pass to the BIOS when you invoke a command.
In the same sense, consider this code:
return syscall(437, ptr, 525376);
This line will tell you absolutely nothing about the reasoning behind the code. While the numbers seem arbitrary,in this case, they correspond to calling SYS_openat with the flags O_APPEND | O_CREAT | O_CLOEXEC.
The whole program above is just the way you have to go to invoke a system call (as we would call it today), and there is no logic or sense to it. Just something that you have to know. And once you do know, all of this can be packaged into a much simpler, higher-level concept: the system call. (Yes, it is an interrupt invocation, not the same thing. You may go stand in the pedantic corner, mind.)
Hopefully, I wouldn’t ever have to think about this program any longer. I managed to not only understand what it does, but actually grok it.
Comments
Oh how I miss Pascal! Turbo Pascal was decades ahead of its time. I too used to remember every line of code.. until about 2004. I could recall a function name and the code for it would just pop into my head. The sliding window of memory has moved forward though, and the code I wrote pre 2004 is no longer on my head drive. It seems I can hold about 20 years of code in my head… Cheers!
Man...that brings back some memories! I learned Pascal also in middle school from 1982-1985. Just seeing that code listing brought a wave of nostalgia.
I'Ԁ liie to find out more? I'd want tⲟ find out more details.
Hahaha, this sure brings back memories! In High School, I got really in to memory resident programs, sometimes called a TSR (Terminate Stay Resident) and I mostly did that programming in Pascal. In some ways I really miss that language! Anyway, it took me forever to figure out the command that would actually make the program a memory resident one...the Keep command! Thanks for taking me down memory lane on this one. :-)
Ah yes, the good old days. There was this big book of all the interrupt calls and how to use them that I spent a lot of time with. I was working on a C application in small memory model, and to keep the code size below 64K I had to convert many of the C disk I/O calls from cdecl to Pascal to save the bytes cleaning up the stack on each call. (cdecl the caller cleans the stack, Pascal the method cleans the stack.) Too bad I can't find the book, it looks like it would be worth quite a bit now.
Ah, Pascal... my first "real" programming language (I did a bit of BASIC before, but nothing really interesting). I had inherited my first PC from a cousin who did a bit of Pascal, and his programs were still on the hard drive. There was a piece of code that changed the keyboard LEDs status (Caps Lock, Num Lock, Scroll Lock), written in assembler (it was possible to include assembler code directly in Turbo Pascal), and even though I never really understood how it worked, I reused it in a MineSweeper game I wrote to make the LEDs flash when you won. Fun times!
Somewhere I have the "DOS Programmer's Reference" book. Back in the late 80's, I wore that reference to the point where the binding broke and some pages in the middle make out.
INT-13 is the BIOS entry point, much like syscall is for the Linux kernel. There are values you pass it for various tasks, parking the disk head being one of them, I would assume. If I can find that reference and the page that explains the right call, I'll scan and send a copy.
I think the process of disassembling the PC, taking the drive, to share the code was called, "Sneaker Net," as you had to put on your sneakers and walk the code to somebody else's location :)
Weren't the mid-90's drives already using the kinetic energy of the rotating disk to generate enough current to auto-park the heads in case of abrupt power loss?
Also I thought they had a gravity sensors to detect falls and instantly park the heads.
All those AH, DL, etc, are 8086 registers. The "use regs" is telling. That is 8086 assembly code wrapped in Pascal. I managed to do quite a lot of it in high school, back in the nineties!
I ɑm regulɑr reader, how are you everybody? Thiss pieϲe of writing posted at this web page iѕ actually goߋd.
My partner and I stumbled over here different web address and thought I may as well check things out. I like what I see so i am just following you. Look forward to exploring your web page for a second time.
My first experience with assembler was with an IBM 370, which numbered its registers. When I moved to a Z-80, it took a while to get my head around registers having letter names.
Comment preview