CPE 408330� Assembly Language and�Microprocessors��Chapter 7: Assembly Language �Program Development with MASM�������
[Computer Engineering Department,
Hashemite University, © 2008]
Lecture Outline
2
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program
Assembly language statement syntax
Directive statement syntax
Constants in a statement
Operand expressions using the arithmetic, relational, and logical operators
Value-returning and attribute operators
3
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program
4
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program
Assembly language statement syntax:
LABEL: OPCODE OPERAND(S) ;COMMENT(S)
START: MOV CX, 10 ;Load a count of 10 into register CX
MOV CX, 10 ;Initialize the count in CX
CLC ;Clear the carry flag
*. Leave one blank space between fields and one space before the opcode if no label is used.
5
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program
6
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Symbol | Register |
DL | Data register low byte |
DH | Data register high byte |
DX | Data register |
CL | Counter register low byte |
CH | Counter register high byte |
CX | Counter register |
BL | Base register low byte |
BH | Base register high byte |
BX | Base register |
AL | Accumulator register low byte |
AH | Accumulator register high byte |
AX | Accumulator register |
Symbol | Register |
ES | Extra segment register |
SS | Stack segment register |
DS | Data segment register |
CS | Code segment register |
BP | Base pointer register |
SP | Stack pointer register |
DI | Destination index register |
SI | Source index register |
Symbols used for specifying register operands
Assembly language statement syntax
7.1 Statement Syntax for a Source Program
7
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Addressing mode | Operand | Example | Segment |
Register | Destination | MOV AX, BX | - |
Immediate | Source | MOV AL, 15H | - |
Direct | Destination | MOV [15] , AX | Data |
Register indirect | Source | MOV AX, [SI] | Data |
| | MOV AX, [BP] | Stack |
| | MOV AX, [DI] | Data |
| | MOV AX, [BX] | Data |
Based | Destination | MOV [BX]+DISP, AL | Data |
| | MOV [BP]+DISP, AL | Stack |
Indexed | Source | MOV AL, [SI] | Data |
| | MOV AL, [DI] | Data |
Based indexed | Destination | MOV [BX][SI]+DISP, AH | Data |
| | MOV [BX][DI]+DISP, AH | Data |
| | MOV [BP][SI]+DISP, AH | Stack |
| | MOV [BP][DI]+DISP, AH | Stack |
Assembly language statement syntax
7.1 Statement Syntax for a Source�Program
8
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Directive statement syntax:
LABEL: DIRECTIVE OPERAND(S) ; COMMENT (S)
EXAMPLES:
DB FFH ; Allocate a byte location initialized to FFH
DB FFH, FFH, FFH, FFH, FFH ; load next 5 bytes
(DB stands for Define Bytes)
7.1 Statement Syntax for a Source�Program
9
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Constants in a statement
Constants in an instruction or directives can be expressed in any of many data types such binary, decimal, hexadecimal, octal, and character data types.
The first digit of a hexadecimal number must be one of the numbers in the range 0 through 9.
Typically, data and addresses are expressed in hexadecimal form, and the count for shift, rotate, and string instructions in commonly expressed in decimal form.
2’s complement of the number must be used for negative numbers expressed in binary, hexadecimal, or octal form.
7.1 Statement Syntax for a Source�Program
10
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Constants in a statement
7.1 Statement Syntax for a Source�Program
The repeat count in CX for a string instruction is to be equal to decimal 255. Assume that the instruction that is to load the count has the form
MOV CX, XX
where XX stands for the count, an immediate operand that is to be loaded into CX. Show how the instruction should be written.
MOV CX, 255D
or MOV CX, 255
or MOV CX, 0FFH
11
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program
The count in a move instruction that is to load CX is to be -10. Write the instruction and express the immediate operand in binary form.
The binary form of 1010 is 010102. Forming the 2’s complement by complementing each bit and adding 1, we get
10101
+1
________
10110
Therefore, the instruction is written as
MOV CX, 10110B
12
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program - Expressions
13
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program - Expressions
14
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program - Expressions
15
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program - Expressions
16
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program - Expressions
Find the value the assembler assigns to the source operand for the instruction
MOV BH, ( A * 4 – 2 ) / ( B – 3 )
for A=8 and B=5.
The solution is calculated as
(8*4-2)/(5-3) = (32 – 2)/(2)
= (30)/(2) = 15
And using hexadecimal notation, we get the instruction
MOV BH, 0FH
17
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program - Expressions
What value is used for the source operand in the expression
MOV AX, A LE (B – C)
if A=234, B=345, and C=111?
Substituting into the expression, we get
234 LE (345 – 111)
234 LE 234
since the relational operator is satisfied, the instruction is
equivalent to
MOV AX, 0FFFFH
18
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program
19
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Value-returning and attribute operators:
- SEG A, Offset A, TYPE A (B, W, Far or Near), SIZE A and Length A
- PTR (override the TYPE), DS: ES: SS, SHORT, THIS, HIGH and LOW
7.1 Statement Syntax for a Source�Program
20
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.1 Statement Syntax for a Source�Program
21
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
22
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
23
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Data directives
Segment-control directives
Modular programming directives
Directives for memory usage control
Directives for program listing control
7.2 Assembler Directives
24
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Duplicate the values in ( ) a number of times
7.2 Assembler Directives
25
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
26
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Segment-control directives
Three kinds of memory segment:
• Code segment
• Data segment
• Stack segment
The segment-control directives partitions and assigns a source program to a specific memory segment.
The beginning of a segment is identified by the segment (SEGMENT) directive and its end is
marked by the end of segment (ENDS) directive.
7.2 Assembler Directives
27
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
28
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Name of the segment
Paragraph (align-type):
- Sets the boundaries (here it is 16 byte)
- Can also be Byte, Word or Page
Combine-type attribute:
Specifies that this along with other
segments that have the same
name should be concatenated to
generate one physical Seg. Can also
be: Common (overlap), AT (specific)
Stack (part of the stack) or Memory
(Above all defined seg)
Class attribute, tells that this
is a code seg. Other values
are: Data, Stack
Tells which register
holds the base address
of the Seg
7.2 Assembler Directives
29
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
30
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
31
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
32
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
33
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
34
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Modular programming directives
Two kinds of procedures:
• Near procedure - IP in stack when called
• Far procedure - IP and CS in stack when called
NEAR is assumed as the default attribute of a
procedure.
If a procedure can be called from other modules,
its name must be made public by using the
PUBLIC directive.
If a procedure in another module is to be called
from the current module, its name must be
declared external by using the EXTRN directive.
7.2 Assembler Directives
35
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
36
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
37
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.2 Assembler Directives
38
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Examples of a source program using directives
7.2 Assembler Directives
39
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Examples of a source program using directives
7.3 Creating a Source File with an�Editor
40
CPE 0408330 @ 2008
S. Abed - HU, Jordan
C:\_
EDIT A:BLOCK.SRC [↵]
Type the program line by line
.
MOV AX, DATASEGADDR
MOV DS, AX
.
etc.
Alt-F
Menu to save or print the file
NEW
OPEN
.
etc.
Loaded file not saved. Save it now
<Yes> <No> <Cancel> <Help>
[↵]
X [↵]
7.4 Assembling and Linking Programs
41
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.4 Assembling and Linking Programs
42
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Source listing
Original
source code
Machine
Code
Program
starting
address
7.4 Assembling and Linking Programs
43
S. Abed - HU, Jordan
Source listing
Symbol
table
Errors and warnings are reported here
Segments created
7.4 Assembling and Linking Programs
44
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Link program and modular programming
7.4 Assembling and Linking Programs
45
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Benefits of modular programming:
7.4 Assembling and Linking Programs
46
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Initiating the assembly and linking processes
ML [OPTIONS] file name [[OPTIONS] file name] … [/link link-options]
ML /Fl /Fm BLOCK.ASM (↵)
Display sequences for initiating assembly of a program
7.4 Assembling and Linking Programs
47
CPE 0408330 @ 2008
S. Abed - HU, Jordan
Initiating the assembly and linking processes
LINK (↵)
Display sequences to initiate linking of object files
7.5 Loading and Executing a Run�Module
48
CPE 0408330 @ 2008
S. Abed - HU, Jordan
7.5 Loading and Executing a Run�Module
49
CPE 0408330 @ 2008
S. Abed - HU, Jordan
3, 21, 39, 42, 50
50
CPE 0408330 @ 2008
S. Abed - HU, Jordan
H.W. #7