Jan 15 2012

Teaching Assembly, Part 2

Category: Uncategorizedzvolkov @ 12:36 pm

Just to dump my thoughts on the subject, and clean the brain buffer for new ideas, I’m writing this post summarizing what I taught my son since the last post:

  • Learned the notion of video memory. Drew a stickman by setting bits in video memory. Made the stickman “move” by erasing and redrawing it with an offset.
  • Made a “font” consisting of binary representation of pixels required to draw letter “A”. Learned to “print” strings consisting of A’s. Discussed what it would take to print arbitrary strings this way.
  • Learned the notion of ASCII code as a way to represent characters in memory.
  • Called CP/M system function to print a $-terminated string.

Now, in our first phase, all programming was done in CP/M’s debugger “SID”, running on an 8080 emulator. The idea was to start with simple non-segmented addressing, understand limitations of 8-bit arithmetic, and overall get used to a highly restricted environment the assembler programmer works in. At some point I realized it wasn’t our primary goal to learn CP/M and decided to upgrade to 8086.

Here’s what we learned on 8086:

  • Learned that DOS command DEBUG — DOS equivalent of CP/M’s SID — still works on Windows XP (and even on 32-bit Windows 7 or so I was told!). Played with DEBUG to learn its slightly different commands and output format. Turns out there’s an improved clone of DEBUG called GRDB.
  • 8086 registers. All 16 bit. Many accumulators. Extra registers.
  • 8086 versatile MOV commands and the [xyz] notation. No need to remember 8080′s LXI/LDAX bullshit.
  • 8086 built-in multiplication and division
  • File system basics. Directories and files. Path. Commands CD and DIR. The process of loading executable file into memory and starting it.
  • 8086 has lots of RAM. Segment addressing. Why segments “overlap” (or, rather, can start anywhere on 16 byte boundary). Tiny model vs. small model.
  • DOS equivalent of CP/M’s print functions (Interrupt 21h, same function #9). Same $-terminated strings.
  • Rewrote a couple of previously written programs (e.g. reversing an array) from 8080 assembler into 8086.
  • Learned to save a program from DEBUG into a .COM file.
  • The notion of source code vs. executable. Compilation. Wrote a “hello world” in .asm file and compiled it to .COM using MASM.
  • A DOS Int 21 function to input a character.
  • Wrote a console game (!) that guesses a number from 1 to 1000 by progressively cutting the range in half.
  • Representation of negative numbers. Two’s complement format.

This was a time when I almost decided that we were done and it was time to move on to C. I even showed Matthew hello world in C and he liked how you don’t need to juggle registers and how printf magically formats strings and numbers. But then I realized: first, I was rushing a bit and perhaps we need to stay on assembler until he gets comfortable writing programs w/o my help. Most importantly, I realized C was not exactly meant for “fun” programming — it is perfect for processing files but less perfect for drawing on screen, or for making sounds, or for developing micro-games, not without using third-party libraries anyway. I really believe I should avoid the libraries as they will certainly overwhelm Matthew (“look these people have already written EVERYTHING there was to be written!”).

And so I this morning I decided to linger on assembler a bit longer. Here’s what we learned in the last few days:

  • The notion of global constants (the strings we print) vs. global variables. Rewrote the game to take advantage of global variables as opposed to holding stuff in registers all the time.
  • The notion of local variables on stack.
  • Intel 80386. 32-bit registers.
  • Modern debuggers: IDA Pro and OllyDbg.
  • FASM (Flat assembler) as a simpler, more modern alternative to MASM. FASMW IDE.
  • The boot process. BIOS vs. OS.
  • Interrupts. Actual (hardware interrupts) vs. interrupts as system API.
  • DOS interrupts and BIOS interrupts vs. directly working with hardware.
  • Keyboard scan codes. Difference between scan- ASCII- codes.

Now I’m thinking we should develop a console game that would involve moving an object on screen with cursor keys. Perhaps such a game as Snake or Sokoban or Arcanoid would work well. Any ideas?

Comments are closed.