PROWAREtech
Intel IA-32 Assembly Tutorial - A Guide to the Basics of x86 Assembly - Page 03
Instructions
A line of code, or instruction, may have a label, must have an instruction mnemonic, usually have an operand and optionally, a comment. The following is an example with all four.
.code
lblLoop: mov ebx, my_variable ;moving my_variable to ebx
lblLoop:
is a code label, mov
is an instruction mnemonic, ebx
and my_variable
are both operands, and the comment begins with a semicolon.
A code label (loop:
) is used to jmp
(jump, unconditionally) to:
.code
lblLoop:
mov ebx, eax
jmp lblLoop
This is an infinite loop.
Data labels define variables in the data (.data) area of a program.
.data
my_first_variable DWORD ;creates an unsigned 32-bit variable
my_second_variable SWORD ;creates a signed 16-bit variable
my_third_variable REAL4 ;creates a 4 byte single precision real variable
my_fourth_variable REAL8 ;creates an 8 byte double precision real variable
my_fifth_variable REAL10 ;creates an 10 byte double precision extended precision real variable
my_first_array BYTE 10 DUP(?) ;creates ten byte array uninitialized
my_second_array BYTE 20 DUP(0) ;creates twenty byte array initialized to all zeros
.code
mov eax,10
my_first_variable
is at offset 0 and my_second_variable
is at offset 4, etc. Also,
the size of a variable can easily be determined using the current location counter which is the $ sign.
.data
my_first_variable DWORD ;creates an unsigned 32-bit variable
my_first_variable_size = ($ - my_first_variable)
my_second_variable SWORD ;creates a signed 16-bit variable
my_third_variable REAL4 ;creates a 4 byte single precision real variable
my_fourth_variable REAL8 ;creates an 8 byte double precision real variable
my_fourth_variable_size = ($ - my_fourth_variable);
my_fifth_variable REAL10 ;creates an 10 byte double precision extended precision real variable
my_first_array BYTE 10 DUP(?) ;creates ten byte array uninitialized
my_first_array_size = ($ - my_first_array)
my_second_array BYTE 20 DUP(0) ;creates twenty byte array initialized to all zeros
my_second_array_size = ($ - my_second_array)
.code
mov eax,10
Notice that the current location counter ($) must be used immediately after the variable is defined.
Instruction mnemonics examples:
mov
add
sub ;subtract
mul ;multiply
div ;divide
jmp
call ;call a PROCedure
stc ;set the Carry flag
inc ;increment by one
dec ;decrement by one
The instruction mnemonic can have between zero and three operands.
Comments can be single line ones using the semicolon or a block using the COMMENT directive.
COMMENT @ Comment line 1 Comment line 2 Comment line 3 @
More on Directives
The .386
directive identifies the minimum hardware the program will run one.
A modern machine would use .686
. The .model flat,stdcall
tells the assembler to generate code for a CPU protected mode program using a flat 32-bit
memory model (no 16-bit and 24-bit pointers to think about). STDCALL specifies that each procedure should
clean the memory stack up after they are done with it. .stack 8192
specifies
how large the stack should be.
Little Endian Order
Intel processors store the least significant byte of a multi-byte variable at the lowest address. This table demonstrates how Intel processors store the 32-bit value 12345678h.
78h | 56h | 34h | 12h |
0000 | 0001 | 0002 | 0003 |
To reverse this order to "Big Endian Order" use the instructions BSWAP
(Byte-SWAP) for DWORD values or XCHG
(eXCHanGe) for WORD values.
BSWAP eax ; reverse the order of bytes in eax
XCHG al, ah ; exchange the order of bytes in ax