Intel x86 Assembly Language & Registers
Writing machine code direct in binary would be too difficult and error prone. However we can do it using something called assembly language.
Each assembly language statement corresponds to a single machine instruction.
Opcodes are specified using mnemonics (jmp
, mov
)
Registers are given names and addresses are specified using labels.
Labels can also be used to label lines of code to jump to.
Single Line Example
adjust: mov eax, num1 ; Moves num1 to eax
- A label can refer to the address of an instruction or the address of a data item.
adjust:
num1
- Statements like this can be translated to binary by a program called an assembler.
- It may also be possible to use and in-line assembler.
- Allows insertion of assembly code into HLL programs.
- Advantage of mix and match.
Intel x86 Registers
The x86 also has a number of general purpose registers:
EAX
: Accumulator RegisterEBX
: Base RegisterECX
: Counter RegisterEDX
: Data Register
Other such as the Flags
register and ESP
register will be considered later.
Even though the registers have names they are general purpose so can be used for anything.
The Accumulator
Most CPUs have an accumulator. This is a register for a general purpose data storage and computation. On an x86 CPU it looks like this:
As the word size of x86 has increased over time so have the registers. To handle backwards compatibility each word size has a different name.
Register and Opcode Examples
mov eax, 42 ; Put 42 into eax.
mov ax, count ; Get 16-bit variable.
mov al, 'x' ; Put ASCII val of 'x' in low byte.
inc eax ; Add one to eax.
add eax, 10 ; Add 10 to eax.
mov sum, eax; Store 32-bit var.
Note that the first operand acts as the destination.
Note that mov var, var
is not allowed.
Assignment
We can now handle assignment:
num = count1 + count2 - 10
The equivalent in asm
would be:
mov eax, count1
add eax, count2
sub eax, 10
mov num, eax
This transfers count1
to the accumulator and then adds count2
to the accumulator. 10 is then subtracted from the accumulator and the result is transferred to the label num
.