Download Chapter 3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Assembly Language for x86 Processors
6th Edition
Kip Irvine
Chapter 3: Assembly Language
Fundamentals
Slides prepared by the author
Revision date: 2/15/2010
(c) Pearson Education, 2010. All rights reserved. You may modify and copy this slide show for your personal use, or for
use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
Directives and Instructions
 Assembly language statements are either directives or
instructions
 Instructions are executable statements. They are translated
by the assembler into machine instructions. Ex:
call MySub
mov ax,5
;transfer of control
;data transfer
 Directives tells the assembler how to generate machine
code, allocate storage, or define segments. They do not
execute at run time. Ex:
count BYTE 50 ;creates 1 byte
;of storage
;initialized to 50
2
A Template for Assembly Language Programs
TITLE Program Template
;
;
;
;
;
(Template.asm)
Program Description:
anything after the ‘;’ is ignored
Author:
Creation Date:
Revisions:
Date:
Modified by:
INCLUDE Irvine32.inc ; contains library procedures for IA-32
; for 32-bit protected mode programs
.data
; data segment, read and write
; (insert variable declarations here)
.code
; code segment, read-only
main PROC
; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main
This is the template to follow in all your programs
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
3
A Template for ASM if Irvine32.inc is not Included
 Irvine32.inc:
procedures
and
information for IA-32
library
setup
 .386: identifies 80386 as required
processor. Use .586 for Pentium.
 .model: set the running mode to 32bit protected mode and use the MSWindows calling convention
 main PROC: label of the entry
point of the program
 first instruction to execute
 END: marks the end of the
program and identifies the
program’s startup procedure
 exit: macro that halts the
program then returns the
control to the caller (here the
Win32 console)
 .data and .code: beginning of
the data segment and code
4 segment
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD
DumpRegs PROTO
.data
;data declarations
.code
main PROC
… ;instructions here
call DumpRegs
INVOKE ExitProcess, 0
main ENDP
END main
The FLAT Memory Model
 The .model flat directive tells the assembler to generate code that will
run in protected mode and in 32-bit mode
 Also ask the assembler to do whatever is needed in order that code,
stack, and data share the same 32-bit memory segment
 All the segment registers will be loaded with the correct values at load
time and do not need to be changed by the programmer
 Only the offset part of a logical address becomes relevant
 Each data byte (or instruction) is referred to only by a 32-bit offset
address
 The directives .code and .data mark the beginning of the code and
data segments. They are used only for protection
.code is read-only
.data is read and write
5
Example: Adding and Subtracting Integers
TITLE Add and Subtract
(AddSub.asm)
; This program adds and subtracts 32-bit integers.
INCLUDE Irvine32.inc
.code
main PROC
mov eax,10000h
add eax,40000h
sub eax,20000h
call DumpRegs
exit
main ENDP
END main
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
;
;
;
;
EAX = 10000h
EAX = 50000h
EAX = 30000h
display registers
6
Assemble-Link Execute Cycle
(Steps to Produce an Executable File)
• The following diagram describes the steps from creating a
source program through executing the compiled program.
• If the source code is modified, Steps 2 through 4 must be
repeated. All 4 steps performed via the Visual Studio
environment. No need to use command lines in a window.
Link
Library
Source
File
Step 1: text editor
•
Step 2:
assembler
Object
File
Listing
File
Step 3:
linker
Executable
File
Step 4:
OS loader
Output
Map
File
See Getting started with MASM and Visual Studio 2012 at
http://www.asmirvine.com/ for instruction on assembling, linking and
running ASM programs using Microsoft Visual Studio
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
7
Listing File
• Use it to see how your program is compiled
• Contains
•
•
•
•
•
source code
addresses
object code (machine language)
segment names
symbols (variables, procedures, and constants)
• Example: addSub.lst
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
8
Map File
• Information about each program segment:
•
•
•
•
starting address
ending address
size
segment type
• Example: addSub.map (16-bit version)
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
9
Integer Constants
• Optional leading + or – sign
• binary, decimal, hexadecimal, or octal digits
• Common radix characters:
•
•
•
•
•
•
h – hexadecimal
q/o – octal
d – decimal
b – binary
r – encoded real
real
1011h
1011q or 1011o
1011d or 1011 (base 10 is the default)
1011b
3F800000r = +1.0 (topic of Chap 12)
-26.E5+05 , 2. , +3.0 , …
• More examples: 30d, 6Ah, -42, 1101b
• Hexadecimal beginning with letter: 0A5h
• A5h is not a number (must start with digit 0)
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
10
Character and String Constants
• Enclose character in single or double quotes
• 'A‘
,
"x"
• ASCII character = 1 byte
• Enclose strings in single or double quotes
• "ABC"
• 'xyz‘
,
“123” (this is a string, not a number)
• Each character occupies a single byte
• Embedded quotes:
• 'Say "Goodnight," Gracie'
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
11
Reserved Words and Identifiers
• Reserved words cannot be used as identifiers
• Instruction mnemonics, directives, type attributes,
operators, predefined symbols
• See MASM reference in Appendix A
• Identifiers are programmer-chosen names
•
•
•
•
•
•
Variable, constant, procedure, code label
1 to 247 characters, including digits
not case sensitive
first character must be a letter, _, @, ?, or $
Cannot be the same as an assembler reserved word
Avoid using ‘@’ as first character since many keywords
start with it.
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
12
Directives
• Commands that are recognized and acted upon by the
assembler
• Not part of the Intel instruction set; but used by the assembler
(i.e. the compiler) to direct the OS to perform certain tasks.
• Used to declare code, data areas, select memory model,
declare procedures, etc.
• not case sensitive
• Different assemblers have different directives
• NASM not the same as MASM, for example
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
13
Instructions
• Assembled into machine code by assembler
• Executed at runtime by the CPU
• We use the Intel IA-32 instruction set
• Always INCLUDE Irvine32.inc in your programs
• An instruction contains:
•
•
•
•
Label
Mnemonic
Operand
Comment
(optional)
(required)
(depends on the instruction)
(optional)
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
14
Mnemonics and Operands
• Instruction Mnemonics
• memory aid
• examples: MOV, ADD, SUB, MUL, INC, DEC
• Operands
•
•
•
•
constant
constant expression
register
memory (data label)
Constants and constant expressions are often called
immediate values
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
15
Instruction Format Examples
• No operands
• stc
; set Carry flag
• One operand
• inc eax
• dec myByte
• Two operands
; register
; memory
(there are also 3-operand instructions, but they are rare)
• add ebx, ecx
• sub myByte, 25
• add eax, 36 * 25
; register, register
; memory, constant
; register, constant-expression
• All instructions are in the .code segment of programs
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
16
Example: Adding and Subtracting Integers
TITLE Add and Subtract
(AddSub.asm)
; This program adds and subtracts 32-bit integers.
INCLUDE Irvine32.inc
.code
main PROC
mov eax,10000h
add eax,40000h
sub eax,20000h
call DumpRegs
exit
main ENDP
END main
;
;
;
;
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
EAX = 10000h
EAX = 50000h
EAX = 30000h
display content of registers
17
Example Output
Program output, showing registers and flags:
EAX=00030000
EBX=7FFDF000
ECX=00000101
EDX=FFFFFFFF
ESI=00000000
EDI=00000000
EBP=0012FFF0
ESP=0012FFC4
EIP=00401024
EFL=00000206
CF=0
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
SF=0
ZF=0
OF=0
18
Suggested Coding Standards
• Indentation and spacing
• code and data labels – no indentation
• executable instructions – indent 4-5 spaces
• comments: right side of page, aligned vertically
• 1-3 spaces between instruction and its operands
• ex: mov ax, bx
• 1-2 blank lines between procedures
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
19
Alternative Version of AddSub
(If not including Irvine32.inc)
TITLE Add and Subtract
(AddSubAlt.asm)
; This program adds and subtracts 32-bit integers.
.386
.MODEL flat,stdcall
.STACK 4096
ExitProcess PROTO, dwExitCode:DWORD
DumpRegs PROTO
.code
main PROC
mov eax,10000h
add eax,40000h
sub eax,20000h
call DumpRegs
INVOKE ExitProcess,0
main ENDP
END main
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
; EAX = 10000h
; EAX = 50000h
; EAX = 30000h
20
Data Definition Statement
• A data definition statement declares a variable and allocates memory for
the variable. The allocation directive defines the type of the variable.
• May optionally assign a name (label) to the data
• Syntax:
[name] directive initializer [,initializer]
var1 BYTE 10
var2 SWORD AFh, ?, -2, +7, 0BC9h
• All initializers become binary data in memory
• All variable declarations are in the .data segment of programs
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
21
Defining BYTE and SBYTE Data
8-bit unsigned integer and 8-bit signed integer type
Each of the following defines a single byte of storage:
value1 BYTE 'A'
; character constant
value2 BYTE 0
; smallest unsigned byte
value3 BYTE 255
; largest unsigned byte
value4 SBYTE -128
; smallest signed byte
value5 SBYTE +127
; largest signed byte
value6 BYTE ?
; uninitialized byte
• MASM does not prevent you from initializing a BYTE with a
negative value, but it's considered poor style.
• If you declare a SBYTE variable, the Microsoft debugger will
automatically display its value in decimal with a leading sign.
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
22
Defining [S]Byte Arrays
Examples that use multiple initializers:
list1 BYTE 10,20,30,40
list2 BYTE 10,20,30,40
BYTE 50,60,70,80
BYTE 81,82,83,84
list3 BYTE ?,32,41h,00100010b
list4 BYTE 0Ah,20h,‘A’,22h
A question mark (?) in the initializer leaves the initial value of
the variable undefined. Ex: c SBYTE ? ; c is undefined
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
23
Defining Strings
(1 of 3)
• A string is implemented as an array of characters
• For convenience, it is usually enclosed in quotation marks
• It often will be null-terminated
• Character type is BYTE
• Examples:
str1 BYTE "Enter your name",0
str2 BYTE 'Error: halting program',0
str3 BYTE 'A','E','I','O','U‘
greeting
BYTE "Welcome to the Encryption Demo program "
BYTE "created by Kip Irvine.",0
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
24
Defining Strings
(2 of 3)
• To continue a single string across multiple lines, end
each line with a comma:
menu BYTE "Checking Account",0dh,0ah,0dh,0ah,
"1. Create a new account",0dh,0ah,
"2. Open an existing account",0dh,0ah,
"3. Credit the account",0dh,0ah,
"4. Debit the account",0dh,0ah,
"5. Exit",0ah,0ah,
"Choice> ",0
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
25
Defining Strings
(3 of 3)
• End-of-line character sequence:
• 0Dh = carriage return
• 0Ah = line feed
str1 BYTE "Enter your name:
",0Dh,0Ah
BYTE "Enter your address: ",0
newLine BYTE 0Dh,0Ah,0
• Line continuation character (\)
• Concatenates two source code lines into a single statement
• greeting1 BYTE “Welcome to the Encryption Demo Program”,0
• greeting1 \
BYTE “Welcome to the Encryption Demo Program”,0
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
26
Using the DUP Operator
• Use DUP to allocate (create space for) an array of
any type or for a string.
Syntax: [var_name] TYPE counter DUP ( argument )
• Counter and argument must be constants or constant
expressions. DUP must be used only with data allocation directives.
var1 BYTE 20 DUP(0)
; 20 bytes, all equal to zero
var2 BYTE 20 DUP(?)
; 20 bytes, uninitialized
var3 BYTE 4 DUP("STACK")
; 20 bytes: "STACKSTACKSTACKSTACK"
var4 BYTE 10,3 DUP(0),20
; 5 bytes
Var5 BYTE 2 DUP( ‘a’ , 2 DUP ( ‘b’ ) ) ; 6 bytes : ‘abbabb’
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
27
Defining WORD and SWORD Data
• 16-bit unsigned & signed integer type
• Define storage for 16-bit integers
• or double characters
• single value or multiple values
word1
word2
word3
word4
myList
array
WORD
SWORD
WORD
WORD
WORD
WORD
65535
–32768
?
"AB"
1,2,3,4,5
5 DUP(?)
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
;
;
;
;
;
;
largest unsigned value
smallest signed value
uninitialized, unsigned
double characters
array of words
uninitialized array
28
Defining DWORD and SDWORD Data
32-bit unsigned & signed integer type
Storage definitions for signed and unsigned 32-bit
integers:
val1
val2
val3
val4
DWORD
SDWORD
DWORD
SDWORD
12345678h
–2147483648
20 DUP(?)
–3,–2,–1,0,1
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
;
;
;
;
unsigned
signed
unsigned array
signed array
29
Defining QWORD, TBYTE, Real Data
64-bit integer, 80-bit integer, and real types
Storage definitions for quadwords, tenbyte values,
and real numbers:
quad1 QWORD
val1 TBYTE
rVal1 REAL4
rVal2 REAL8
rVal3 REAL10
ShortArray
1234567812345678h
1000000000123456789Ah
-2.1
; 4-byte single-precision
3.2E-260
; 8-byte double-precision
4.6E+4096
; 10-byte extended precision
REAL4 20 DUP(0.0)
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
30
Offset Address of Variables and Data
 The optional variable name is a label marking its address in the
data segment.
 The (offset) address of a variable is the address of its first byte.

Ex: If the following data segment starts at address 0.
.data
Var1 BYTE “ABC”
Var2 BYTE “DEFG”
31





The address of Var1 is 0 = the address of ‘A’
The address of ‘B’ is 1
The address of ‘C’ is 2
The address of Var2 is 3
The address of ‘E’ is 4 …
Little Endian Order
• All data types larger than a byte store their individual
bytes in reverse order. The least significant byte occurs
at the first (lowest) memory address.
• Example:
val1 DWORD 12345678h
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
32
Little Endian Order
 Ex:
A WORD 1234h, 5678h ; allocates 2 words
 Intel’s x86 are little endian processors: the lowest order
byte (of a word or double word) is always stored at the
lowest address.
 Ex: if variable A (above) is located at address 0, we
have:
 address:
0
1
2
3
 value:
34h 12h 78h 56h
33
Little Endian Order
 Ex:
B DWORD 12345678h ;allocates 1 double word
 If variable B is located at address of 0, we have:
 address: 0
1
2
3
 value:
78h
56h
34h
12h
 If a value fits into a byte, it will be stored in the lowest ordered byte
available. Ex: V WORD ‘A’
 the value will be stored as:
address:
0
1
value:
41h
00h
34
Legacy Data Directives
 Legacy data directives are also supported by NASM and TASM.
 Var1 DB -128
; 8-bit integer type (signed or unsigned)
 Var2 DW +32768
; 16-bit integer type (signed or unsigned)
 Var3 DD 1.2
; 32-bit integer/real (signed or unsigned)
 Var4 DQ 3.2E-260
; 64-bit integer/real (signed or unsigned)
 Var5 DT 4.6E+4096
; 80-bit integer/real (signed or unsigned)
The Legacy Data Directives do not distinguish between signed or
unsigned data
35
Adding Variables to AddSub
TITLE Add and Subtract, Version 2
(AddSub2.asm)
; This program adds and subtracts 32-bit unsigned
; integers and stores the sum in a variable.
INCLUDE Irvine32.inc
.data
val1 DWORD 10000h
val2 DWORD 40000h
val3 DWORD 20000h
finalVal DWORD ?
.code
main PROC
mov eax,val1
; start with 10000h
add eax,val2
; add 40000h
sub eax,val3
; subtract 20000h
mov finalVal,eax
; store the result (30000h)
call DumpRegs
; display the registers
exit
main ENDP
END main
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
36
Equal-Sign Directive
• name = integer expression
•
•
•
•
expression is a 32-bit integer (expression or constant)
may be redefined
name is called a symbolic constant
No memory is allocated for a constant
• ASM substitutes name with value (of expression) in
each occurrence of name
• good programming style to use symbols
COUNT = 500
; this is a constant, not a variable
mov ax, COUNT ; AX ← 500
A = (-3 * 8) + 2
B = (A+2)/2
; constants can be defined in terms of another constants
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
37
Calculating the Size of a Word Array
Divide total number of bytes by 2 (the size of a word)
list WORD 1000h,2000h,3000h,4000h
ListSize = ($ - list) / 2 ; ListSize is a constant
; evaluated at runtime
Difference ($ - list) is the number of bytes
The $ operator (current location counter) returns the
offset associated with the current program statement
The constant must follow immediately after the array
whose size you want to calculate
Works for any type: BYTE, DWORD, QWORD, … etc
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
38
EQU Directive
• Define a symbol as either an integer or text expression.
• Cannot be redefined
PI
EQU <3.1416>
; text expression
Mat1
EQU 10 * 10
; integer expression
Mat2
EQU <10*10>
; text expression
pressKey EQU <"Press any key to continue...",0>
.data
prompt BYTE pressKey ; prompt ← “Press any …”,0 “
M1 WORD
Mat1
; M1 WORD 100
M2 WORD
Mat2
; M2 WORD 10 * 10
P
REAL4 PI
; P REAL4 3.1416
Text must be enclosed with <...>
(useful for real value)
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
39
TEXTEQU Directive
• Define a symbol as either an integer or text expression.
• Called a text macro
• Can be redefined at any time
continueMsg TEXTEQU <"Do you wish to continue (Y/N)?">
rowSize = 5
.data
prompt1 BYTE
count
continueMsg
TEXTEQU %(rowSize * 2)
setupAL TEXTEQU <mov al,count>
; assigns the content of a textmacro
; evaluates the integer expression
; assigns text
.code
setupAL
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
; generates: "mov al,10"
40
Exercise 1
 Suppose that the following data segment starts at
address 0
.data
A WORD
1,2
B WORD
6ABCh
Z EQU
232
C BYTE
'ABCD'




41
A) Find the address of variable A.
B) Find the address of variable B.
C) Find the address of variable C.
D) Find the address of character ‘C’.
4C 61 46 69 6E
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
42
Real-Address Mode Programming
(1 of 2)
• Generate 16-bit MS-DOS Programs
• Advantages
• enables calling of MS-DOS and BIOS functions
• no memory access restrictions
• Disadvantages
• must be aware of both segments and offsets
• cannot call Win32 functions (Windows 95 onward)
• limited to 640K program memory
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
43
Real-Address Mode Programming
(2 of 2)
• Requirements
• INCLUDE Irvine16.inc
• Initialize DS to the data segment:
mov ax,@data
mov ds,ax
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
44
Add and Subtract, 16-Bit Version
TITLE Add and Subtract, Version 2
(AddSub2r.asm)
INCLUDE Irvine16.inc
.data
val1 DWORD 10000h
val2 DWORD 40000h
val3 DWORD 20000h
finalVal DWORD ?
.code
main PROC
mov ax,@data
; initialize DS
mov ds,ax
mov eax,val1
; get first value
add eax,val2
; add second value
sub eax,val3
; subtract third value
mov finalVal,eax
; store the result
call DumpRegs
; display registers
exit
main ENDP
END main
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
45
Summary
• Integer expression, character constant
• directive – interpreted by the assembler
• instruction – executes at runtime
• code, data, and stack segments
• source, listing, object, map, executable files
• Data definition directives:
• BYTE, SBYTE, WORD, SWORD, DWORD, SDWORD, QWORD,
TBYTE, REAL4, REAL8, and REAL10
• DUP operator, location counter ($)
• Symbolic constant
• EQU and TEXTEQU
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
46
4C 61 46 69 6E
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
47