Opcodes and Mnemonics

At the hardware level in an Intel or compatible processor, instructions are built directly into the CPU circuitry and these are represented by opcodes.

While assembly code can be written at the Byte level, it is a particularly complex method of writing code since it involves memorizing a very large number of opcodes.  Additionally, such program code must use the Intel numeric (little-endian) data format.  With little-endian, multi-byte numeric values are stored in reverse order to usual human representation.

For example, to copy the 32-bit value &H56A700FE into the EAX register (MOV EAX, &H56A700FE), you must first find the opcode for the MOV EAX mnemonic (&HA1) followed by the data in reverse order (&HFE, &H00, &HA7, and &H56).

In hex format, the whole instruction would look like this:

A1 FE 00 A7 56

Obviously this becomes a very tedious and error prone way to write assembly code.  As a result, a system was developed (many years ago) where groups of similar opcodes were given verbose names that made them a lot more convenient to use than raw numeric opcodes.  These names are referred to as mnemonics, and this is the system used in PowerBASIC's 32-bit Inline Assembler.

Each mnemonic represents a reserved name that represents a family of opcodes that perform similar tasks in the processor.  The actual numeric opcodes are different depending on the size and type of operands being used.  For example, with the MOV mnemonic:

! MOV EAX, VAR1  ' opcode = &HA1

! MOV VAR1, EAX  ' opcode = &HA3

The use of mnemonics provides a far more intuitive way of representing opcodes; however, there is no exact correlation between what you write using mnemonics and what you get as finished opcodes.  This is because the opcode you actually get for a given mnemonic can depend on whether it is using near or far addressing, the operand data types (registers or pointers or constants), etc.  However, PowerBASIC takes care of these details automatically and transparently for you, leaving you to get on with writing your actual program.

 

See Also

The Inline Assembler

Using assembly-language in your code

Mnemonics and Operands

Registers