chapter 3 elements of assembly language how to develop assembly language programs directives in...

Post on 21-Dec-2015

242 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 3 Elements of Assembly Language

How to develop assembly Language ProgramsDirectives in MASM

Contents

Types and formats of statements in MASMExample program for how to assemble, link,

and executeStructure of assembly language programConstant operands ( 操作数 ) and instructio

n operands

3.2 A Complete Example

Purpose: prompt for ( 提示输入 ) two numbers and then find and display their sum.

Pseudocode design for the program1. Prompt for the first number;2. Input ASCII characters representing the first number;3. Convert the characters to a 2’s complement double

world4. 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

world8. 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;

; Example assembly language program -- adds two numbers; Author: R. Detmer; Date: revised 7/97

.386

.MODEL FLAT

Structure of the program(1)comment

directives

Accept 80386 instruction

Flat memory model

Structure of the program(2)

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

INCLUDE io.h ; header file for input/output

cr EQU 0dh ; carriage return characterLf EQU 0ah ; line feed

Prototype a function( 声明函数 )

Copy file

Equate symbols to values

.STACK 4096 ; reserve 4096-byte stack

Structure of the program(3)

Declare the Size of runtime stack

Structure of the program(3)

.DATA ; reserve storage for data

number1 DWORD ?number2 DWORD ?

Starts the data segment

Reserve a single doubleword of storage

Symbolic name

Structure of the program(4)

prompt1 BYTE "Enter first number: ", 0prompt2 BYTE "Enter second number: ", 0string BYTE 40 DUP (?)label1 BYTE cr, Lf, "The sum is "sum BYTE 11 DUP (?) BYTE cr, Lf, 0

Reserve a byte of storage

Repeat items in

parentheses

.CODE ; start of main program code

_start: output prompt1 ; prompt for first number input string, 40 ; read ASCII characters atod string ; convert to integer mov number1, eax ; store in memory

Structure of the program(5)

Starts the code segment

label

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

INVOKE ExitProcess, 0 ; exit with return code 0

PUBLIC _start ; make entry point public

END ; end of source code

Call the procedure

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, stdcallincludelib lib\kernel32.libExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD.STACK 4096 ; reserve 4096-byte stack.DATA ; reserve storage for data {;data define;}.CODE ; start of main program codestart: {;your codes;}INVOKE ExitProcess, 0 ;exit with return code 0; retEND 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 lineComments:

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 statement

s

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:e

xample.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 fileMicrosoft (R) Macro Assembler Version 6.14.8444 09/15/06 08:11:53D:\masm32\HelloCon.asm Page 1 - 1

Offset address of each directives or instructions

Object code of instructions or value assigned by directives

statements

Representing data in hexdicimal

Second part of listing file

Shows all the symbols that are used in the program. Macro name Segment and procedure names

Microsoft (R) Macro Assembler Version 6.14.8444 09/15/06 08:11:53D:\masm32\HelloCon.asm Symbols 2 - 1

3.5 Variable OperandsNumeric operands (数值)

Directives:ByteWordDwordRadix (数制)

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 modeMemory mode

Max. size of seg.

File type OS Code seg. Data 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.

Include and includelib

Include filename Insert other files in current file

Includelib filename Tell the linker use library file named by file

name. 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 progra

mming using assembly language. Test HelloCon.asm and HelloWin.asm. Make a program complete the following func

tion: s=x+y. Use debugger to prove you program is correct.

Report: Introduce your developing enviroment. Write your program. Introduce the debugging ste

p that you used.

3.6 Instruction Operands

Two operands Destination operands (目的操作数) Source operands (源操作数)

ConstantsDesignate CPU registersReference 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

Result: eax=00000064hMachine code: B8 00000064

MOV al , ‘/’ ; 2F -> al, B0 2FADD eax,135 ; eax+135->eax ;05 00000087

Register addressing mode(寄存器寻址方式)

Operand is in a register.

MOV eax , ebx ; ebx -> eaxMOV 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 ; not allowed

MOV BL,DX ; not allowed

MOV CS,AX ; 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 the 32-bit address built into the instruction.

Programmer often codes a symbol that is associated with a BYTE, DWORD, or WORD directive in the data segment or with an instruction in the code segment.

The location corresponding to such a symbol will be relocatable so that the assembler listing shows an assembly-time address that may be adjusted later.

ADD eax, number2;05 00000004

Note:

value BYTE 100MOV eax , value

;;;;;

value EQU 100MOV eax , value

Register mode

direct modeRegister mode

immediate mode

Register indirect mode

The offset of the memory which stores operand is contained in register.

ADD eax , [edx] ; [edx]->eax, 03 02

operandlike pointervs.ADD eax , edx ; edx ->eax

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 ambi

guous, 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 o

r 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] ;16-bit, CX = DS:[BX+DI]

MOV CH,[BP+SI] ;8-bit, CH = SS:[BP+SI] For 32-bit: allows the combination of any two 3

2-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],CLMOV LIST[BP+SI+4],DHMOV 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 EAX,[EBX+4*ECX]MOV [EAX+2*EDI+100H],CXMOV AL,[EBP+2*EDI-2]MOV EAX,ARRAY[4*ECX]MOV 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, 2P67. Exercises3.4 2, 3, 4P72. Exercises3.5 1-32P77. Exercises3.6 1-8P80. Exercises3.7 1-6

top related