X 86 Asm
X 86 Asm
Instructions Labels
• Assembled into machine code by assembler • Act as place markers
– marks the address (offset) of code and data
• Executed at runtime by the CPU
• Easier to memorize and more flexible
• Member of the Intel IA-32 instruction set
mov ax ax, [0020] → mov axax, val
• Four parts • Follow identifier rules
– Label (optional)
• Data
D llabel
b l
– Mnemonic (required)
– must be unique
– Operand (usually required) – example:
l myArray BYTE 10
– Comment (optional) • Code label (ends with a colon)
– target of jump and loop instructions
Label: Mnemonic Operand(s) ;Comment – example: L1: mov ax, bx
...
3
jmp L1 4
Reserved words and identifiers Mnemonics and operands
• Reserved words cannot be used as identifiers • Instruction mnemonics
– Instruction mnemonics,
mnemonics directives
directives, type attributes
attributes, – "reminder"
reminder
operators, predefined symbols – examples: MOV, ADD, SUB, MUL, INC, DEC
• Identifiers • Operands
– 1-247 characters, including digits – constant (immediate value), 96
– case insensitive (by default) – constant expression,
expression 2+4
– first character must be a letter, _, @, or $ – Register, eax
– examples:
p – memoryy ((data label),
), cou
count
t
var1 Count $first • Number of operands: 0 to 3
_main MAX open_file – stc ; set Carry flag
@@myfile xVal _12345 – inc ax ; add 1 to ax
, bx
– mov count, ; move BX to count
5 6
Directives Comments
• Commands that are recognized and acted upon • Comments are good!
by the assembler – explain the program's
program s purpose
– tricky coding techniques
– Part of assembler’s syntax but not part of the Intel
instruction set pp p
– application-specific explanations
p
– Used to declare code, data areas, select memory • Single-line comments
model declare procedures
model, procedures, etc
etc. – begin
g with semicolon (;)
– case insensitive • block comments
• Different assemblers have different directives – begin with COMMENT directive and a programmer
programmer-
chosen character and end with the same
– NASM != MASM, for example programmer-chosen character
• Examples:
E l .data
d .code
d PROC
OC COMMENT !
This is a comment
and this line is also a comment
7 ! 8
Example: adding/subtracting integers Example output
directive marking a comment
TITLE Add and Subtract (AddSub.asm) Program output
output, showing registers and flags:
comment
; This program adds and subtracts 32-bit integers.
EAX 00030000
EAX=00030000 EBX
EBX=7FFDF000
7FFDF000 ECX
ECX=00000101
00000101 EDX
EDX=FFFFFFFF
FFFFFFFF
INCLUDE Irvine32.inc copy definitions from Irvine32.inc ESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4
.code code segment.
seg e t. 3 segments:
seg e ts: code, data, stac
stack EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0
main PROC beginning of a procedure
mov eax,10000h source ; EAX = 10000h
add eax,40000h ; EAX = 50000h
sub eax,20000h
d ti ti ; EAX = 30000h
destination
call DumpRegs ; display registers
exit defined in Irvine32
Irvine32.inc
inc to end a program
main ENDP
END main marks the last line and
define the startup procedure
9 10
Listing Map
Step 1: text editor File File
13
15 16
Data definition statement Integer constants
• A data definition statement sets aside storage in • [{+|-}] digits [radix]
memory for a variable.
variable • Optional leading + or – sign
• May optionally assign a name (label) to the data. • binary, decimal, hexadecimal, or octal digits
• Only size matters,
matters other attributes such as signed are
• C
Common radix
di characters:
h t
just reminders for programmers.
– h– hexadecimal
• Syntax:
– d– d i l (d
decimal (default)
f lt)
[name] directive initializer [,initializer] . . .
– b– binary
At least one initializer is required, can be ?
– r– encoded real
• All initializers become binary data in memory
– o– octal
±1.bbbb×2 (E-127)
• double
1 11 52
S E M
19 20
Real number constants (decimal reals) Character and string constants
• [sign]integer.[integer][exponent] • Enclose character in single or double quotes
sign
i → {+|-}
{ |} – 'A',
'A' ""x"
"
exponent → E[{+|-}]integer – ASCII character = 1 byte
• Examples: • Enclose
l strings in single
l or double
d bl quotes
– "ABC"
2.
– 'xyz'
+3.0
– Each character occupies a single byte
44.2E+05
-44 2E+05
26.E5 • Embedded quotes:
– ‘Say
y "Goodnight,"
g Gracie’
– "This isn't a test"
21 22
A variable name is a data label that implies an offset list4 BYTE 0Ah,20h,‘A’,22h
(an address).
23 24
Defining strings (1 of 2) Defining strings (2 of 2)
29 30
33 34
Equal-sign directive
• name = expression
– expression
i iis a 32-bit
32 bit integer
i t (
(expression
i or constant)
t t)
– may be redefined
– name is
i called
ll d a symbolic
b li constant
t t
Symbolic constants • good programming style to use symbols
– Easier to modify
COUNT = 500
– Easier to understand, ESC_key
Array DWORD COUNT DUP(0) .
COUNT=5 mov al,COUNT
mov al,l COUNT
COUNT=10
mov al
al, COUNT
36
Calculating the size of a byte array Calculating the size of a word array
• current location counter: $ • current location counter: $
– subtract
bt t address
dd off li
listt – subtract address of list
– difference is the number of bytes – difference is the number of bytes
– divide by 2 (the size of a word)
list BYTE 10,20,30,40 list BYTE 10,20,30,40
ListSize
stS e = 4 ListSize
stS e = ($ - list)
st)
list
li t WORD 1000h,2000h,3000h,4000h
1000h 2000h 3000h 4000h
ListSize = ($ - list) / 2
list BYTE 10,20,30,40
var2 BYTE 20 DUP(?) list DWORD 1,2,3,4
ListSize = ($ - list) ListSize = ($ - list) / 4
39 40
Addressing Modes
Addressing
45 46
47 48
Direct-offset operands (cont) Data-Related Operators and Directives
A constant offset is added to a data label to produce an • OFFSET Operator
effective address (EA).
(EA) The address is dereferenced to
get the value inside its memory location. • PTR Operator
.data
data • p
TYPE Operator
arrayW WORD 1000h,2000h,3000h • LENGTHOF Operator
arrayD DWORD 1,2,3,4
.code
• SIZEOF Operator
mov ax,[arrayW+2] ; AX = 2000h • LABEL Directive
mov ax,[arrayW+4]
[ W 4] ; AX = 3000h
mov eax,[arrayD+4] ; EAX = 00000002h
.code .code
mov ecx,LENGTHOF array1 ; 32 mov ecx,SIZEOF array1 ; 64
55 56
ALIGN Directive PTR Operator
• ALIGN bound aligns a variable on a byte, word, Overrides the default type of a label (variable).
doubleword or paragraph boundary for
doubleword, Provides the flexibility to access part of a variable.
variable
efficiency. (bound can be 1, 2, 4, or 16.) .data
myDouble DWORD 12345678h
bVal BYTE ? ; 00404000 .code
ALIGN 2 o a
mov ax,myDouble
, y oub e ; e
error
o – why?
y?
wVal WORD ? ; 00404002 mov ax,WORD PTR myDouble ; loads 5678h
bV l2
bVal2 BYTE ? ; 00404004
ALIGN 4 mov WORD PTR myDouble,4321h ; saves 4321h
dVal DWORD ? ; 00404008
To understand how this works, we need to know
dVal2 DWORD ? ; 0040400C
about little endian ordering of data in memory.
memory
57 58
61 62
A data declaration spans multiple lines if each line In the following example, array identifies only the first
(except the last) ends with a comma
comma. The LENGTHOF WORD declaration.
declaration Compare the values returned by
and SIZEOF operators include all lines belonging to the LENGTHOF and SIZEOF here to those in the previous
declaration: slide:
.data .data
array WORD 10,20, array WORD 10,20
30,40, WORD 30,40
50,60 WORD 50,60
.code .code
mov eax
eax,LENGTHOF
LENGTHOF array ; 6 mov eax
eax,LENGTHOF
LENGTHOF array ; 2
mov ebx,SIZEOF array ; 12 mov ebx,SIZEOF array ; 4
63 64
LABEL Directive Indirect operands (1 of 2)
• Assigns an alternate label name and type to an existing An indirect operand holds the address of a variable,
storage location usuallyy an arrayy or string.
g It can be dereferenced (j
(just
• LABEL does not allocate any storage of its own; it is like a pointer). [reg] uses reg as pointer to access
jjust an alias. memory
.data
d
• Removes the need for the PTR operator val1 BYTE 10h,20h,30h
.data
data .code
code
dwList LABEL DWORD mov esi,OFFSET val1
wordList
o d st LABEL WORD
O mov al,[esi] ; dereference ESI (AL = 10h)
intList BYTE 00h,10h,00h,20h
.code inc esi
mov eax,dwList ; 20001000h mov al,[esi]
l [ i] ; AL = 20h
mov cx,wordList ; 1000h
mov dl
dl,intList
intList ; 00h inc esi
65
mov al,[esi] ; AL = 30h 66
67 68
Indexed operands Index scaling
An indexed operand adds a constant to a register to You can scale an indirect or indexed operand to the
generate an effective address. There are two notational offset
o set of
o an
a array
a ay element.
ele e t. Thiss iss do
done
e by multiplying
ult ply g
forms: [label + reg] label[reg] the index by the array's TYPE:
.data
.data
d
arrayW WORD 1000h,2000h,3000h arrayB BYTE 0,1,2,3,4,5
.code
code arrayW WORD 00,1,2,3,4,5
1 2 3 4 5
mov esi,0 arrayD DWORD 0,1,2,3,4,5
y + esi] ; AX = 1000h
mov ax,[arrayW .code
code
mov ax,arrayW[esi] ; alternate format
mov esi,4
add esi,2
add
dd ax,[arrayW
[ W + esi]
i] mov al
al,arrayB[esi*TYPE
arrayB[esi*TYPE arrayB] ; 04
etc. mov bx,arrayW[esi*TYPE arrayW] ; 0004
mov edx
edx,arrayD[esi*TYPE
arrayD[esi*TYPE arrayD] ; 00000004
69 70
Pointers
You can declare a pointer variable that contains the
offset of another variable.
variable
.data
arrayW WORD 1000h,2000h,3000h
1000 2000 3000
ptrW DWORD arrayW
.code
code
mov esi,ptrW
mov ax,[esi] ; AX = 1000h
71