1、Part B,Fundamentals of Assembly Language,Chapter 3,Elements of Assembly Language汇编语言的要素,Textbook: 4 Requirement for Coding in Assembly Language,Outline,the basic elements for deveopling an assembly program: the use of comments, the general coding format, the directives for controlling the assemble p
2、rogram listing, the requirements for defining segments and procedures, and the requirements for defining data items,Contents,Statement (语句) Identifiers (标识符) Reserved words (保留字) The use of comment (注释) Operand (操作数) Data Definition (数据定义) Expression (表达式),Contents,Directive (伪指令) 1. Segment directi
3、ve 2. PROC directive3. Assume directive 4. End directive 5. Simplified segment directives 6. EQU equivalence directive 7. ORG directive Addressing (寻址),Assembly Language Statements汇编语句,3.1,Assembly Language Statements,An assembly program consists of a set of statements. 汇编语言源代码文件由一系列语句组成.Comments (注
4、释): can be used with any statement. A comment generates no machine code, without affecting the assembled programs size or execution.,Comment (注释),can improve the programs clarity, especially in assembly language. (程序可读性更好) A semicolon (;) begins the comment, and the comment extended until the end of
5、 the line.注释的开头用分号“;”标识,直到行尾是注释。斜杠“”可将一个注释语句拆分成几行,紧跟斜杠后的部分也是注释。 不产生机器码,不影响汇编程序的大小和执行。,ADD AX, BX ; accumulate total quantity,Examples,Statement elements,A statement always contains 4 parts: mnemonic, identifier, operand, and comment. They must be in the order: identifier mnemonic operand(s) ;comment
6、 标识符 助记符 操作数 ;注释An identifier, mnemonic, operand are separated by at least one blank or tab character,(Statement),Instruction (指令性语句), which the assembler translates to object code;label: operation operand(s) ;comment,Three Types of Statements (1) :,L1: MOV AX, 0 ;move 0 to AX,Examples,Textbook: P54
7、 - 55,(Statement),Directives (指示性语句), which only tell the assembler to perform a specific action, such as define a data item. Directives generates no machine code. name/variable directive expression/parameter (s);comment,Three Types of Statements (2) :,counter DB 1 ;define byte with 1 value,Examples
8、,END SEGMENT,(Statement),Macros (宏语句) is “shorthand” for a sequence of other statements, be they instructions, directives, or even other macros.,Three Types of Statements (3) :,Contents,Statement (语句) Identifiers (标识符) Reserved words (保留字) The use of comment (注释) Operand (操作数) Data Definition (数据定义)
9、 Expression (表达式),Identifiers (标识符),An identifier (symbol) is a name that you apply to an item in your program that you expect to reference.,Identifiers (标识符),The two types of identifier: 1. Label: refers to the address of an instruction, procedure or segment, such as Main in the following statement
10、.标号:指指令、过程、段的地址,Main: PROC FAR,Identifiers (标识符),The two types of identifier: 2. Name/variable: refers to the address of a data item, such as COUNTER in名字/变量:指一个数据项的地址,COUNTER DB 0,Identifiers (标识符),The two types of identifier: 1. Label2. Name/variable,Main: PROC FAR,COUNTER DB 0,Examples,An identif
11、ier can use the following characters:,Alphabet (字母):A through Z and a through zDigits (数字): 09 (not the first character)Special characters (特殊字符):question mark, underline, dollar, at, dot(? _ $ . ),The first character of an identifier must be an alphabetic or a special character, except for the dot.
12、 Reserved words can not be used as your own identifiers. The assembler treats uppercase and lowercase the same. The maximum length of an identifier is 31 characters up to MASM6.0 and 247 since.,An identifier can use the following characters:,Descriptive, meaningful names are recommended. Because the
13、 assembler use some special words that begin with the symbol, you should avoid using it for your own definitions.,identifier,Examples,Total, qty25, $P50,Reserved Words(保留字),Certain names in assembly language Instructions, such as MOV and ADD Directives, such as END and SEGMENT Operators, such as FAR
14、 Predefined symbols, such as DataSee Appendix C for a list of reserved words.,Contents,Statement (语句) Identifiers (标识符) Reserved words (保留字) The use of comment (注释) Operand (操作数) Data Definition (数据变量定义) Expression (表达式),Operand操作数,The operation is most commonly used for defining data area and codin
15、g instructions. For a data item, an operation such as DB or DW defines a field, work area, or constant. For an instruction, an operation such as MOV or ADD indicates an action to perform.The operand (if any) provides information for the operation to act on. 操作数向指令提供数据来源,Operand操作数,For a data item, t
16、he operand defines its initial value. For example, in the following definition of a data item named COUNTER, the operation DB means “define byte”, and the operand initializes its contents with zero value.,Examples,COUNTER DB 0,Operand操作数,For an instruction, an operand indicates where to perform the
17、action. It provides a source of data for an instruction to process.,MOV AX, 0,Examples,Operand操作数,Some instructions do not require an operand whereas other instructions may have one or two operands. 指令可能需要0/1/2个操作数。多个操作数中间用逗号分隔,可以加空格Many instructions have 2 operands. In general, the 1st one gives th
18、e destination of the operation. The 2nd one gives the source for the operation. 2个操作数的指令: 前者给出目标,后者给出源.,Operand操作数,Operand item consist of constant, register, label, name/variable, or expression (one or more).操作数项可为:常数 寄存器 变量 表达式,Contents,Statement (语句) Identifiers (标识符) Reserved words (保留字) The use
19、 of comment (注释) Operand (操作数) Expression (表达式) Data Definition (数据定义),Expression 表达式,Expression is an operand, It consist of constant, register, label, name/variable and combining operator . 表达式是一个操作数,由常量 、寄存器、标号、名字/变量、算术运算符组成。Expression is divided by value expression, and address expression. 表达式分为
20、数值表达式和地址表达式,1 Value Expression 数值表达式,Value expression is a expression that its value can be calculated by assembler during assembling.数值表达式是在汇编过程中能够由汇编程序计算其值的表达式,其组成部分在汇编时能完全确定。通常是一些常量的运算组合。,1 Value Expression,1 Constant 常量 2 Arithmetic operators 算术运算符 3 Relation operators 关系运算符 4 Logical operators
21、逻辑运算符 5 Other operator in value expression 表达式中的其它操作符 6 Operator priority 注意运算符和操作符的优先级,1 Value Expression,1 Constant 常量 Constant is an immidiate mode operand, the data to be used is built into the instruction before it is executed. 常量是一个立即数,直接写在汇编语言语句中,在程序的执行过程中,它不能发生变化。Numeric operands can be expr
22、essed in digital, hexadecimal, binary, or octal notations. Eg. 10101011B, 324Q, 1234D, 1234H, 0abcdH, AB,1 Value Expression,2 Arithmetic operators 算术运算符 Sign: +, - Operators:+, -, *, /, MOD(取模) Eg. 120+(321-90) mod 3, 322*5/32These expressions are evaluated by the assembler at assembly time, not at
23、run time, with the resulting value used for assembly.汇编时,而非运行时,汇编器会对表达式求值.,1 Value Expression,3 Relation operators 关系运算符 EQ(相等), NE(不等), LT(小于), GT(大于), LE(小于等于), GE(大于等于)。4 Logical operators 逻辑运算符 AND(逻辑与)、OR(逻辑或)、NOT(逻辑非) XOR(异或)、SHL(左移位)和SHR(右移位)。,2 Address expression,Address expression is a expr
24、ession that its value can be calculated by assembler during assembling.是计算存储单元地址的表达式,其计算结果表示一个存储单元的地址,而非该存储单元的值。 address value = address,2 Address expression,address value = addressB1+1 = next location address of B1Note: Its not adding 1 with content of B1 location,Examples,2 Address expression,Addr
25、ess expression B1+1, B1+3, W1+2:The content of “B1+1”, “B1+3” and “W1+2” is 11H , A and 5678H.,Examples,3. Valuereturning operator,TYPE variable or label ; return type attribute value of variable or label SEG variable or label ;return the segment address of segment where the variable or label is loc
26、ated inOFFSET variable or label ; return offset address of variable or label,SEG (段属性操作符 ),return the segment address of segment where the variable or label is located in 返回该标识符所在段的段地址Is AX equal to BX ? Yes.,Examples,OFFSET (偏移量属性操作符),;return offset address of variable or label 返回该标识符离它所在段的段地址有多少字节
27、。BX=AX+4,Examples,TYPE(类型属性操作符),; return type attribute value of variable or label 返回该变量所占字节数,或标号的“远”(FAR)、“近”(NEAR)类型TYPE B1=1 TYPE W1=2,Examples,4. Attribute operator (强制属性操作符),PTR compelling to appoint or change temporarily the type of variable and label. 使指令中存储单元操作数具有明确的属性Format:,type PTR addres
28、s-expression数据类型 PTR 地址表达式,PTR (强制属性操作符),MOV SI, 1 ;error MOV BYTE PTR SI, 1 ;appoint byte type MOV WORD PTR SI, 1 ;appoint word type,Examples,Contents,Statement (语句) Identifiers (标识符) Reserved words (保留字) The use of comment (注释) Operand (操作数) Expression (表达式) Data Definition (数据定义),Define Types of
29、Data,The assembler provides a set of directives that permits definitions of items by various types and lengths.For example, DB defines byte and DW defines word.A data item may contain an undefined (that is, uninitialized) value, or it may contain an initialized constant, defined either as a characte
30、r string or as a numeric value.,Data definition,Usually the programmer will code a symbol that is associated with a BYTE, DWORD, or WORD directive in the data segment or with an instruction in the code segment.通常情况下,在数据段中程序员用一个标识符来表示相应的单字节、双字或字。或者在代码段中,用标识符表示指令。,Data definition,number2 DW ? add eax,
31、 number2,Examples,Data definition,Any memory mode operand specifies using data in memory or specifies a destination in memory.使用内存单元的数据作为操作数时,可以在内存中指定使用数据,或者在内存中指定一个目的地址.number2 DW 5 add eax, number2 ;number2 is a memory operand.,Examples,内存数据,访问内存单元的数据时,可通过存储单元的偏移量(有效地址)来访问它。另一种访问方法是给存储单元取一个符号名,然后通
32、过引用该符号名来访问其所对应的存储单元。,Format for data definition: (数据定义的伪指令语句一般格式),变量名 数据定义符 表达式1,表达式n ;注释,Name/Variable: Names of data items should be unique and descriptive. The name is otherwise optional, as indicated by the square brackets.,Directive (Dn) : The directives that define data items are DB (byte) DW
33、(word) DD (double-word) etc. It explicitly indicates the length of the defined item.,Expressions: The expression in an operand may specify an uninitialized value or a constant value. An expression may contain multiple items separated by commas(,) and limited only by the length of the line, as follow
34、s: Flag DB 21, 22, 23, 24, 25, 表达式定义内存单元的初值表达式,可有多个,用逗号分开;若某存储单元没有初值表达式,则用问号?表示,Flag DB 21, 22, 23, 24, 25, The assembler defines these constants in adjacent bytes, from left to right. A reference to Flag is to the first 1-byte constant, 21, and a reference to Flag+1 is to the second constant, 22. F
35、or example, the instruction “MOV AL, Flag+3” loads the value 24 into the AL register.,The conventional directives used to define data,Data definition directive listDescription Conventional MASM6.0 type AttributeDirectives Directives ValueByte DB byte 1 Word DW word 2 Double word DD Dword 4 Far word
36、DF Fword 6 Quad word DQ Qword 8 Ten bytes DT Tbyte 10,DB/BYTE (字节变量),The length of the defined item is one byte. (每个变量占1字节单元)Hexadecimal notation is used in the following figure.,Examples,DW/WORD(字变量),It reserves 2 adjacent bytes of memory. (每个变量占用两个连续的字节单元) 由于字变量的数据是按照“高高低低”的原则存于存储单元之中的,而字节数据是按照排列顺
37、序存于存储单元中的,所以,它们的存储方式有所不同。,Examples,DD/DWORD(双字变量),It reserves 4 adjacent bytes of memory.(每个字占用四个连续的字节单元),Examples,Flag DB ? ;expression define an un-initialized value, but it must fit the defined size,Data definition,Data definition,Examples,Flag DB 21, 21, 21, 21, 21, The expression permits duplic
38、ation of constants in a statement of the format.,Repeat operand DUP (重复说明符),DW 10 DUP (?) ;10 words, uninitialized DB 5 dup (12) ;5 bytes, containing hex 0C0C0C0C0C DB 3 Dup (5 DUP(4) ;15 4sBuffer1 DB 5, 0, 5 DUP(?) ;7 byteBuffer2 DW 25 DUP(ab) ;25*2 byteBuffer3 DW 1, 5DUP(1,2,4 DUP(0) ;62 byte,Exam
39、ples,Addressing Data in Memory,Internal memory,Memory is used to store program instruction and data. Memory hold its contents until loading in new contents. The information in a memory location is called the content of this memory location.,Internal memory,Bytes in memory are numbered consecutively
40、, beginning with 00 , so that each location has a uniquely numbered address.The address in computer is represented by Binary, but it is conveniently written with Hexadecimal.,Addressing Data in Memory,Consider the number 0529H. It requires 2 bytes, or one word, of memory. It consists of a high-order
41、 byte, 05, and a low-order byte, 29. The processor stores the data in memory in reverse-byte sequence: The loworder byte in the low memory address The highorder byte in the high memory address,内存高高低低的原则,Addressing Data in Memory,Processor automatic transfer 0529H into memory addresses 04A26H and 04A
42、27H04A26H byte unit content: (04A26H)=29H,Examples,Word unit content is represent by lowest address:(04A26H)=0529H,Addressing Data in Memory,The data defined in the following statements are stored in 10 memory units. Please write down the arrangement of the data in the memory. 假设:这些数据存放在10个内存单元中, 请列
43、出这10个单元中的数据。,Question,Addressing Data in Memory,Answer,Contents,directive (伪指令) 1. Segment directive 2. PROC directive3. Assume directive 4. End directive 5. Simplified segment directives 6. EQU equivalence directive 7. ORG directive,Directives,Act by assembler only during the assembly of a program
44、and generate no machine-executable code. 在程序的汇编过程中指示汇编程序如何汇编源程序,但不产生机器码Function: defining data, assigning location, pointing ending of a program, and so on. 功能:定义数据、分配地址,指示程序结束等,Segment directive (段定义伪指令),An assembly program in .EXE format consists of one or more segments. In real mode, a stack segm
45、ent defines stack storage, a data segment defines data items, and a code segment provides for executable code.The directives, SEGMENT and ENDS, are used to define a segment.,Segment directive (段定义伪指令),Format:,Segment directive (段定义伪指令),The align option (对齐类型) indicates the boundary on which the segm
46、ent is to begin. 表示当前段对起始地址的要求 The typical requirement is PARA, which causes the segment to align on a paragraph boundary so that the starting address is evenly divisible by 16.,Segment directive (段定义伪指令),The combine option (组合类型) indicates whether to combine the segment with other segment when are
47、linked after assembly. 告诉连接程序如何合并不同模块中段名相同的段,Segment directive (段定义伪指令),The class option (类别类型) is used to group related segments when linking. 连接程序利用该类别来调整同名同类别的段,并使它们相邻,Segment directive,Data segment:Code segment:,Examples,The code segment contains the executable code for a program, which consists
48、 of one or more procedures.The procedures are defined with the PROC directive and ENDP directive.,PROC directive (过程定义伪指令),The procedure-name must be present, must be unique, and must follow assembly language naming conventions. The ENDP directive indicates the end of a procedure.,PROC directive (过程
49、定义伪指令),The operand “Attribute” is related to program execution. You may use “Far” and “Near” (default).,PROC directive (过程定义伪指令),Attribute,A procedure that is in the same segment as calling procedure is a NEAR procedure and is accessed by an offset. 子程序和调用者在同一个段中,称近类型A called procedure is external to the calling segment is FAR attribute, and is accessed by segment:offset. 如果一个子程序要被另一段的程序调用,类型为FAR,