Monday, April 29, 2013

How I coded in 1985

Back in 1985 I worked on the computerization of a machine designed to stick labels on bottles. The company that made the machines was using electromechanical controls to spool labels off a reel and onto products (such as bottles of shampoo) passing by on a conveyor. The entire thing needed to work with mm accuracy because consumers don't like labels that aren't perfectly aligned.

Unfortunately, electromechanical controls aren't as flexible as computer controls and so the company had contracted a local technical college (where I was studying electronics) to prototype computer control using a KIM-1. Another student had put together the machine with a conveyor, a mechanism for delivering the labels, control of stepper motors, and infrared sensors for detecting labels and products.


My job was to write the software in 6502 assembly. Unfortunately, there wasn't an assembler and the KIM-1 just had a hex keypad and small display. So, it meant writing the code by hand, hand assembling and typing it in.  The code looked like this:


It was immediately obvious that computer control was going to be more flexible. The program first did automatic calibration: it measured the length of labels on the spool itself, it measured the distance between labels itself and it enabled an operator to quickly set up the 'overhang' distance (how much of the label is sticking out so the product can catch onto it.


While running it could automatically detect how fast the conveyor was moving and compensate, and spot when a label was missing from the supply spool (which happened when one peeled off by accident).

Of course, writing code like this is a pain. You first had to write the code (the blue), then turn it into machine code (the red) and work out memory locations for each instruction and relative jumps. At the time I didn't own a calculator capable of doing hex so I did most of the calculations needed (such as for relative jumps in my head).

But it taught me two things: to get it right the first time and to learn to run code in my own head.  The latter has remained important to this day. I continue to run code in my head when debugging, typically I reach for the brain debugger before gdb or similar. On the KIM-1 there were only the most basic debugging functions and I built a few into the program, but most of the debugging was done by staring at the output (on the hex display), the behaviour (of the steppers) and by running the code through in my head.

Here's the full program for the curious.

PS A number of people have pointed out that in 1985 the KIM-1 was far from the state of the art and we had lots of niceties like compilers etc. True. In fact, prior to this I had been programming using BASIC and ZASM (Z80 assembler) under CP/M, but you go to war with the army you have: the technical college had a KIM-1 to spare, it had good I/O and it thus it made a fine prototyping system for an embedded controller.

If you enjoyed this blog post, you might enjoy my travel book for people interested in science and technology: The Geek Atlas. Signed copies of The Geek Atlas are available.

52 Comments:

Blogger Stephan.Schmidt said...

A friend of mine wrote games like this without an assembler on his Sharp MZ-800. Mostly they were bug free on first run. I was amazed.

12:40 PM  
Blogger Tom Greenhaw said...

Brings back old memories. Makes me want to dig through some old boxes in the garage with some of the microcode stuff for bit slice processors.

Also reminds me of how we walked to school 10 miles away in snow :-)

1:18 PM  
OpenID porneL said...

Text editors with syntax coloring are older than I expected ;)

1:44 PM  
OpenID Nix said...

Running code in your head is perhaps the most crucial of all skills for a software developer. Amazingly, it's not universal: before I started working for a major software firm, most of the software developers I worked with in my day job could *not* do it. They had to actually run things before they could see what would happen: they couldn't emulate the abstract machine in their heads well enough to see what it would do. I have no idea how anyone could write anything while so hobbled.

1:49 PM  
Blogger Alex Evans said...

Assembling the code in your head and manually entering it in via a hex key pad. That's simultaneously disgusting and something I must try for myself. For the learning experience, of course.

1:57 PM  
Blogger Alex Evans said...

Assembling the code in your head and manually entering it in via a hex key pad. That's simultaneously disgusting and something I must try for myself. For the learning experience, of course.

2:01 PM  
Blogger Matthew Persico said...

Makes me wish I had saved my first program in HS two years earlier than this: blackjack in Basic, with card drawing characters on a Commodore PET. I had a ton of looseleaf too. And this was before I learned about functions; it was probably 50% gotos! LOL.

2:47 PM  
Blogger Unknown said...

cmon, we had access to Cray X_MP supercomputer and Sun workstations in 1985

2:50 PM  
Blogger Andyr said...

Hey I have one of these. Mine is mounted on a peace of wood with a handle, sort of early version of laptop.

2:55 PM  
Blogger EmptySet said...

I used to write my Apple ][ code by hand like this during recess with notebook paper. Computer time was hard to come by at my school, so when you actually got some time, it was all business. :) I probably wrote better code as a kid as a result.

2:55 PM  
Blogger Prescious Cheeks said...

I thought we were all doing that in 1985. I was, on my Vic-20, and then C=64. Or an Apple, or Trash-80, or whatever you had. It was a lot easier if you had an assembler, you didn't have to do the hex conversions or branc calculations by hand. I think I punched in my first assembler from Compute! My first real machine code program was a high res graphics program that would draw a line with mad speed between x1,y1 and x2, y2.

I still have printouts of disassembled code, where I'd do something else... try to crack a copy protected program on the Macintosh, alter. Following all the branches with colored arrows across multiple pages, figuring out what was going on, all from my 6502 skillz, on 68K code. And finally finding what to change to neutralize the licence check, and licence my BBS software (Firstclass) for unlimited user accounts, connections, etc.

2:57 PM  
Blogger Wildgoose said...

That brings back memories.

Although I'm surprised you were using a Kim-1 as late as 1985, I remember playing with one of these around 1980 or 1981.

2:58 PM  
Blogger peter303 said...

Lots of demos at the Stanford Linear Accelerator Homebrew Computer Club in the mid-1970s were from machine instructions entered by dip switch. Until a couple of guys came along and attached a keyboard and BIOS (Apple-1).

3:03 PM  
Blogger sırrı baklagil said...

Hello,

Really thank you for the article, it's awesome. So, may I put that article in my website?


Thanks again.

hostgator hosting coupons

3:06 PM  
Blogger Paul Coy said...

Bought my KIM1 in July 1976. I still have it somewhere. I added a "TV-Typewriter" and keyboard to it. I think it was 40 characters by 16 lines. I'm pretty sure there were assemblers for it but they cost an arm and a leg. Neither was I willing to give up.
Thanks for the article.

3:48 PM  
OpenID kmoser said...

I've got tons of documents like this showing how I programmed in 1979

5:48 PM  
Blogger Jose Dinuncio said...

How nice!

I remember that was the way I used to program for the Atari 800XL. I made the listing in paper and the converted it to DATA or strings:

500 DATA 114, 25, 83, ...

11:46 PM  
Blogger liet said...

Seconded

1:25 AM  
Blogger liet said...

This comment has been removed by the author.

1:26 AM  
Blogger liet said...

Seconded

1:35 AM  
Blogger DR said...

When I was in college I used to input programs like this into the computer that controlled the graphite furnace on the atomic absorption mass spectrophotometer that I did trace metal analysis on. I also sometimes spent entire day's trying to calibrate the wavelength and slit settings so I could get reproducible results on the the darn thing. Occasionally, I would have to adjust the programming to burn off the remaining matrix in the cleaning cycle at the end of each program. I would sometimes work all night if things were clicking so I could get through as many samples as possible before things got back out of calibration. Oh, and the programs I input with a similar keypad were much shorter and simpler than this.

2:08 AM  
Blogger HamiMiami said...

It wasn't just assembler. I've still got a pack of COBOL coding sheets if anyone needs one :-)

2:22 AM  
Blogger Bogdan Zamfir said...

Good old days.
It reminds me of my first big program: a program for 3D graphics written in Basic for Sinclair Spectrum. Of course it was full of goto, Spectrum basic had a very limited support for procedures, and all variables were global (there was no concept of local variables). I wrote the program in about a week on a B5 notebook, and entered it in computer in a couple of days. The 3D objects were entered by cordinates. And I was amazed when I finished the program, and then entered coordinated for a Zeppelin - about 2-300 vertexes. And when I start pressing left/ right arrow, and I saw the thing redrawn rotated! It was really amazing

5:49 AM  
Blogger Wayne Borean said...


Friend of mine desperately wanted to learn Assembler on the Commodore C64, but couldn't afford an Assembler, a Data Cassette, or a 1541 Disk Drive.

Being a stubborn sort, he wrote out an Assembler in a notebook that was short enough that he could type it in, then use it.

Inspired piece of programming in my opinion. Of course in the summer, his machine would overheat before he finished typing it in, but that's life...

Wayne

10:13 AM  
Blogger Rob said...

I wrote lots of Z80 assembler in 1985 and most of it did work first time - of course the code tends to do one simple thing at a time so it makes it easier to test.

10:35 AM  
Blogger Crank-on-a-Bike said...

Very nice article. Base skills like this go a long way.

11:50 AM  
Blogger Crank-on-a-Bike said...

Very nice article. Base skills like this go a long way.

11:52 AM  
Blogger Crank-on-a-Bike said...

Very nice article. Base skills like this go a long way.

11:52 AM  
Blogger Crank-on-a-Bike said...

Very nice article. Base skills like this go a long way.

11:52 AM  
Blogger David Shillito said...

Around the mid-70s one of my jobs involved creating a debugger for a Prime 100 mini-computer. I had to write the thing, hand assemble it as the original poster here did, toggle it in through the front-panel switches and debug (the debugger) with techniques such as placing halt instructions at various locations and looking at the registers in lights until it worked well enough to punch itself out on the paper-tape writer.
The machine would have lost the program unless I punched it out so I had to complete all of this in a single session. As I recall it I managed it a session of several hours. The resulting program was probably only 1000-2000 bytes or so - such programming was very economical compare with today's multi-megabyte EXEs.

Still coding for a living today, aged 62.

12:05 PM  
Blogger Low Limit said...

Is this just page 1 of 100 pages of assembly language? In 1985 I had an Atari 800 which would assemble 6502 via the assembly language ROM cartridge(!)--the idea of hand converting asm to machine language makes my head ache, but I'm not a big fan of needlework either.

12:24 PM  
Blogger Vellanova said...

Hmmm - surely there was a assembler for 6502 available on some platform you had access to. And if there wasn't, you probably could have saved a great deal of time by writing a rudimentary one.

1:53 PM  
Blogger Paul Glover said...

I recall how to "dry run" a program on paper being part of a computer studies class in high school (before CS became "how to use MS Office"), but doing a lot of coding in my spare time prior to this I'd already picked up that concept and also the ability to step through sections of code in my head.

Indeed this is how I first learned to code in Z80 assembler on a ZX Spectrum. The program was handwritten and I did the hand-assembly thing counting on my fingers when I needed to calculate the operand for a JR (jump relative). There was no debugger and no system monitor, either the code did what you expected, or it misbehaved and you had to figure out what went wrong by "running" it on paper or in your head and comparing that to what you saw happen.

I often still code on paper today, or at least express what I'm trying to do in a sort of pseudo-code fashion, and often find myself mentally stepping through the final code before I run it.

3:51 PM  
Blogger Nathan Pfluger said...

I wonder if that is what helps me so much.

My memory of the first time that I realized a lot of people don't have that skill was in college when I was taking and TA'ing an assembley language class.

Helping some classmates, I could debug their 68000 ASM in my head and point out the flaws. They were so amazed that someone could do that. [Not that there is much to running a 30 line ASM program in your head]

4:09 PM  
Blogger Davionics said...

Cool story. Did just a bit of assembly at one point. And you didn't even have an assembler.

I think the 'in your head' thing naturally happens after years or decades of programming - more for some, less for others. But if it doesn't, I wouldn't sweat it too much. I think there's a far more crucial skill programmers need: the ability to design. But, you say, you're just a guy pounding code in a cubicle. The designer sits in a windowed office somewhere. Still you're designing at some level. One of the biggest problems is code written to designs that are unclear or murky. If the design is not right the code can't be either, regardless of how good a programmer you are.

So I always say: 'get the abstraction right and the code will follow'.

4:28 PM  
Blogger Walter Henry said...

This brings back some memories, I learned how to program by copying programs written in Assembly onto an Atari 400 computer and then debugging where my copying went wrong. The debugging part is where I learned to most as I would look up what each step did, figure out what the value needed to by and why, etc. That was around 1979'ish

4:40 PM  
Blogger David said...

In 1985 I was programming games in Z80 and 6502 using a Tatung Einstein (CP/M, 40 column computer with Amstrad 3" disks) running the Zen assembler for Z80 and one I'd written for 6502. Yes I learned Z80 by writing a 6502 assembler in it!

Nice to see 6502 asm again. I once wrote a 400 byte version of Life for the Vic-20 in hex and swore never again.

5:56 PM  
Blogger Dan Sutton said...

That's right! That's right! I used to do things like that back then, too. I think all programmers should spend a good amount of time learning this type of thing - machine code, little crappy computers and so on: you're not a real programmer until you know how to do this stuff. It builds a bond with the machine that no amount of object-oriented, high-level language development will ever form.

6:53 PM  
Blogger Born to Program said...

Debugging in your head. Yeah, when I was doing mainframe assembler programming I used to get yelled at because I'd keypunch my own programs. The accepted methodology at the time was to write the programs out by hand onto coding sheets to be given to the professional keypunchers.
I rebelled because it was bad enough chasing my own bugs, let alone keypunching errors by someone else.
Many a time I'd be typing in the program I had written partially out and I'd realize what I had written down had no chance of working.

7:38 PM  
Blogger Adisak said...

This is exactly the same way I wrote my first published video game. I wrote the asm on notebook paper in my spare time (i.e. while pretending to listen in class), hand compiled the assembler to 6502 opcodes, entered the hex codes into the Apple II hex monitor (CALL -151) and did a BSAVE to write the binary range to a file.

8:19 PM  
Blogger Steve Goodenough said...

Great to see an article about the KIM-1. After seeing it on Tomorrows World on BBC1 (UK) I wrote asking for more information, they replied with the London address of Commodore. Was ready to buy one then they announced the PET, which I ended up buying instead in 1980.
Brings back happy memories of coding in Basic then dropping down to 6502 assembler for performance.
It got me my first programming job in 1984 so a great investment I say.
Oh yes, my PET still works, not bad for 33 years, but assembler was left many years ago.

9:54 PM  
Blogger TK said...

I wrote my first program in a similar manner in 2001 :)

10:55 AM  
Blogger Greg V said...

I have sat through some coding in a couple of classes and I think it is mindbogglingly on what all of you do. Keep up the good work so I won't have to (or at least try to).

12:55 AM  
Blogger Duncan Gunn said...

I remember being spoilt by my mum and dad as they bought me an Acorn Electron which had a built-in assembler. TBH I spent an awfully long time writing programs that picked it's ROM apart, prevented the [BREAK] old program recovery and programs that simulated physical phenomena. Never did owt really hard on it though.

9:47 PM  
Blogger Duncan Gunn said...

I remember being spoilt by my mum and dad as they bought me an Acorn Electron which had a built-in assembler. TBH I spent an awfully long time writing programs that picked it's ROM apart, prevented the [BREAK] old program recovery and programs that simulated physical phenomena. Never did owt really hard on it though.

9:48 PM  
Blogger Neil Cherry said...

Really now, by hand? We only did that in school. ;-)

Okay, actually that's kind of true. In 1985 I recall writing a funky 2-pass (okay 1.5 pass) Z80 assembler in Atari BASIC and stringing up an EPROM burning (shift reg' and transistors) on the game ports. It was easier than doing the whole thing by hand. But I also needed to work with more than 7 pages of code (it fit in 4K). I never did print it out (I had no printer). Shortly after that I began learning this new high level language called C. Wow! ;-)

Ah yes, the good old days, boy am I thrilled to have lived through it but I much prefer today with the nice Arduinos and Pis.

Now get off my lawn!

11:08 PM  
Blogger 542516 said...

Yes please!

11:37 PM  
Blogger 542516 said...

Yes please!

11:38 PM  
Blogger Joe Nelis said...

I have a file full of KIM-1 hand assembled code from back in 1977 that looks just like that. I never did a business application like this - it was all hobby stuff. Taught myself computers and programming with that KIM-1 and hand assembled 6502 code. Now, every 'more modern method' that I use to write programs is a piece of cake. Devices like the Raspberry Pi boards that are available today are much more affordable and have much more capability and could probably provide nearly the same educational value, but only if you use a low level language and get to where you can fully exploit the I/O.

1:12 AM  
Blogger darrellp said...

I remember doing the same and then having to input the data using the paddles on the front of a DEC mini-computer. Slow going.

4:39 PM  
Blogger Performance Blue ST220 said...

I wanted to learn about computers around 78/79 so built a computer based on the S100 bus with Z80 processor board I/O, memory board etc and Kansas city cassette backup, Couldn't afford an assembler so coded in mnemonic and converted to hex manually. A thousand byte bull and cows program taught me well.
Like the DEC guy above I used to work on a system from ABS computers and would enter diags with "paddles" 77 10 02 and all that.

9:02 AM  
Blogger Performance Blue ST220 said...

I wanted to learn about computers around 78/79 so built a computer based on the S100 bus with Z80 processor board I/O, memory board etc and Kansas city cassette backup, Couldn't afford an assembler so coded in mnemonic and converted to hex manually. A thousand byte bull and cows program taught me well.
Like the DEC guy above I used to work on a system from ABS computers and would enter diags with "paddles" 77 10 02 and all that.

9:03 AM  

Post a Comment

Links to this post:

Create a Link

<< Home