Danger Computer

The danger computer

Articles tagged: code

Piano.a86

posted by: Gar
filed under: code history

My super brilliant coworker Philip showed us a neat javascript sound generator he was playing with (source here) and it reminded me of one of the very first assembler programs I ever wrote. I hunted down the source code, and here it is.

.radix 16
org 100
        jmp short start

intro   db  0d
        db  'Piano! by GeorgeCo.'
        db  1a
notes   dw  0eee, 0e18, 0d4e, 0c8e, 0bda, 0b30, 0a8e, 09f8
        dw  0968, 08ed, 0862, 07e8, 0777, 070c, 06a7, 0647

start:  in      al,61
        and     al,0fc
        out     61,al
        xor     ax,ax
        int     16
        cmp     al,1b
        jz      done
        in      al,60
        and     al,0f
        shl     al,1
        cbw
        mov     bx,ax
        mov     ax,notes+[bx]
        out     42,al
        mov     al,ah
        out     42,al
        in      al,61
        or      al,03
        out     61,al
        mov     ah,86
        mov     cx,0001
        mov     dx,6000
        int     15
        jmp short start
done:
        ret

I'd love to walk you through this.

First here's a quote that I'm sure was the inspiration for this app, though I can't remember for sure. It's from my copy of the MASM bible for MASM 5.1

The IBM PC's speaker can be used to generate a tone by programming it via the 8255 chip at port address 61h and using the system timer (Intel 8254 chip) to control the speaker. You first set up the timer as an oscillator by sending the data byte B6h to the port 43h. Then you have to compute the ratio of the frequency of sound you want and the frequency of the timer's clock (1.19 MHz). This value is written to port 42h. The 8255 chip is then told to drive the speaker under the control of the timer. You can do this by reading port 61h and writing the value back with the first two bits set to 1 (performing a bitwise-OR with 3). This gets the sound going. Now you must wait as long as you want the sound to continue and then shut the speaker off by reading port 61h again and setting bots 0 and 0 to zero.

As you will see, this is almost 100% the path I took. I never ended up sending byte B6h to port 43h. I don't know why.

Here we go...

.radix 16
org 100

Assembler boilerplate, setting the .radix, or default base of bare numbers, to 16 (hexadecimal). Also sets the starting point of the app at 100 (hex, which is 256 decimal). This was the default for .com files. As such, numbers here are assumed to be hex unless specified otherwise (i.e. int 16 is really 0x16h aka 22 decimal)

jmp short start

intro   db  0d
        db  'Piano! by GeorgeCo.'
        db  1a

For some reason I was really into making it so if you were to type (DOS equivalent of cat) my program out it would show you what it was in plain English without any weird character codes. My .com files always started in this way, with a jump around a newline (which would move the cursor back to position 0, overwriting the bytecode for the jmp) and then a quick string describing the app (and sometimes usage info) followed by the EOF character 1a, which would cause type to stop output of the file.

GeorgeCo was what I was going to call my software company which obviously never became a real thing.

notes   dw  0eee, 0e18, 0d4e, 0c8e, 0bda, 0b30, 0a8e, 09f8
        dw  0968, 08ed, 0862, 07e8, 0777, 070c, 06a7, 0647

These were the precalculated values for the 16 notes (two octaves) I wanted to play. They were based on the a440 pitch standard. As we already know, the numbers were the ratio of the sound you wanted and the frequency of the timer's clock (1.19 MHz).

start:  in      al,61
        and     al,0fc
        out     61,al

Port 61 was the PPI (Programmable Peripheral Interface) port B on PC/XT systems. Bits 1 and 0 were the gate and data controls for the PC speaker (the little speaker inside the tower that usually beeped when you turned it on). Setting these both to 0 ensured that the PC speaker was off.

        xor     ax,ax
        int     16

Interrupt 16 was for accessing keyboard services. Calling it with ah set to 00 called the 'read (wait for) next keystroke' service. Once the interrupt returned al contained an ascii character. If al was 00, ah contained an extended ascii keystroke. In this app, the interrupt was being used for its blocking functionality only, A subsequent in call was made to get the scan code directly from the keyboard once the interrupt returned.

        cmp     al,1b
        jz      done

If the key that was pressed was escape, jump to the end of the program (and exit)

        in      al,60
        and     al,0f
        shl     al,1
        cbw

Port 60 was the PPI port A which was the most recent scan code from they keyboard hardware interrupt. It was then bitmasked with 0f and doubled (because it will be a pointer to an array of words not bytes), using cbw to expand al into a signed word in ax. Since al could never be less than 10 I have no idea why I didn't just clear ah out instead of using cbw. I'm sure it was a weird optimization thing I've forgotten.

        mov     bx,ax
        mov     ax,notes+[bx]

Here we see ax being used as an offset pointer to the array of numbers in notes. The scancode we read from the keyboard has now been used to lookup and load one of 16 numbers from that array.

        out     42,al
        mov     al,ah
        out     42,al

To set the frequency of sound you want, you wrote to port 42.

        in      al,61
        or      al,03
        out     61,al

Turn the speaker back on, now at our frequency selected via the keyboard scan code.

        mov     ah,86
        mov     cx,0001
        mov     dx,6000
        int     15

This was the BIOS wait function. It went into an interrupt-enabled waiting period of cx,dx milliseconds. This was a nice short 'blip' duration I ended up with.

        jmp short start

Do it all over again, go back and wait for a keyboard input.

Well I hope you liked this little trip down memory lane. Thanks for reading.

Your Code Isn't the Most Important Part of Your Project

posted by: Gar
filed under: code

I wrote a blog post over on the &yet blog about documentation. Go check it out.

Ditching docker and gitlab

posted by: Gar
filed under: indieweb code

Last week I migrated off of docker. I also moved my repos off of my local gitlab instance back to github. The reason for both of these was the same: everything was broken. I knew there was trouble on the horizon several weeks ago when I tried pushing to one of my instances and it failed. The fix took trawling through dozens of identical issues only to find that there was a disconnect between the ubuntu version of my server and the one that docker was expecting to exist. To be fair, a lot of the problem lies with dokku, but really all I wanted was a heroku-type setup I could run on my own box. I could not get things working again and am not really feeling up to putting the amount of effort that would be required in order to get things working.

I have no idea why my gitlab instance stopped working, I suspect it is something similar between the confluence of docker requiring constant maintenance and gitlab being fragile to set up to begin with.

This blog is still using bumble, and I'm still running that on my own box, so there is at least that consolation.

This really feels like an indieweb failure, partly because I was not willing to put in the effort, and partly because I think the level of effort being asked was too great. I look forward to any advances on this front but until then I'm just running my node instances on localhost and forwarding nginx to them manually, with only a few lines of config. There is no automation: if things crash or reboot I have to start everything up again. However this feels good enough for now, even if it's not ideal.

As far as post-mortems go this one is pretty light, but then again the reasons for failure are still a mystery to me so I don't have much more to write about other than the fact that I've gone back to a different setup.

Welcome to the gar zone

posted by: Gar
filed under: code indieweb

I recently registered the domain gar.zone with the intent of turning it into my own personal url shortener. Thanks to this helpful blog post by Nuno Job I found a good launching off point called tiny. I patched it up, fixed a bug, and am now using it to power my own url shortener. Yay. It doesn't do analytics or anything fancy like that but it works for me! Source is here

You can see it in action here

LevelDB backend for hastebin

posted by: Gar
filed under: code

level is awesome. It works in node too which is awesome (check out level-up. I wrote a level backend for hastebin cause I wanted to use it for artifice.

That's about it. I think I've fleshed out enough bugs to submit my patches back to hastebin to see if they get merged. That'd be just neat.

Well, see ya. Oh wait I forgot to link to my repo artifice source is here.

p.s. I was too lazy to try to port my existing pastebins over so I guess those links are gone, but that's pastebin for you.