View Single Post
Old 03-15-2006, 11:31 PM   #8 (permalink)
TheHeadFL
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

A compiler takes a high level language like C or C++ or whatever and breaks it down into assembly language instructions.

For instance

Code:
x = 2;
x++;
if (x < 5)
{
   x+=2;
}
else
{
   x-=2;
}
x = x * 2;
Would generate this assembly (approximately, MIPS assembly)

Code:
  ADDIU A0, R0, 2
  ADDIU A0, A0, 1  
  ADDIU A1, A0, 0
  SUBIU A1, A1, 5
  BGEZ A1, label1
  ADDIU A0, A0, 2
  J label2
label1:
  SUBIU A0, A0, 2
label2:
  MULIU A0, A0, 2
  SB A0, x
For reference, SB = Store Byte, ADDIU = Add Immediate Unsigned, BGEZ = Branch on Greater Than or Equal to Zero, J = Jump. Register R0 always contains zero.

Values are assigned to registers by way of adding the contents of R0 and that value, and storing the contents in the destination register.

Anyway, those instructions all contain a binary representation...

ADDIU might specify a 6-binary bit code, maybe 000100.

Lets say we have the instruction ADDIU A0, R0, 2

We build the machine code:

ADDIU = 000100
A0 = 00010
R0 = 00000
"2" = 0000000000000010

On a 32 bit machine, this instruction is represented as (actually its in reverse, but for illustration we won't reverse it):

Code:
00010000010000000000000000000010
ADDIU   A0   R0     Immediate

In hex:
10400002
This is the code that is sent to the CPU to be executed.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline