Wednesday, January 21, 2009

A Dose of Machine Code

I want to let you in to the mysterious work that hardware does when it fetches instructions... (e.g. runs your software) There is a register called the IP register on Intel compatible chips such as the Core2Duo and AMD Athlon. It is called the (IP) Instruction Pointer because it will always point to the instruction that is to be executed. For instance (in Assembler):

B7FD78098 MOV [Cnt1], 5

This previous instruction is said to be stored at the B7FD78098 address in your RAM. The mnemonic for "MOV [Cnt1], 5" can be directly translated to machine code, which is usually edited in hexadecimal code (just like the address space). If this wasn't the case, then that address would look like this:

101101111111110101111000000010011000

Which number base would you like to work with? As a side note, I chose this number assuming the computer that is running this code has at least 50GB of RAM. But regardless of the fact, the IP points to this address, executes what's on that space, and then increments to the next instruction. It is very linear and it is all part of basic computer architecture.

If computers are so linear, how do they implement more intelligent structures? Well by jumping of course! There are 30+ separate jumping commands on hardware that used to run DOOM (80x86) and I'll show you one just below.

mov [cnt1], 5
loop_label: nop
dec cnt1
jnz cnt1, loop_label

The first line moves the value of 5 into our variable, cnt1. The second line is nothing but a label... labels are usually mnemonics for an address space similar to what I described above. So at this point, we are in a loop, but the computer doesn't really "know" this. NOP is an instruction that means NO OPERATION so it does nothing. DEC is an instruction that decrements the variable by 1 (each time). JNZ is one of our jumping instructions that will jump to the label if cnt1 IS NOT ZERO. So as you can see, the logic is very simple even though the jump is backwards... essentially, if cnt1 IS NOT ZERO, then the IP register is loaded with the address of loop_label. Once this happens, the computer will continue to execute from that new location. Once the counter is ZERO, the condition will fail and the instruction after JNZ will execute.

Questions?