6.1) assembly language program format 6.2) features of assembly language 6.3) data definition...
Post on 21-Dec-2015
221 views
TRANSCRIPT
6.1) Assembly Language Program Format
6.2) Features of Assembly Language
6.3) Data Definition
CHAPTER 6
ASSEMBLY LANGUAGE PROGRAM FORMAT AND DATA
DEFINITION
Format for Assembly Language Program
There are 2 types of executable programs: 1. .COM program2. .EXE program
.COM program – consist of one segment that contains code, data and the
Stack – is useful as a small utility program or as a resident program
(one that is installed in memory and is available while other programs run)
.EXE program – consist of separate code, data and stack segments. – is used for more serious programs.
Assembly language can be written by using either .COM or .EXE
format.
Observe that each segment (data, code and stack) of the .EXE program is defined separately whereas in .COM, no separate segment for data and stack.
PSP (program segment prefix)
stack
data segment
code segment
PSP
code segment
.EXE .COM
ES
SS
DS
CS
ESDSCS
The statement ASSUME in .COM states that the CS, DS, SS and ES registers will have the same starting address for code segment.
PAGE directive to establish 60 lines and 132 columns per page
TITLE directive to identify the program’s name as A04ASM1
; symbol is for comment
STACK to define the stack segment
DATASEG to define the data segmentCODESEG to
define the code segment
ASSUME directive is used to tell the assembler the starting address of segments with the segment registers
Initialize the address of data segment in DS
Procedure MAIN
END directive to tell the assembler that this is the end of the source program
Request to end the program and return to the OS
Ending Program ExecutionAfter executing an assembly language program, the
programmer must tell the system to terminate the executing program with the help of DOS interrupt services.
INT 21H is the commonly used interrupt service. It used the function code in the AH register to determine the next course of action.
INT 21H can also be used to control input from the keyboard, control the screen, disk I/O and output to the printer.
INT 21H with function code 4CH is used to terminate program execution. The function code 4CH must be priory entered into AH. Example:
Examples of .EXE program• Example
addition operation of 2 numbers, 215 and 125.
• Values 215 and 125 are defined in the data segment using names (FLDD and FLDE) with the size of each is one word (DW = Define Word = 16 bit or 2 bytes).
• The result is kept in FLDF, which its size is also one word.
• The AX register is used to hold operand 1 and also the result. Then the result is put into FLDF
Examples of.COM program
• value 215 and 125 is defined in the data segment using names (FLDD and FLDE) with the size of each is one word (DW = Define Word = 16 bit or 2 bytes)
• The result is kept in FLDF, which its size is also one word.
• The AX register is used to hold operand 1 and also the result. Then the result is put into FLDF
Features of Assembly Language
• A few features of the Assembly Language that will be discussed here are:
1) Program Comments2) Reserved Words3) Identifiers4) Statements5) Directives
1. Program Comments
• Comments start with a semicolon ‘;’. All characters written on the right side of the semicolon is considered as a comment (will not be executed). Below are some examples of comments: – In a row of its own
• ; Comment Example– In the same line with other commands
• ADD AX, BX ; Adds the value of BX and AX registers
• Note: Comments will not be changed into machine code, hence the length of a comment will not influence the size of the program in machine code
2. Reserved Words
• Certain names in assembly language are reserved for their own purposes, to be used only under special conditions. Reserved words, by category include: – Instructions: such as MOV and ADD which are
operations that the computer can execute; – Directives: such as END and SEGMENT which
you use to provide information to the assembler;– Operators: such as FAR, SIZE which you use in
expressions – Predefined Symbols: such as @Data, @Model
which return information to your program during assembly
4. Statements• An assembly program has a set of statements:
– Instructions such as MOV and ADD that will be translated into machine code or object code.
– Directives that tells assembler to perform certain actions like define a data item etc
5. Directives• Directives are statements that enable the programmer to
determine how the source program is arranged. It will not be changed into machine code. The following are a few directives:
SEGMENT defines the starting of a segment. Segment name must be stated uniquely and must adhere to the rules of naming. ENDS shows the end of the segment and has the same name like SEGMENT. The maximum size of a segment is 64K. Operands for SEGMENT may contain:
Align: option that shows the boundary on which the segment 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 or 10H. The omission of the align operand causes the assembler to default to PARA.
Combine: option that indicates whether to combine the segment with other segments when they are linked after assembly. The types of combine are STACK, COMMON, PUBLIC and AT. For instance, Stack segment is usually defined as:
Segment name SEGMENT PARA STACK
Class: option that is used to group related segments when linking. This book uses the classes ‘code’ for code segment, ‘data’ for data segment and ‘stack’ for stack segment.
5. Directives (cont.)
iii) PROC Directive
The code segment contains executable code for a program, which consists of one or more procedures, defined initially with the PROC directive with the ENDP directive. Here is the format :
Procedure name (procedure-name) must be present, must be unique and must follow assembly language naming conventions
iv) END Directive
There are 3 END directives:
END: terminates the program (last statement in assembly language)
ENDS: ends a segment
ENDP: ends a procedure
v) ASSUME Directive
.EXE program uses SS register to address the stack segment , DS register for data segment and CS register for code segment. The ASSUME directive is used to tell the assembler the relation of the three registers with the segment:
ASSUME SS:stackname, DS:datasegname, CS:codesegname, …
SS:stackname means the SS register is associated with the name for the stack segment. Same applies to DS and CS regarding data segment and code segment.
Data Definition
Assembler offers a few directives that enable programmers to define data according to its type and length. Format for data definition:
[name]
Data names are optional because in assembly language programming, data is not necessarily reference by its name.
Dn Directive
Next slide are the common directives to define data and also directives used in MASM 6.0
The following are some examples of numeric and character data definition
Page 60, 132TITLE A04DEFIN (EXE) Define data directives
.MODEL SMALL
.DATA;DB – Define Bytes:;-----------------------BYTE1 DB ? ; UninitializedBYTE2 DB 48 ; Decimal constantBYTE3 DB 30H ; Hex constantBYTE4 DB 01111010B ; Binary constantBYTE5 DB 10 DUP (0) ; Ten zerosBYTE6 DB ‘PC FAIR’ ; Character stringBYTE7 DB ‘12345’ ; Number as charactersBYTE8 DB 01, ‘Jan’, 02, ‘Feb’ ; 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 6 DUP (0) ; Six zeros
;DQ – Define Doublewords:;---------------------------------DWORD1 DD ? ; UninitializedDWORD2 DD 41562 ; Decimal valueDWORD3 DD 24, 48 ; Two constantsDWORD4 DD BYTE3 – BYTE2 ; Difference between addressesEND
DB or BYTE-to define item with the size of one byte. The range of its value is stated in the table before.
DW or WORD-to define item with the size of one word or 2 bytes. The range of its value is stated in the table before. Assembler will change numeric constants into binary object code (presented in hexadecimal) and kept in the memory in reverse bytes. For instance, if the real value is 3039H it will be kept as 3930H in the data segment
DD or DWORD- to define item with the size of 4 bytes. Its range of values is stated in the table above. As usual data is kept in reverse byte or reverse sequence. For example, if data is 00BC614EH it will be kept as 4E61BC00H.
Expressions
Expressions in operand may specify an uninitialized value or a constant value. Example:
DATAX DB ? ; Uninitialized item, size of 1 baitDATAY DB 25 ; Initialized item, DATAY with value 25
Uninitialized item is used to store a value which size is defined. The value of a data can be used and edited to suit the program’s needs. Expressions can contain a few constants that is separated by the sign ‘,’ and the quantity is limited to the row length.
Example: DATAZ DB 21, 22, 23, 24, 25, 26, …
The assembler defines the above constant byte by byte , from left to right. DATAZ or DATAZ+0 contains the value 21, DATAZ+1 contains 22, DATAZ+2 contains 23 and so forth. Example of instruction MOV AL, DATAZ+3 will enter the value 24 into the CL register
Expressions also allows duplication of constants using the format below:
Example:DW 10 DUP(?) ; Ten words, uninitializedDB 5 DUP(12) ; Five bytes containing
0C0C0C0C0CDB 3 DUP(5 DUP(4)) ; Fifteen 4s
Character Strings
Character strings are defined either using single quotes like ‘name’ or double quotes, “name”. The content in quotes will be kept as object code in ASCII format Example:
DB “Crazy Sam’s CD Emporium” ; double quotes for string,
; single quotes for apostrophe.DB ‘Crazy Sam’’s CD Emporium’ ; single quotes for string,
; two single quotes for apostrophe.
Numeric Constants
Numeric constant is used to define the numeric value and the memory address. Below are a few numeric format:
Binary: use binary digit 0 and 1 followed with radix specifier B. Example: 01001100B.
Decimal: use decimal digits of 0 to 9, followed by radix specifier D or none. Example 125D or 125.
Hexadecimal: use hexadecimal digits of 0 to 9 and A till F, followed by radix specifier H.
Real: it is decimal or hexadecimal constant followed by the radix specifier R. Assembler will change the value into floating point format.