|
File Assembler.html Author McKeeman Copyright © 2007 index Assembly of CPU InstructionsAssemblersMachine code is patterns of bits laid out in executable memory. For example, the following snippet of xcom output.
>> xcom -asmDump x:=1
x86 code dump, 24 bytes code=0xe754220
5589e58b7508b801000000898600000000b800000000c9c3
x := 1
One can, in principle, construct such bit patterns by hand.
In fact, more mnemonic forms are presented by an assembler,
software typically prepared and maintained by the manufacturer.
Compilers commonly output a text file containing the mnemonic
instructions and leave the rest up to the manufacturer. Such
output might look like this:
>> xcom -asmTrace x:=1
Asmx86: 1 pushR EBP (save x86 frame pointer)
Asmx86: 2 movRR EBP,ESP
Asmx86: 4 movRP ESI,8 (point at X frame)
Asmx86: 7 movRC EAX,=1=0x1
Asmx86: 12 movMR 0,EAX (x)
Asmx86: 18 movRC EAX,=0=0x0 (no error)
Asmx86: 23 leave
Asmx86: 24 ret
Passing the buck to the manufacturer works,
but is rather slower than could be achieved by more direct means.
Besides, the approach of this course is to do it all.
Smart AssemblersIt is possible to implement an assembler that does not do exactly what it is told. The assembler, as an optimizer, can apply the as if rule, actually laying out code that has the same effect as demanded by its input, but choosing more efficient code sequences. Peephole optimization is one example of this kind of "smarts". Intel x86The x86 line of Intel CPUs is a CISC design; it is the most successful general purpose computer chip ever manufactured. It is remarkable that one design has evolved from a 4-bit uni-processor to a 64-bit multi-processor, largely keeping backward compatibility at each stage. Here is a description the small part of the Intel x86 that is actually used by xcom. That is not to say that the x86 is elegant, or easy to use, or efficient. The dominance of the x86 design has led to much documentation and lamentation. ExecutionThe xcom system has its own x86 assembler. It is an object. It has a method for each necessary instruction (many fewer than available in the full hardware). Each method builds and places corresponding bit patterns into an internal array. When the translation is finished, and the array is full, the xcom assembler offers one more method which immediately executes the bits. This is load&go. When one needs more instructions, the assembler needs to be changed. The xcom system has its own 32-bit x86 instruction-by-instruction emulator. It walks the assembler output, emulating each necessary instruction The emulator can either zip through the compiled code, printing two lines per instruction executed, or it can be run interactively, stepping forward and backward in the instruction trace. When one needs more instructions, the emulator needs to be changed. ReferencesFor quick reference, it is often enough to look at: Historically important documents are made available by The official documents from Intel:
Other CPUsOver the years, the author and students of the xcom system have prepared subset assemblers for the IBM S/360, Digital VAX, Digital Alpha, Motorola 68000, and Intel 486. It is a necessary step in moving the system forward in time. As of this writing, the 64-bit version of Intel X86 (A64) looms. |