Chapter 3 Elements of Assembly Language

Download Report

Transcript Chapter 3 Elements of Assembly Language

Chapter 3
Elements of Assembly Language
How
to develop assembly Language Programs
Directives in MASM
Contents
 Types
and formats of statements in MASM
 Example program for how to assemble, link,
and execute
 Structure of assembly language program
 Constant operands (操作数) and instruction
operands
3.2 A Complete Example
 Purpose:
prompt for (提示输入) two
numbers and then find and display their
sum.
Pseudocode design for the program
1. Prompt for the first number;
2. Input ASCII characters representing the first number;
3. Convert the characters to a 2’s complement double
world
4. Store the first number in memory;
5. Prompt for the second number;
6. Input ASCII characters representing the first number;
7. Convert the characters to a 2’s complement double
world
8. Add the first number to the second number;
9. Convert the sum to a string of ASCII characters;
10. Display a label and the characters representing the sum;
Structure of the program(1)
comment
; Example assembly language program -- adds two
numbers
; Author: R. Detmer
; Date: revised 7/97
Accept 80386 instruction
.386
.MODEL FLAT
directives
Flat memory model
Structure of the program(2)
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
Prototype a function(声明函数)
INCLUDE io.h
; header file for input/output
Copy file
cr
Lf
EQU
EQU
0dh
0ah
; carriage return character
; line feed
Equate symbols to values
Structure of the program(3)
.STACK 4096
; reserve 4096-byte stack
Declare the Size of runtime stack
Structure of the program(3)
.DATA
; reserve storage for data
Starts the data segment
number1 DWORD ?
number2 DWORD ?
Symbolic
name
Reserve a single
doubleword of storage
Structure of the program(4)
prompt1
prompt2
string
label1
sum
BYTE
BYTE
BYTE
BYTE
BYTE
BYTE
"Enter first number: ", 0
"Enter second number: ", 0
40 DUP (?)
cr, Lf, "The sum is "
Repeat items
11 DUP (?)
in
cr, Lf, 0
parentheses
Reserve a byte of
storage
Structure of the program(5)
.CODE
; start of main program code
Starts the code segment
_start:
label
output prompt1
; prompt for first number
input string, 40
; read ASCII characters
atod string
; convert to integer
mov number1, eax ; store in memory
output prompt2
; repeat for second number
input string, 40
atod string
mov number2, eax
mov eax, number1 ; first number to EAX
add eax, number2 ; add second number
dtoa sum, eax
; convert to ASCII characters
output label1
; output label and sum
Call the procedure
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC
END
_start
; make entry point public
; end of source code
Make the label visible outside the file
Marks the physical ends of the program
Template for assembly language
;Comments for your program
.386
.MODEL FLAT, stdcall
includelib lib\kernel32.lib
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
.STACK 4096
; reserve 4096-byte stack
.DATA
; reserve storage for data
{;data define;}
.CODE
; start of main program code
start:
{;your codes;}
INVOKE ExitProcess, 0 ;exit with return code 0
;ret
END start
;end of source code
3.1 Assembly Language
Statements
 Length:
up to 512
If not enough, use backslash(\) character at
the end of a line
 Comments:
Begin with the semicolon(;) and extends
until the end of the line
Three types of functional assembly
language statements
 Instructions(真指令)
Can be translated into machine code( object
code)
 Directives(伪指令)
Tell the assembler to take some action , not
result in machine code

macros(宏)
Shorthand for a sequence of other
statements
Components of a statement
 name:
标号
mnemonic operands ;comment
助记符
操作数
注释
3.3 How to Assemble , Link and
Run a Program
 Edit
source code
 Assemble source code
 Link
 execute
Make executable file
 Assemble
 ml /c /coff /Zi example.asm
 Link
Link /subsystem:console /entry:start
/out:example.exe /debug example.obj io.obj
kernel32.lib
 Run
example
3.4 The Assemble Listing File
 Help
to understand
the assembly process
 Shows the source
code, the object code
and additional
information
ml /c /coff /Zi /Fl example.asm
First part of listing file
Microsoft (R) Macro Assembler Version 6.14.8444
D:\masm32\HelloCon.asm
09/15/06 08:11:53
Page 1 - 1
Offset address of Object code of
statements
each directives or instructions or
instructions
value assigned by
directives
Representing data in hexdicimal
Second part of listing file
Microsoft (R) Macro Assembler Version 6.14.8444
08:11:53
D:\masm32\HelloCon.asm
 Shows
09/15/06
Symbols 2 - 1
all the symbols that are used in
the program.
Macro name
Segment and procedure names
3.5 Variable Operands
Numeric
operands(数值)
Directives:
Byte
Word
Dword
Radix(数制)
Changes the default number base
Assembler assumes that a number is decimal
Constant operands
 EQU
Symbol EQU value
=
Symbol = value
Can be redefined
Model define directives
 Select
Instruction set
.386, .486, .586,386p, .mmx
 Option
(选项)
Option casemap:none

mode define
model memory mode, [call], [others]
Memory mode
Memory
mode
Max.
size of
seg.
File type OS
Code seg.
Tiny
64KB
.com
DOS
One seg.
Small
64KB
.exe
DOS
1
1
Medium
64KB
.exe
DOS
1
Multi.
Compact
64KB
.exe
DOS
Multi.
1
Large
64KB
.exe
DOS
Multi.
Multi.
Huge
64KB
.exe
DOS
Multi.
Multi.
flat
4GB
.exe(PE) Windows
Same seg.
Data seg.
Include and includelib
 Include
filename
Insert other files in current file
 Includelib
filename
Tell the linker use library file named by
filename.
Insert function code in .exe file.
Declare function
 Function_name
PROTO [call]
[parameter1name:parameter1 type],…
Call:
C, stdcall,
Data Area define
 .data
Begin a data segment
 .data?
Not initial data
 .const
Define constant
 .stack
[size]
Define stack
Code area define
 .code
Begin code segment
 END
[LABEL]
The last statement in the program.
Label shows the entry point(入口点) of
the program.
Laboratory Exercise1
 Build
the developing environment for
programming using assembly language.
 Test HelloCon.asm and HelloWin.asm.
 Make a program complete the following
function: s=x+y. Use debugger to prove you
program is correct.
 Report:
Introduce your developing enviroment.
Write your program. Introduce the debugging
step that you used.
3.6 Instruction Operands
 Two
operands
Destination operands(目的操作数)
Source operands(源操作数)
 Constants
 Designate
CPU registers
 Reference memory locations
Addressing modes
 Giving
the location of the data for each mode
Immediate addressing mode(立即数寻址方式):
in the instruction itself
Register addressing mode (寄存器寻址方式)
 in
a register
Memory addressing mode (内存寻址方式):
at some address in memory
MOV instruction
 We
use MOV instruction introduce
addressing mode.
MOV dest , src ; src -> dest
Immediate addressing mode
(立即数寻址方式)
 Operand
itself is in the instruction.
 Eaxmple:
MOV eax , 100 ; 100 -> eax
Machine code: B8 00000064
Result: eax=00000064h
MOV al , ‘/’
ADD eax,135
; 2F -> al, B0 2F
; eax+135->eax
;05 00000087
Register addressing mode
(寄存器寻址方式)
 Operand
is in a register.
MOV eax , ebx ; ebx -> eax
MOV eax , 100 ; 100 -> eax
8-bit registers: AH, AL… and DL
16-bit registers: AX, BX… and DI
32-bit registers: EAX, EBX… and EDI
segment registers: CS, ES, DS, SS, FS and GS
Not allowed
Never mix an 8-bit register with a 16-bit register, an
8-bit register with a 32-bit register, or a 16-bit register
with a 32-bit register because this is not allowed by
the microprocessor and results in an error when
assembled.
 A segment-to-segment register MOV instruction is not
allowed. Changing the CS register with a MOV
instruction is not allowed.

MOV ES,DS
MOV BL,DX
MOV CS,AX
; not allowed
; not allowed
; not allowed
Memory addressing modes
(内存寻址方式)
 Operand
is stored in memory.
 How to calculate memory address
Direct(直接寻址方式):
 memory address is built into the instruction
Register indirect(寄存器间接寻址方式):
 memory address is in a register
Direct addressing mode
(直接寻址方式)
 Operand
has the00000004
32-bit address
ADD
eax, number2;05
built into the
instruction.
Note:
 Programmer often codes a symbol that is
associated
value
BYTE 100with a BYTE, DWORD, or WORD
directive
in the data segment or with an
MOV
eax , value
instruction
in
the
code
segment.
direct mode
;;;;; Register mode
 The location corresponding to such a symbol
willEQU
be relocatable
so that the assembler listing
value
100
MOV
eax , an
value
shows
assembly-time address that may be
adjusted
later. immediate mode
Register mode
Register indirect mode
 The
offset of the memory which stores
operand is contained in register.
ADD eax , [edx] ; [edx]->eax, 03 02
like pointer
vs.
ADD eax , edx ; edx ->eax
operand
Example for indirect addressing
value DWORD 100
MOV edx, value ; edx=100
MOV
edx , OFFSET value
; edx=offset address of value
MOV
eax , [edx] ;eax=100
Register used for indirect mode
 Registers
EAX, EBX, ECX, EDX, ESI, EDI
can be used for register indirect
addressing.(in data segment)
 Register EBP can be used for register indirect
addressing an address in stack segment.
 ESP also can be used for register indirect
addressing. But we seldom use it.
 For 16-bit: only use BX, SI, DI, BP
Size of the memory operand
 When
the size of the memory operand is
ambiguous, the PTR directive must be used to
give the size to the assembler.
MOV [ebx] , 0
MOV BYTE PTR [ebx], 0
MOV eax, [edx]; no need use PTR,the size of dest
operand is the same as the src operand.
 WORD
PTR, DWORD PTR used for word
or doubleword operands respectively.
Base-plus-index addressing
(基址变址寻址)

For 16-bit: use one base register (BX or BP),
and one index register (DI or SI) to indirectly
address memory.
MOV CX,[BX+DI]
MOV CH,[BP+SI]

;16-bit, CX = DS:[BX+DI]
;8-bit, CH = SS:[BP+SI]
For 32-bit: allows the combination of any two
32-bit extended registers except ESP.
MOV CL,[EDX+EDI] ;8-bit, CL=DS:[EDX+EDI]
MOV [EAX+EBX],ECX;32-bit, DS:[EAX+EBX] =ECX
Register relative addressing
(寄存器相对寻址)

In register relative addressing, the data in a
segment of memory are addressed by adding
the displacement (位移量)to the contents of a
base or an index register (BX, SI, DI, or BP).
MOV AX,[DI+100H] ;16-bit, AX = DS:[DI+100H]
MOV ARRAY[SI],BL ;8-bit, ARRAY[SI] = BL

For 32-bit, any of the 32-bit general-purpose
registers can be used in the register relative
addressing.
MOV DI,[EAX+10] ;16-bit, DI = DS:[EAX+10]
MOV ARRAY[EBX],EAX ;32-bit, ARRAY[EBX] = EAX
Base relative-plus-index addressing
(相对基址变址寻址)

Adds a displacement, besides using a base
register (BX or BP) and an index register (SI or
DI), to form the memory address.
MOV DH,[BX+DI+20H]
MOV AX,FILE[BX+SI]
MOV LIST[BP+DI],CL
MOV LIST[BP+SI+4],DH
MOV EAX,FILE[EBX+ECX+2]

In 80386 and above, all the 32-bit extended
registers are both base register and index
register.
Scaled-index addressing
(比例变址寻址)

This data-addressing mode is unique to the 80386
and above. It uses one or two 32-bit registers to
access the memory. The second register is multiplied
by a scaling factor (1, 2, 4, or 8).
MOV
MOV
MOV
MOV
MOV

EAX,[EBX+4*ECX]
[EAX+2*EDI+100H],CX
AL,[EBP+2*EDI-2]
EAX,ARRAY[4*ECX]
AL,[2*EBX]
A scaling factor of 1X is implied and need not be
included in the assembly language instruction.
3.7 Input/Output Using Macros
Defined in IO.H
 Read
this part youselves, and do the
exercises. If you have any question,
please put up your hand and ask me or
discuss with others.
Exercises
 P45.
Exercises3.1 1, 2
 P67. Exercises3.4 2, 3, 4
 P72. Exercises3.5 1-32
 P77. Exercises3.6 1-8
 P80. Exercises3.7 1-6