28
Chapter 4 Requirements for Coding in Assembly Languag

Chapter 4 Requirements for Coding in Assembly Language

Embed Size (px)

Citation preview

Chapter 4

Requirements for Coding in Assembly Language

Assembly Language Features:

Program Comments, 註解 Reserved Words 保留字 Identifiers 認別字 Statements

Directives 假指令

Program Comments

1. The use of comments throughout a program can improve its clarity. 清楚 A comment begins with a semicolon (;), all character on the line to the

right are comments.

;Calculate productivity ratio

ADD AX, BX ;Accumulate total quantity

2. You may include any number of comments without affecting the

assembled program’s size or execution.

3. Using COMMENT Directive: COMMENT + This routine scans keyboard input for + invalid characters. Delimiter: the first nonblank character, such as %, +, following COMMENT.

Reserved Words:

instructions: MOV, ADD, operations that the computer can execute

directives: END, SEGMENT, providing information to the assembler

operators: FAR, SIZE, which you use in expressions

predefined symbols: @Data, @Model, which return information to your

program during the assembly.

Identifiers:

A name you apply to an item in your program that you expect to reference.

Two types of identifiers: name, label.

Name refers to the address of a data item:

COUNTER DB 0

Label refers to the address of an instruction, procedure, or segments

MAIN PROC FAR B30: ADD BL, 25

An identifier can use: alphabetic letters, digits, and special characters (?, _,

$, @, dot .). The first character of an identifier must be an alphabetic letters not use in the first characteror special character, except for the dot.

Statements:

Two types of statements: instructions, directives 指令 假指令Format for a statement:

[identifiers] operation [operand (s)] [;comment]

Directive: COUNT DB 1 ;name, operation, operand

Instruction: L30: MOV AX, 0 ;name, operation, 2 operands

operation: defining data areas (DB, DW) and coding instructions (MOV).

operand: providing information for the operation to act on.

RET ;return from a procedure INC BX ;increment BX register by 1 ADD CX, 25 ;add 25 to CX register

Directives:

The PAGE and TITLE Listing Directives:

PAGE [length] [,width] PAGE 60, 132 maximum number of lines = 60, maximum number of characters per line = 132

TITLE directive causes a title to print on line 2 of each page on program listing.

TITLE text [comment]

TITLE ASMSORT Assembly program to sort CD titles

Segment Directive:

a stack segment defines stack storage, a data segment defines dataitems, a code segment provides for executable code.

NAME OPERATION OPERAND

segment-name SEGMENT [align] [combine] [‘class’]

… segment-name ENDS

align: indicate boundary, PARA (default): align on paragraph boundary (10H)combine: indicate whether to combine the segment with other segments when they are linked after assembly. Using PUBLIC and COMMON to combine separately assembled programs when linking them.class: group related segments when linking. ‘code’ for code segment, ‘data’ for data segment, ‘stack’ for stack segment.

Defining segments for an .EXE program:

Page 60,132TITLE A04ASM1 ;------------------------------------------------------STACK SEGMENT PARA STACK ‘Stack’ …STACK ENDS;-------------------------------------------------------DATASEG SEGMENT PARA ‘Data’ …DATASEG ENDS;--------------------------------------------------------CODESEG SEGMENT PARA ‘Code’ MAIN PROC FAR …MAIN ENDP ;end of procedureCODESEG ENDS ;end of segment END MAIN ;end of program

PROC Directive: NAME OPERATION OPERAND COMMENTprocedure-name PROC FAR ;begin procedure …procedure-name ENDP ;end procedure

FAR: related to program execution, the program loader uses this procedure as the entry point for the first instruction to execute.

NEAR: other additional procedures in the code segment is coded with the NEAR operand.

END Directive:

An END directive ends the entire program and appears as the laststatement.

END [procedure name]

The operand may be blank if the program is not to execute. ( fordata definitions or programs going to be linked with another module)

In most programs, the operand is the name of the first PROC designatedas FAR, where the program execution is to begin. 指定

ASSUME Directive:

An .EXE program uses the SS register to address the stack,

DS to address the data segment, CS to address the code segment.

ASSUME SS: stackname, DS: datasegname, CS: codesegname,…

SS: stackname tells the assembler to associate the name of the stack 關聯segment with the SS register.

Conventional Segment Directives Page 60, 132TITLE A04ASM1 Skeleton of an .EXE program;-------------------------------------------------------------------------STACK SEGMENT PARA STACK ‘Stack’ …STACK ENDS;--------------------------------------------------------------------------DATASEG SEGMENT PARA ‘Data’ …DATASEG ENDS;--------------------------------------------------------------------------CODESEG SEGMENT PARA ‘Code’MAIN PROC FAR ASSUME SS: STACK, DS: DATASEG, CS: CODESEG MOV AX, DATASEG ;set address of data MOV DS, AX ; segment in DS … MOV AX, 4C00H ;end processing INT 21HMAIN ENDP ;end of procedureCODESEG ENDS ;end of segment END MAIN ;end of program

1. By associating segments with segment registers, the assembler

can determine offset addresses for items in the segments.

2. initialize the address of the data segment in DS MOV AX, DATASEG MOV DS, AX the processor allows you to move data only from a general-purpose register to a segment register.

3. Ending program execution: INT 21H, function code 4CH which INT 21H (a common DOS interrupt) recognizes as a request

to end program execution

Example of a Source Program Page 60, 132TITLE A04ASM1 (EXE) Move and Add operations;----------------------------------------------------------------------------STACK SEGMENT PARA STACK ‘Stack’ DW 32 DUP(0)STACK ENDS;----------------------------------------------------------------------------DATASEG SEGMENT PARA ‘Data’FLDD DW 215FLDE DW 125FLDF DW ?DATASEG ENDS;----------------------------------------------------------------------------CODESEG SEGMENT PARA ‘Code’MAIN PROC FAR ASSUME SS:STACK, DS:DATASEG, CS:CODESEG MOV AX, DATASEG MOV DS, AX MOV AX, FLDD ;move 0215 to AX ADD AX, FLDE ;add 0125 to AX MOV FLDF, AX ;store sum in FLDF MOV AX, 4C00H ;end processing INT 21HMAIN ENDP ;end of procedureCODESEG ENDS ;end of segment END MAIN

1. STACK contains one entry, DW (Define Word) that defines 32 words initialized to zero.

2. DATASEG defines three words named FLDD (initialized with 215), FLDE (125), and FLDF (un-initialized).

3. CODESEG contains the executable instructions for the program.

4. When loading a program from the disk into memory for execution, the loader sets correct segment addresses in SS and CS, but the program has to initialize DS ( and usually ES).

Simplified Segment Directives

1. a shortcut in defining segments

2. initialize memory model before defining any segment

3. different models tell assembler how to use segments, to provide enough space for object code, and to ensure optimum execution speed.

. MODEL memory-model

MODEL number of code segments number of data segment Small <= 64 K <= 64 K Medium any size <= 64 K Compact <= 64K any size Large any size any size Huge any size any size

Tiny model is intended for the use of .COM programs with data, code, stack in one 64K segment.

1. The Small model is suitable for most of the examples in this book.

The assembler assumes that the addresses are near (within 64K)

and generates 16-bit offset addresses.

2. For Compact model, the assembler may assume 32-bit addresses,

which require more time for execution.

3. The Huge model is the same as Large, but may contain variables

such as arrays greater than 64K.

4. The .MODEL directive automatically generates the required ASSUME

statement for all models.

5. the formats define the stack, data, and code segments:

.STACK .DATA .CODE

EXE program with simplified segment directives page 60, 132TITLE A04ASM2 (EXE) Move and add operations;--------------------------------------------------------------------------- .MODEL SMALL .STACK 64 ;define stack .DATA ;define dataFLDD DW 215FLDE DW 125FLDF DW ?;---------------------------------------------------------------------------- .CODE ;define code segmentMAIN PROC FAR MOV AX, @data ;set address of data MOV DS, AX ; segment in DS MOV AX, FLDD ;move 0215 to AX ADD AX, FLDE ;add 0125 to AX MOV FLDF, AX ;store sum in FLDF MOV AX, 4C00H ;end processing INT 21HMAIN ENDP ;end of procedure END MAIN ;end of program

Defining Types of Data

Format for data definition: [name] Dn expression

Name: used to reference a data item by the program

Directive (Dn): DB (byte), DW (word), DD (double word) DF (farword, 6 bytes), DQ (quadword, 8 bytes), DT( ten-bytes)

Expression: un-initialized item DATAX DB ? a constant DATAY DB 25 multiple constants DATAZ DB 21, 22, 23, 24, 25, …

MOV AL, DATAZ+3 loads value 24 (18H) into the AL register Duplication of constants DW 10 DUP(?) ; ten un-initialized words DB 5 DUP(12) ; five bytes of 0CH DB 3 DUP(5 DUP(4)); fifteen 4s

Character strings: DB ‘Computer City’ DB “Crazy Sam’s CD Emporium” DB ‘Crazy Sam’’s CD Emporium’

Numeric Constants: define arithmetic values and memory location

Binary: 0,1 followed by radix specifier B, used in bit-handling instructions AND, OR, XOR, and TEST

Decimal: 0~9[D] such as 125 or 125D, converted by assembler into 7D

Hexadeicimal: 0~F followed by H, the first digit should be 0~9, such as 3DH, 0DE8H which assembler stores as 3D and E80D

Real: the assembler converts a real value into floating-point format DB ’24’ generates two ASCII characters: represented as hex 3234 DB 24 represented as hex 18

page 60,132TITLE A04DEFIN (EXE) Define data directives

.MODEL SMALL

.DATA;; DB - Define BytesBYTE1 DB ? ;Un-initializedBYTE2 DB 48 ;Decimal constantBYTE3 DB 30H ;Hex constantBYTE4 DB 01111010B ;Binary constantBYTE5 DB 10 DUP(0) ;Ten zerosBYTE6 DB 'PC Emporium‘ ;Character stringBYTE7 DB '12345' ;Numbers as charsBYTE8 DB 01,'Jan',02,'Feb',03,'Mar‘ ;Table of months;; DW - Define Words:WORD1 DW 0FFF0H ;Hex constantWORD2 DW 01111010B ;Binary constantWORD3 DW BYTE8 ;Address constantWORD4 DW 2,4,6,7,9 ;Table of 5 constantsWORD5 DW 8 DUP(0) ;Eight zeros

;; DD - Define Doublewords:DWORD1 DD ? ;Un-initializedQWORD3 DQ 41562 ;Decimal constant

END

DWORD2 DD 41562 ;Decimal valueDWORD3 DD 24, 48 ;Two constantsDWORD4 DD BYTE3 - BYTE2 ;Diff between addresses;; DQ - Define QuadwordsQWORD1 DQ 0 ;Zero constantQWORD2 DQ 05E39H ;Hex constant

Assemble and link the program. Then use debug to load the .EXE file and

(debug a04defin.exe)

key in D DS:100 for a display of the data. ( because the loader set DS

With the address of the PSP, but the program begins 100 bytes after that.

EQUATE DIRECTIVES

1. define symbolic names with other names and numeric values

2. do not generate any data storage, the assembler uses the defined value to substitute in other statements

3. program is more readable and easier to maintain when many statements uses the assigned values.

The equal-sign directive: value_of_pi = 3.1416 right_col = 79 screen_positions = 80*25

Examples of the use of the preceding directives: IMUL AX, value_of_pi ;multiply AX by 3.1416 CMP BL, right_col ;compare BL to 79 MOV CX, screen_positions ;move 2000 to CX

The EQU Directive

FACTOR EQU 12 TABLEX DB FACTOR DUP(?) is equal to TABLEX DB 12 DUP(?)

RIGHT_COL EQU 79 … MOV CX, RIGHT_COL ;move 79 to CX

ANNL_TEMP DW 0 … AT EQU ANNL_TEMP MPL EQU MUL

The TEXTEQU Directive

For example:

PROMPT_MSSGE TEXTEQU <‘Add, Change, or Delete?’>

And use it like this:

USER_PROMPT DB PROMPT_MSSGE

The assembler converts this definition to

USER_PROMPT DB ‘Add, Change, or Delete?’

Practice 1 .data num1 DB 20H num2 DB 0A2H result DW ? msg1 DB ‘end of operations’, ‘$’1. Write an assembly program using conventional segment definitions for the following: (a) move the content of num1 to the AL register, (b) shift AL contents two bit left: MOV CL, 2 SHL AL , CL (c) move contents of num2 to BL, (d) multiply AL by BL (MUL BL). (e) store the product to result: MOV result, AX (f) display msg1. Remember the instruction to end program execution. Assemble, link and use DEBUG to trace and to check registers and flags.

2. the following program contains a number of assembly-time errors, as

(see next page)

indicated to the right. Correct each error.TITLE A04ASM2

.MODEL SMALL

.STACK 64

.DATADATA1 DB 25DATA2 DB 280 ;1: value out of rangeDATA3 DW ?

.CODEMAIN PROC

MOV AX, data ;2:improper operand typeMOV DS, AXMOV AX, DATA1 ;3:operand types must matchADD AX, DATA2 ;4: operand types must matchMOV DATA3, AXMOV FX, 4C00H ;5: Symbol not definedINT 21H

MAIN ENDPEND MAIN

The assembler did not locate the error about the MAIN PROC statement;What is it?