You are on page 1of 23

ASSEMBLY LANGUAGE

PROGRAM DEVELOPMENT
WITH MASM

ASSEMBLY LANGUAGE PROGRAM


DEVELOPMENT WITH MASM
7.1
7.2
7.3
7.4
7.5

Statement Syntax for a Source Program


Assembler Directives
Creating a Source File with an Editor
Assembling and Linking Programs
Loading and Executing a Run Module

611 37100 Lecture 07-2

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

611 37100 Lecture 07-3

7.1 Statement Syntax for a Source


Program
Assembly language statement syntax
LABEL:

OPCODE

OPERAND(S)

; COMMENT (S)

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

611 37100 Lecture 07-4

7.1 Statement Syntax for a Source


Program
Assembly language statement syntax
Symbol

Register

AX

Accumulator register

AH
AL

Symbol

Register

SI

Source index register

Accumulator register high byte

DI

Destination index register

Accumulator register low byte

SP

Stack pointer register

BX

Base register

BP

Base pointer register

BH

Base register high byte

CS

Code segment register

BL

Base register low byte

DS

Data segment register

CX

Counter register

SS

Stack segment register

CH

Counter register high byte

ES

Extra segment register

CL

Counter register low byte

DX

Data register

DH

Data register high byte

DL

Data register low byte

Symbols used for specifying register operands

611 37100 Lecture 07-5

7.1 Statement Syntax for a Source


Program
Assembly language statement syntax
Addressing mode

Operand

Example

Segment

Register

Destination

MOV AX, VARIABLE

Immediate

Source

MOV AL, 15H

Direct

Destination

MOV VARIABLE, AX

Data

Register indirect

Source

MOV AX, [SI]

Data

MOV AX, [BP]

Stack

MOV AX, [DI]

Data

MOV AX, [BX]

Data

MOV [BX]+DISP, AL

Data

MOV [BP]+DISP, AL

Stack

MOV AL, [SI]

Data

MOV AL, [DI]

Data

MOV [BX][SI]+DISP, AH

Data

MOV [BX][DI]+DISP, AH

Data

MOV [BP][SI]+DISP, AH

Stack

Based
Indexed
Based indexed

Destination
Source
Destination

MOV [BP][DI]+DISP, AH

611 37100 Lecture 07-6

Stack

7.1 Statement Syntax for a Source


Program
Directive statement syntax
LABEL:

DIRECTIVE

OPERAND(S)

; COMMENT (S)

EXAMPLES:
DB

0FFH

; Allocate a byte location initialized to FFH

DB

0FFH, 0FFH, 0FFH, 0FFH, 0FFH

(DB stands for Define Bytes)

611 37100 Lecture 07-7

7.1 Statement Syntax for a Source


Program
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 on 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.
2s complement of the number must be used for
negative numbers expressed in binary, hexadecimal,
or octal form.
611 37100 Lecture 07-8

7.1 Statement Syntax for a Source


Program
EXAMPLE
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.

Solution:
or
or

MOV CX, 255D


MOV CX, 255
MOV CX, 0FFH

611 37100 Lecture 07-9

7.1 Statement Syntax for a Source


Program
EXAMPLE
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.

Solution:
The binary form of 1010 is 010102. Forming the 2s complement
by complementing each bit and adding 1, we get
10101
+1
10110
Therefore, the instruction is written as
MOV CX, 10110B

611 37100 Lecture 07-10

7.1 Statement Syntax for a Source


Program
Operand expressions using the arithmetic,

relational, and logical operators


Type

Operator

Arithmetic

A*B

Example

Multiplies A with B and makes the operand


equal to the product

Function

A/B

Divides A by B and makes the operand equal


to the quotient

MOD

A MOD B

Divides A by B and assigns the remainder to


the operand

SHL

A SHL n

Shifts the value in A left by n bits position and


assigns this shifted value to the operand

SHR

A SHR n

Shifts the value in A right by n bits position


and assigns this shifted value to the operand

A+B

Adds A to B and makes the operand equal to


the sum

A-B

Subtract B from A and makes the operand


equal to the difference

611 37100 Lecture 07-11

7.1 Statement Syntax for a Source


Program
Operand expressions using the arithmetic,

relational, and logical operators


Type

Operator

Relational

EQ

A EQ B

Example

Compares value of A to that of B. If A equals B,


the operand is set to FFFFH and if they are not
equal it is set to 0H

Function

NE

A NE B

Compares value of A to that of B. If A is not


equal to B, the operand is set to FFFFH and if
they are equal it is set to 0H

LT

A LT B

Compares value of A to that of B. If A is less


than B, the operand is set to FFFFH and if it is
equal or greater than it is set to 0H

GT

A GT B

Compares value of A to that of B. If A is


greater than B, the operand is set to FFFFH
and if it is equal or less than it is set to 0H

LE

A LE B

Compares value of A to that of B. If A is less


than or equal to B, the operand is set to
FFFFH and if it is greater than it is set to 0H

GE

A GE B

Compares value of A to that of B. If A is


greater than or equal to B, the operand is set
to FFFFH and if it is less than it is set to 0H

611 37100 Lecture 07-12

7.1 Statement Syntax for a Source


Program
Operand expressions using the arithmetic,

relational, and logical operators


Type

Logical

Operator

Example

Function

NOT

NOT A

Takes the logical NOT of A and makes the


value that results equal to the operand

AND

A AND B

A is ANDed with B and makes the value that


results equal to the operand

A OR B

A is ORed with B and makes the value that


results equal to the operand

A XOR B

A is XORed with B and makes the value that


results equal to the operand

OR
XOR

611 37100 Lecture 07-13

7.1 Statement Syntax for a Source


Program
EXAMPLE
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

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

611 37100 Lecture 07-14

7.1 Statement Syntax for a Source


Program
EXAMPLE
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?

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

611 37100 Lecture 07-15

7.1 Statement Syntax for a Source


Program
Value-returning and attribute operators
Type
Value-returning

Operator

Example

Function

SEG

SEG A

Assigns the contents held in the segment


register corresponding to the segment in
which A resides to the operand

OFFSET

OFFSET A

Assigns the offset of the location A in its


corresponding segment to the operand

TYPE

TYPE A

Returns to the operand a number


representing the type of A; 1 for a byte
variable and 2 for a word variable; NEAR or
FAR for the label

SIZE

SIZE A

Returns the byte count of variable A to the


operand

LENGTH

LENGTH A

Returns the number of units (as specified by


TYPE) allocated for the variable A to the
operand

611 37100 Lecture 07-16

7.1 Statement Syntax for a Source


Program
Value-returning and attribute operators
Type

Attribute

Operator

Example

Function

PTR

NEAR PTR A

Overrides the current type of label operand


A and assigns a new pointer type: BYTE,
WORD, NEAR, or FAR to A

DS:, ES:, SS:

ES: A

Overrides the normal segment for operand


A and assigns a new segment to A

SHORT

JMP SHORT A

Assigns to operand A an attribute that


indicates that it is within +127 or 128
bytes of the next instruction. This lets the
instruction be encoded with the minimum
number of bytes

THIS

THIS BYTE A

Assigns to operand A a distance or type


attribute: BYTE, WORD, NEAR, or FAR, and
the corresponding segment attribute

HIGH

HIGH A

Returns to the operand A the high byte of


the word of A

LOW

LOW A

Returns to the operand A the low byte of


the word of A

611 37100 Lecture 07-17

7.2 Assembler Directives


Data directives
Segment-control directives
Modular programming directives
Directives for memory usage control
Directives for program listing control

611 37100 Lecture 07-18

7.2 Assembler Directives


Type
Data

Directives
ASSUME

ENDS

NAME

COMMENT

EQU

ORG

DB or BYTE

= (EQUAL SIGN)

PROC

DD or DWORD

EVEN

PUBLIC

DQ or GWORD

EXTRN

.RADIX

DT or TBYTE

GROUP

RECORD

DW OR WORD

INCLUDE

SEGMENT

END

LABEL

STRUC

ELSE

IFDEF

IFNB

ENDIF

IFDIF

IFNDEF

IF

IFE

IF1

IFB

IFIDN

IF2

ENDM

IRPC

PURGE

EXITM

LOCAL

REPT

IRP

MACRO

.CREF

PAGE

TITLE

.LALL

.SALL

.XALL

.LFCOND

.SFCOND

.XCREF

.LIST

SUBTTL

.XLIST

%OUT

.TFCOND

ENDP
Conditional

Macro

Listing

611 37100 Lecture 07-19

7.2 Assembler Directives


Data directives
Directive

Meaning

Function

EQU

Equate

Assign a permanent value to a symbol

Equal to

Set or redefine the value of a symbol

DB or BYTE

Define byte

Define or initialize byte size variables


or locations

DW or WORD

Define word

Define or initialized word size (2 byte)


variables or locations

DD or DWORD

Define
double word

Define or initialize double word size (4


byte) variable or locations

Commonly used data directives

611 37100 Lecture 07-20

10

7.2 Assembler Directives


Data directives
EXAMPLES:
AA
BB
AA
BB

EQU 0100H
EQU AA+5H
= 0100H
= AA+5H

CC DB 7
EE DB ?
MESSAGE DB JASBIR
TABLE_A DB 10 DUP(?), 5 DUP(7)
TABLE_B DB 0,1,2,3,4,5,6,7,8,9

611 37100 Lecture 07-21

7.2 Assembler Directives


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.

611 37100 Lecture 07-22

11

7.2 Assembler Directives


Segment-control directives
Directive

Function

SEGMENT

Defines the beginning of a segment and specifies


its kind, at what type of address boundary it is to
be stored in memory, and how it is to be
positioned with respect to other similar segments
in memory

ENDS

Specifies the end of a segment

ASSUME

Specifies the segment address register for a


given segment

Segment directives

611 37100 Lecture 07-23

7.2 Assembler Directives


Segment-control directives
EXAMPLES:
SEGA SEGMENT
ASSUME
MOV
.
.
.
SEGA ENDS

611 37100 Lecture 07-24

PARA PUBLIC CODE


CS:SEGA
AX, BX

12

7.2 Assembler Directives


Segment-control directives
Attribute

Function

PARA

Segment begins on a 16 byte address boundary


in memory (4 LSB of the address are equal to 0)

BYTE

Segment begins anywhere in memory

WORD

Segment begins on a word (2 byte) address


boundary in memory (LSB of the address is 0)

PAGE

Segment begins on a 256 byte address boundary


in memory (8 LSB of the address are equal to 0)

Align-type attributes

611 37100 Lecture 07-25

7.2 Assembler Directives


Segment-control directives
Attribute

Function

PUBLIC

Concatenates segments with the same name

COMMON

Overlaps from the beginning segments with


the same name

AT [expression]

Locates the segment at the 16-bit paragraph


number evaluated from the expression

STACK

The segment is part of the run-time stack


segment

MEMORY

Locates the segment at an address above all


other segments

Combine-type attributes

611 37100 Lecture 07-26

13

7.2 Assembler Directives


Segment-control directives
Attribute

Function

CODE

Specifies the code segment

DATA

Specifies the data segment

STACK

Specifies the stack segment

EXTRA

Specifies the extra segment

Class attributes

611 37100 Lecture 07-27

7.2 Assembler Directives


Modular programming directives
Directive

Function

proc-name PROC [NEAR] Defines the beginning of a near-proc


procedure
proc-name PROC FAR

Defines the beginning of a far-proc


procedure

proc-name ENDP

Defines the end of a procedure

PUBLIC Symbol[..]

The defined symbols can be referenced


from other modules

EXTRN name:type[.] The specified symbols are defined in other


modules and are to be used in this module

611 37100 Lecture 07-28

14

7.2 Assembler Directives


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.

611 37100 Lecture 07-29

7.2 Assembler Directives


Modular programming directives
CSEG1

SUB

SUB

CSEG1

PUBLIC
SEGMENT
.
.
PROC
MOV
.
.
RET
ENDP
.
.
.
ENDS

SUB
CSEG2

FAR
AX, BX

Module 1

611 37100 Lecture 07-30

CSEG2

EXTRN
SEGMENT
.
.
CALL
.
.
.
.
.
ENDS

SUB:FAR

SUB

Module 2

15

7.2 Assembler Directives


Directives for memory usage control
Directive

Function

ORG [expression]

Specifies the memory address starting from


which the machine code must be placed

END [expression]

Specifies the end of the source program

EXAMPLE:
ORG 100H
ORG 100H
ORG $+200H
(memory locations 10016 to 30016 are skipped and the machine
code of the program can start at address 30116)

611 37100 Lecture 07-31

7.2 Assembler Directives


Directive for program listing control
Directive

Function

PAGE operand_1 operand_2

Selects the number of lines printed


per page and the maximum number
of characters printed per line in the
listing

TITLE text

Prints text on the second line of


each page of the listing

SUBTTL text

Prints text on the third line of each


page of the listing

EXAMPLE:
PAGE 50 100
PAGE +
611 37100 Lecture 07-32

16

7.2 Assembler Directives


Examples of a source program using directives

611 37100 Lecture 07-33

7.2 Assembler Directives


Examples of a source program using directives

611 37100 Lecture 07-34

17

7.3 Creating a Source File with an


Editor
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>

[]

611 37100 Lecture 07-35

7.4 Assembling and Linking Programs

Object
Module
Source
Program

Assembler
Program
Source
Listing

Assembling a source program

611 37100 Lecture 07-36

18

7.4 Assembling and Linking Programs


Source listing

Program
starting
address
Original
source code

Machine
Code

611 37100 Lecture 07-37

7.4 Assembling and Linking Programs


Source listing

Symbol
table

611 37100 Lecture 07-38

19

7.4 Assembling and Linking Programs


Source listing example of listing syntax error

Comment out
the variable N

List syntax
error A2008

611 37100 Lecture 07-39

7.4 Assembling and Linking Programs


Source listing example of listing syntax error

Spelling error
of DEC

List syntax
error A2006

611 37100 Lecture 07-40

20

7.4 Assembling and Linking Programs


Link program and modular programming
Object module 1
(MOD1.OBJ)

Object module 2
(MOD2.OBJ)

Run
module
Link
Program
Link
map

Object module 3
(MOD3.OBJ)

Linking object modules

611 37100 Lecture 07-41

7.4 Assembling and Linking Programs


Benefits of modular programming
Programmers can be working on a software
project simultaneously.
Smaller sizes of the modules require less time to
edit and assemble.
Modular programming makes it easier to reuse old
software.

611 37100 Lecture 07-42

21

7.4 Assembling and Linking Programs


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

611 37100 Lecture 07-43

7.4 Assembling and Linking Programs


Initiating the assembly and linking processes
LINK ()

Display sequences to initiate linking of object files

Link map file


611 37100 Lecture 07-44

22

7.5 Loading and Executing a Run


Module

611 37100 Lecture 07-45

7.5 Loading and Executing a Run


Module

611 37100 Lecture 07-46

23

You might also like