APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Some Assemblance of Order

© September 2003 Tony Lawrence

I don't do any machine or assembly language coding any more, but I still have tender feelings in that direction. Assembler 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 assembler code, and I truly miss seeing them in magazines.

Some of the fun things I've done with Assembler language include:

My experience with machine language goes back to TRS-80 days, when 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. 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 that, 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: basicinrom, basicinrom, basicin..). 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 then it is now, it was still cheap enough that I popped for it. I also bought myself a copy of the The Z-80 Microcomputer Handbook by William Barden. I still have that: it's dig 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 ac 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 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 just 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).

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!

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. Eventually you didn't even have to worry abouut pushing and popping your registers: you's just say your subroutine "USES BX" and the MASM took care of the details. Neat.

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 is too slow, C is as close as I get to the bare metal. I don'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. Just because the listings with their perfect columns of repetitive instrictions 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 assembler I ever wrote. It was part of an MSDOS C program (An alternative to DOS "dir"):

          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
showit:   push    pd
          call    s_print
          pop     pd
outahere: popa

Isn't that pretty? Never mind what it does: I can't remember that now. But it's still pretty.

I miss it.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Some Assemblance of Order (machine and assembly language programming)


Inexpensive and informative Apple related e-books:

Take Control of High Sierra

Take Control of Pages

Photos: A Take Control Crash Course

El Capitan: A Take Control Crash Course

Digital Sharing Crash Course

More Articles by © Tony Lawrence

"My next brush with bare metal programming was 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."

Didn't that machine come with CP/M? I seem to recall that CP/M was advertised by Tandy as a "feature." I always thought that was strange, seeing as how an operating system is really a necessity like drinking water, not a feature like GPS in a new automobile.

"The alluring simplicity of the instructions melds with the intense concentration and attention to detail and is almost hypnotic."

Even more hypnotic is what happens on the screen when you make a major goof in your code. About everything you can imagine, mostly unintelligible, pops up in brilliant colors -- that is, until you hit the reset button.

In 1982, I bought the brand new Commodore 64, which like the Trash-80, came with a built-in BASIC interpreter. It didn't run fast enough to suit me (in those days, I ran an NHRA B-altered race car, so everything else seemed kind of slow at the time). So I purchased Commodore's Macro Assembly Development System, better known as MADS. If you used it long enough, you probably were in danger of going mad.

Once of the aspects of 65xx machine language programming that I always appreciated was its utter simplicity. The 65xx processors were sometimes referred to as the original RISC processor, due to the simple nature of their instructions, as well as the CPU's pipelining feature. As Tony eluded, you tended to use the same 10 or 12 instructions over and over. However, since the 6510 processor in the C-64 had 56 instructions, I was determined to somehow use all of them -- which I think I eventually did. Some instructions like ASL, LSR and ROL were bit twiddlers and I had to contrive uses for them. Others like BIT were just plain strange (BIT merely sets processor flags -- it doesn't actually do anything else), but turned out to be real handy -- you can use BIT to test for negativity without altering the contents of a register. Ah, the good old days!

Anyhow, as long as machine code is being dragged out of the past, here is some parting code in the one assembly language that I truly grok:

;add floating point accumulators
dadd pha ;save registers
sed ;set decimal mode
clc ;clear carry
ldy #size_of_dnum-1 ;length of a decimal number
dadd01 lda acum1,y ;get from accumulator 1
adc acum2,y ;add to accumulator 2
sta acum1,y ;save result
dey ;step index
bpl dadd01 ;do it again
cld ;revert to binary mode
ply ;recover registers
rts ;return to caller


"Didn't that machine come with CP/M?"

No. Completely incompatible - BIOS in the very wrongest place..


I just stumbled on this: https://www.linuxgazette.com/issue94/ramankutty.html

Must be something in the air..


Dunno. But when you consider how fast our hardware has become, will M/L ever make a comeback? After all, to save time by using M/L you have to expend time to write the code. I just can't imagine anything I'm doing at this point in time where I'd feel a compulsion to fire up an assembler again.


For those who thing several dozen instructions is too much, how about a language with exactly ONE instruction?

https://www.catb.org/retro/ and look for oisc


"I just can't imagine anything I'm doing at this point in time where I'd feel a compulsion to fire up an assembler again."

Guess I'll have to eat my words. Out of the clear blue, I received an E-mail from someone who had built a parallel printer interface for his Commodore 64, referring to an article I wrote years ago for the now-defunct "Transactor" magazine. The project I described in that article was for a C-128 (think C-64 on steroids) and included a driver package written in 8502 machine language. This guy wanted to know if I could port the C-128 driver to the C-64.

Anyhow, to keep the story short, there is now a C-64 version of the printer driver, 100 percent M/L, of course. I'm amazed that I remembered any of that stuff, especially the bit-wise parts. It did take me a while to recall how to get the assembler to work. Goes to show how strange the human mind can be.



Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

The danger of computers becoming like humans is not as great as the danger of humans becoming like computers. (Konrad Zuse)

Linux posts

Troubleshooting posts

This post tagged:




Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode