assembly language programming with masm
TRANSCRIPT
-
8/10/2019 Assembly Language Programming With MASM
1/8
ASSEMBLY LANGUAGE
PROGRAM DEVELOPMENT
WITH MASM
611 37100 Lecture 07-2
ASSEMBLY LANGUAGE PROGRAM
DEVELOPMENT WITH MASM
7.1 Statement Syntax for a Source Program
7.2 Assembler Directives
7.3 Creating a Source File with an Editor
7.4 Assembling and Linking Programs
7.5 Loading and Executing a Run Module
611 37100 Lecture 07-3
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-4
7.1 Statement Syntax for a Source
Program
Assembly language statement syntax
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
LABEL: OPCODE OPERAND(S) ; COMMENT (S)
611 37100 Lecture 07-5
7.1 Statement Syntax for a Source
Program
Assembly language statement syntax
Data register low byteDL
Data register high byteDH
Data registerDX
Counter register low byteCL
Counter register high byteCH
Counter registerCX
Base register low byteBL
Base register high byteBH
Base registerBX
Accumulator register low byteAL
Accumulator register high byteAH
Accumulator registerAX
RegisterSymbol
Extra segment registerES
Stack segment registerSS
Data segment registerDS
Code segment registerCS
Base pointer registerBP
Stack pointer registerSP
Destination index registerDI
Source index registerSI
RegisterSymbol
Symbols used for specifying register operands
611 37100 Lecture 07-6
7.1 Statement Syntax for a Source
ProgramAssembly language statement syntax
StackMOV [BP][DI]+DISP, AH
StackMOV [BP][SI]+DISP, AH
DataMOV [BX][DI]+DISP, AH
Data
Data
Data
Stack
Data
Data
Data
Stack
Data
Data
-
-
Segment
MOV [BX][SI]+DISP, AH
MOV AL, [DI]
MOV AL, [SI]
MOV [BP]+DISP, AL
MOV [BX]+DISP, AL
MOV AX, [BX]
MOV AX, [DI]
MOV AX, [BP]
MOV AX, [SI]
MOV VARIABLE, AX
MOV AL, 15H
MOV AX, VARIABLE
Example
DestinationBased indexed
SourceIndexed
DestinationBased
SourceRegister indirect
DestinationDirect
SourceImmediate
DestinationRegister
OperandAddressing mode
-
8/10/2019 Assembly Language Programming With MASM
2/8
611 37100 Lecture 07-7
7.1 Statement Syntax for a Source
Program
Directive statement syntax
EXAMPLES:
DB 0FFH ; Allocate a byte location initialized to FFH
DB 0FFH, 0FFH, 0FFH, 0FFH, 0FFH
(DB stands for Define Bytes)
LABEL: DIRECTIVE OPERAND(S) ; COMMENT (S)
611 37100 Lecture 07-8
7.1 Statement Syntax for a Source
Program
Constants in a statementConstants in an instruction or directives can be
expressed in any of many data types such binary,decimal, hexadecimal, octal, and characterdatatypes.
The first digit of a hexadecimal number must be on ofthe numbers in the range 0 through 9.
Typically, data and addresses are expressed inhexadecimal form, and the count for shift, rotate, andstring instructions in commonly expressed in decimalform.
2s complement of the number must be used fornegative numbers expressed in binary, hexadecimal,or octal form.
611 37100 Lecture 07-9
7.1 Statement Syntax for a Source
ProgramEXAMPLE
The repeat count in CX for a string instruction is to be equal todecimal 255. Assume that the instruction that is to load the counthas the form
MOV CX, XX
where XX stands for the count, an immediate operand that is to beloaded into CX. Show how the instruction should be written.
Solution:
MOV CX, 255D
or MOV CX, 255
or MOV CX, 0FFH
611 37100 Lecture 07-10
7.1 Statement Syntax for a Source
Program
EXAMPLEThe count in a move instruction that is to load CX is to be 10.
Write the instruction and express the immediate operand in binaryform.
Solution:The binary form of 1010 is 010102. Forming the 2s complement
by complementing each bit and adding 1, we get
10101
+110110
Therefore, the instruction is written as
MOV CX, 10110B
611 37100 Lecture 07-11
7.1 Statement Syntax for a Source
Program
Operand expressions using the arithmetic,relational, and logical operators
Subtract B from A and makes the operand
equal to the difference
Adds A to B and makes the operand equal to
the sum
Shifts the value in A right by n bits position
and assigns this shifted value to the operand
Shifts the value in A left by n bits position and
assigns this shifted value to the operand
Divides A by B and assigns the remainder to
the operand
Divides A by B and makes the operand equal
to the quotient
Multiplies A with B and makes the operand
equal to the product
Function
A-B
A+B
A SHR n
A SHL n
A MOD B
A/B
A*B
Example
-
+
SHR
SHL
MOD
/
*Arithmetic
OperatorType
611 37100 Lecture 07-12
7.1 Statement Syntax for a Source
Program Operand expressions using the arithmetic,
relational, and logical operators
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
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
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
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
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
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
A GE B
A LE B
A GT B
A LT B
A NE B
A EQ B
Example
GE
LE
GT
LT
NE
EQRelational
OperatorType
-
8/10/2019 Assembly Language Programming With MASM
3/8
611 37100 Lecture 07-13
7.1 Statement Syntax for a Source
Program
Operand expressions using the arithmetic,relational, and logical operators
A is XORed with B and makes the value that
results equal to the operand
A is ORed with B and makes the value that
results equal to the operand
A is ANDed with B and makes the value that
results equal to the operand
Takes the logical NOT of A and makes the
value that results equal to the operand
Function
A XOR B
A OR B
A AND B
NOT A
Example
XOR
OR
AND
NOTLogical
OperatorType
611 37100 Lecture 07-14
7.1 Statement Syntax for a Source
Program
EXAMPLEFind 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 instructionMOV BH, 0FH
611 37100 Lecture 07-15
7.1 Statement Syntax for a Source
ProgramEXAMPLE
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 isequivalent to
MOV AX, 0FFFFH
611 37100 Lecture 07-16
7.1 Statement Syntax for a Source
Program
Value-returning and attribute operators
Returns the number of units (as specified by
TYPE) allocated for the variable A to the
operand
Returns the byte count of variable A to theoperand
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
Assigns the offset of the location A in its
corresponding segment to the operand
Assigns the contents held in the segment
register corresponding to the segment in
which A resides to the operand
Function
LENGTH A
SIZE A
TYPE A
OFFSET A
SEG A
Example
LENGTH
SIZE
TYPE
OFFSET
SEGValue-returning
OperatorType
611 37100 Lecture 07-17
7.1 Statement Syntax for a Source
Program
Value-returning and attribute operators
Returns to the operand A the low byte of
the word of A
LOW ALOW
Returns to the operand A the high byte of
the word of A
Assigns to operand A a distance or type
attribute: BYTE, WORD, NEAR, or FAR, and
the corresponding segment attribute
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
Overrides the normal segment for operand
A and assigns a new segment to A
Overrides the current type of label operand
A and assigns a new pointer type: BYTE,
WORD, NEAR, or FAR to A
Function
HIGH A
THIS BYTE A
JMP SHORT A
ES: A
NEAR PTR A
Example
HIGH
THIS
SHORT
DS:, ES:, SS:
PTRAttribute
OperatorType
611 37100 Lecture 07-18
7.2 Assembler Directives
Data directives
Segment-control directives
Modular programming directives
Directives for memory usage control
Directives for program listing control
-
8/10/2019 Assembly Language Programming With MASM
4/8
611 37100 Lecture 07-19
7.2 Assembler Directives
.TFCOND%OUT
.XLISTSUBTTL.LIST
.XCREF.SFCOND.LFCOND
.XALL.SALL.LALL
TITLEPAGE.CREFListing
MACROIRP
REPTLOCALEXITM
PURGEIRPCENDMMacro
IF2IFIDNIFB
IF1IFEIF
IFNDEFIFDIFENDIF
IFNBIFDEFELSEConditional
ENDP
STRUCLABELEND
SEGMENTINCLUDEDW OR WORD
RECORDGROUPDT or TBYTE
.RADIXEXTRNDQ or GWORD
PUBLIC
PROC
ORG
NAME
EVEN
= (EQUAL SIGN)
EQU
ENDS
DD or DWORD
DB or BYTE
COMMENT
ASSUMEData
DirectivesType
611 37100 Lecture 07-20
7.2 Assembler Directives
Data directives
Define or initialize double word size (4
byte) variable or locations
Define or initialized word size (2 byte)
variables or locations
Define or initialize byte size variables
or locations
Set or redefine the value of a symbol
Assign a permanent value to a symbol
Function
Define
double word
DD or DWORD
Define wordDW or WORD
Define byteDB or BYTE
Equal to=
EquateEQU
MeaningDirective
Commonly used data directives
611 37100 Lecture 07-21
7.2 Assembler Directives
Data directives
EXAMPLES:
AA EQU 0100H
BB EQU AA+5H
AA = 0100H
BB = 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-22
7.2 Assembler Directives
Segment-control directives
Three kinds of memory segment: Code segment
Data segment
Stack segment
The segment-control directives partitions andassigns a source program to a specific memorysegment.
The beginning of a segment is identified by thesegment (SEGMENT) directive and its end ismarked by the end of segment (ENDS) directive.
611 37100 Lecture 07-23
7.2 Assembler Directives
Segment-control directives
Specifies the segment address register for a
given segment
ASSUME
Specifies the end of a segmentENDS
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
SEGMENT
FunctionDirective
Segment directives
611 37100 Lecture 07-24
7.2 Assembler Directives
Segment-control directives
EXAMPLES:
SEGA SEGMENT PARA PUBLIC CODE
ASSUME CS:SEGA
MOV AX, BX
.
.
.
SEGA ENDS
-
8/10/2019 Assembly Language Programming With MASM
5/8
611 37100 Lecture 07-25
7.2 Assembler Directives
Segment-control directives
Segment begins on a 256 byte address boundary
in memory (8 LSB of the address are equal to 0)
PAGE
Segment begins on a word (2 byte) address
boundary in memory (LSB of the address is 0)
WORD
Segment begins anywhere in memoryBYTE
Segment begins on a 16 byte address boundary
in memory (4 LSB of the address are equal to 0)
PARA
FunctionAttribute
Align-type attributes
611 37100 Lecture 07-26
7.2 Assembler Directives
Segment-control directives
Locates the segment at an address above all
other segments
MEMORY
The segment is part of the run-time stack
segment
STACK
Locates the segment at the 16-bit paragraph
number evaluated from the expression
AT [expression]
Overlaps from the beginning segments with
the same name
COMMON
Concatenates segments with the same namePUBLIC
FunctionAttribute
Combine-type attributes
611 37100 Lecture 07-27
7.2 Assembler Directives
Segment-control directives
Specifies the extra segmentEXTRA
Specifies the stack segmentSTACK
Specifies the data segmentDATA
Specifies the code segmentCODE
FunctionAttribute
Class attributes
611 37100 Lecture 07-28
7.2 Assembler Directives
Modular programming directives
The specified symbols are defined in other
modules and are to be used in this module
EXTRN name:type[.]
The defined symbols can be referenced
from other modules
PUBLIC Symbol[..]
Defines the end of a procedureproc-name ENDP
Defines the beginning of a far-proc
procedure
proc-name PROC FAR
Defines the beginning of a near-proc
procedure
proc-name PROC [NEAR]
FunctionDirective
611 37100 Lecture 07-29
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 aprocedure.
If a procedure can be called from other modules,its name must be made public by using thePUBLIC directive.
If a procedure in another module is to be called
from the current module, its name must bedeclared external by using the EXTRN directive.
611 37100 Lecture 07-30
7.2 Assembler Directives
Modular programming directives
PUBLIC SUB
CSEG1 SEGMENT
.
.
SUB PROC FAR
MOV AX, BX
.
.
RET
SUB ENDP
.
.
.
CSEG1 ENDS
EXTRN SUB:FAR
CSEG2 SEGMENT
.
.
CALL SUB
.
.
.
.
.
CSEG2 ENDS
Module 1 Module 2
-
8/10/2019 Assembly Language Programming With MASM
6/8
611 37100 Lecture 07-31
7.2 Assembler Directives
Directives for memory usage control
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)
Specifies the end of the source programEND [expression]
Specifies the memory address starting from
which the machine code must be placed
ORG [expression]
FunctionDirective
611 37100 Lecture 07-32
7.2 Assembler Directives
Directive for program listing control
EXAMPLE:
PAGE 50 100PAGE +
Prints text on the third line of each
page of the listing
SUBTTL text
Prints text on the second line of
each page of the listing
TITLE text
Selects the number of lines printed
per page and the maximum number
of characters printed per line in the
listing
PAGE operand_1 operand_2
FunctionDirective
611 37100 Lecture 07-33
7.2 Assembler Directives
Examples of a source program using directives
611 37100 Lecture 07-34
7.2 Assembler Directives
Examples of a source program using directives
611 37100 Lecture 07-35
7.3 Creating a Source File with an
EditorC:\_
EDIT A:BLOCK.SRC []
Type the program line by line.
MOV AX, DATASEGADDRMOV DS, AX
.etc.
Menu to save or print the fileNEWOPEN
.etc.
Alt-F
[]Loaded file not saved. Save it now
611 37100 Lecture 07-36
7.4 Assembling and Linking Programs
SourceProgram
AssemblerProgram
ObjectModule
SourceListing
Assembling a source program
-
8/10/2019 Assembly Language Programming With MASM
7/8
611 37100 Lecture 07-37
7.4 Assembling and Linking Programs Source listing
Programstartingaddress
Originalsource code
MachineCode
611 37100 Lecture 07-38
7.4 Assembling and Linking Programs Source listing
Symboltable
611 37100 Lecture 07-39
7.4 Assembling and Linking Programs Source listing example of listing syntax error
Comment outthe variable N
List syntaxerror A2008
611 37100 Lecture 07-40
7.4 Assembling and Linking Programs Source listing example of listing syntax error
Spelling errorof DEC
List syntaxerror A2006
611 37100 Lecture 07-41
7.4 Assembling and Linking Programs
Link program and modular programming
Object module 1(MOD1.OBJ)
LinkProgram
Runmodule
Linkmap
Linking object modules
Object module 2(MOD2.OBJ)
Object module 3(MOD3.OBJ)
611 37100 Lecture 07-42
7.4 Assembling and Linking Programs
Benefits of modular programming
Programmers can be working on a softwareproject simultaneously.
Smaller sizes of the modules require less time toedit and assemble.
Modular programming makes it easier to reuse oldsoftware.
-
8/10/2019 Assembly Language Programming With MASM
8/8
611 37100 Lecture 07-43
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-44
7.4 Assembling and Linking Programs
Initiating the assembly and linking processesLINK ()
Display sequences to initiate linking of object files
Link map file
611 37100 Lecture 07-45
7.5 Loading and Executing a Run
Module
611 37100 Lecture 07-46
7.5 Loading and Executing a Run
Module