The Worst Ideas in Programming

CS 301: Assembly Language Programming Lecture, Dr. Lawlor

Everything has a Name

There are lots of times we accidentally make lists of things using names instead of numbers:
std::string faculty0="Chris";
std::string faculty1="Glenn";
std::string faculty2="Jon";

// todo: print all faculty
// todo: write the list of faculty to the disk

(Try this in NetRun now!)

We've accidentally structured the program to make it impossible to loop over the faculty, so we don't.  We thus write three copies of the print code, three copies of the read code, three copies of the write code, three...wait, we just hired a new guy.  We now have to change all the code that exists.

Sometimes you should have a unique special name.  But often it's much easier to just be a number.

The solution is to make an array of faculty, so it's faculty[0] instead of faculty0.  This means we can iterate over the list of faculty.  We can add faculty and all the old code just works with the new guy.

The same situation shows up over and over again, but sometimes it's hard to solve.

Segmented Memory

It's 1978.  Your new microprocessor has 16-bit registers, but that only lets you address 64KB of RAM, which is just barely not enough.

Instead of making the leap to 32-bit registers, you add a second 16-bit "segment register" used to determine which 64KB of RAM you want to access.  You make the very odd choice to combine segment register and offset into a single pointer address like this:
    address = (segment << 4) + offset

This means hardware address 0xA1234 (in the middle of the VGA screen) might get split up into segment 0xA000 and offset 0x1234, sometimes written 0xA000:1234.

Unfortunately, your microprocessor, the Intel 8086, is a huge success, and is the chip used by IBM for the IBM PC.   Segment registers go on to make programmer's lives miserable all through the MS-DOS and Windows 3.1 era.

What's so bad about segmented memory?  Because in segmented mode:

Segment registers still exist in the hardware, even on a modern 64-bit Skylake box, although operating systems use them for their own purposes, and won't let you use them like you could in the old days.

; Load up the GS segment register with an offset
;  (via eax, because mov gs,constant is not an instruction!)
mov eax,1
mov gs,eax

; Load a byte using the segment register
;  (on modern OS, segment is ignored)
mov al,[gs:where_am_i]

ret

section .data

where_am_i:
	db "abcdefghijklmnopqrstuvwxyz",0

(Try this in NetRun now!)

The full set of x86 segment registers:

Generally, segmented memory is reviled as one of the worst ideas from the x86.

Writing Everything in Assembly

I have a love/hate relationship with assembly language.  No other language gets you so close to the bare metal of the CPU, giving you so much power and control over everything the CPU does.   But no other language gives you so many opportunities to silently get the wrong answer or crash: by miscounting bytes, using the wrong register, or operating on the wrong types. 

Compared to a program written in a high-level language, a program written in assembly language is:
Don't do it!  Like nuclear weapons, assembly is a powerful tool to have in your toolbox, but not the right solution to every problem.   I personally get a lot more useful work done by reading assembly than writing it.


Other bad ideas: