100% found this document useful (2 votes)
455 views

Chapter 4 Assembly Language and Programming

This document provides an overview of assembly language programming concepts including: - Assembly language instructions consist of labels, opcodes, operands, and comments. Directives do not execute but provide programming actions for the assembler. - Programs are made up of segments for code, data, stack. Segment directives define segments and assume directives assign registers. - Modular programming uses procedures, extern to share code between modules. Memory usage is controlled with org. - Data types, variables, constants and memory blocks are defined. Operators and directives relate symbols to addresses or values.

Uploaded by

Petra Kalasa
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
455 views

Chapter 4 Assembly Language and Programming

This document provides an overview of assembly language programming concepts including: - Assembly language instructions consist of labels, opcodes, operands, and comments. Directives do not execute but provide programming actions for the assembler. - Programs are made up of segments for code, data, stack. Segment directives define segments and assume directives assign registers. - Modular programming uses procedures, extern to share code between modules. Memory usage is controlled with org. - Data types, variables, constants and memory blocks are defined. Operators and directives relate symbols to addresses or values.

Uploaded by

Petra Kalasa
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 85

Chapter 4

Assembly language and


programming
Objectiv
es
• Assembly Language Format
• Instruction and Directive
• Programming
• Samples & examples
Key Terms

Assembler 汇编程序
Pseudo-instructions 伪指令    pseudo['su:dəʊ]
Directive 指示 ( 伪指令)
Instructions 指令(硬指令)
Debug [,di:‘bʌɡ] 调试
quadword[‘kwɔdwə:d] 四倍字长
Data Types 数据类型
Data Definition 数据定义
Case Sensitive 区分大小写
4.1 Introduction
Assembly language
 An assembly language program is actually
a sequence of instructions.
 Assembly language is a low level
language, which means it is machine
dependent.
• High level language such as C/C++ and
Java is machine independent.
04/21/20 4
Assembly language
• Direct access to computer hardware.

– Understanding much computer’s architecture

– Optimizing certain sections of application


programs for speed and for accessing
computer hardware
Assembly Language Format
 An assembly language instruction
typically has 4 fields:
label, opcode(mnemonic), operand and comment of an instruction.

[label] mnemonic [operand(s)] [;comment]


START: MOV AX, 1 ; set AX to 1

Brackets indicate the field is optional.

• Addressing modes tells how we can


determine the exact location of the data
(operand) we want to manipulate.
04/21/20 7
Label
• A place marker for instructions and data.
• Code labels
– End with a colon(:)
– Imply the instruction’s address
– Usually used as targets of jumping and
looping
• Data labels
– Cannot end with a colon(:)
– Imply the data’s address
Instruction mnemonic
• Hints about the type of operation
performed

• Case insensitive
Operands
Operand type Example
Immediate value 96
Constant 2+4
expression
Register AX
Memory(variable) Variable name
• 0~3 operands
– 0: STC, CLC…
– 1: INC AX
– 2: MOV AX, BX
Comments
• Optional
• Start with semicolon (;)
• Explaining how the program works
Directives
• Embedded in the source code recognized
and acted upon by the assembler.
• Do not execute at run time.
• Do special programming actions during
the assembly process.
• Define variables, macros and procedures
Format
• [label] directive mnemonic [operand(s)]
[;comment]
Symbolic definition
• Associating a symbol with an integer
expression or some text.
• Equal-sign directive
– name=expression
– Example:
• Count=5
– Could be used for redefinition
• Count=5
• MOV AL, count
• Count =10
• ……
EQU directive
• Associates a symbolic name with an
expression or text.
– Name EQU expression
– Name EQU symbol
– Name EQU <text>
• Examples:
– Count EQU 100; count replaces 100
– SUM EQU 30+25; SUM replaces 30+25
– C EQU CX; C replaces register CX
• Can not be used for redefinition
Variable definition
• Define the size of variables, allocate
space for them, and assign them initial
values.
– Name DB expression ; define byte
– Name DW expression ; define word
– Name DD expression ; define double
word
– Name DQ expression ; define 8 bytes
– Name DT expression ;define 10 bytes
Examples
B1 DB 10H, 30H ; stores byte-size data10H, 30H
B2 DB 2x3+5 ; stores byte-size data 0BH
S1 DB ‘GOOD!’ ; byte-size ASCII code of ‘GOOD!’
W1 DW 1000H, 2030H ; word-size two data 1000H, 2030H
W2 DD 12345678H ; double-word size data 12345678H
S2 DB ‘AB’ ; byte-size ASCII code of ‘AB’
S3 DW ‘AB’ 41H,42H
; word-size ASCII code of ‘AB’
41H,42H

Differences between S2 and S3?


Data Types and Data Definition
;Data type and definition This is how data is initialized
in the data segment
DATA1 DB 25D
DATA2 DB 10001001b 0000 19
DATA3 DB 12h 0001 89
0002 12
ORG 0010h
DATA4 DB “2591”
0010 02 05 09 01
ORG 0018h
DATA5 DB ? 0018 00 (set aside a byte)

ORG is used to indicate the beginning of the offset address.


Block of storage locations
 Format:
 N DUP(initialized value) ; duplicate the initialized value n times.
 Allows a sequence of storage locations to be defined or
reserved
 Only used as an operand of a define directive
 Example:

TABLE_A DB 10 DUP(?) ;Allocate 10 bytes of


memory without value
TABLE_B DB 5 DUP (7) ;Allocate 5 bytes of
memory with 7
TABLE_C DB 15 DUP (?), DUP ;Allocate 10 bytes of
(7) memory without value
and 5 bytes with 7
Program Segment Structure
• Data Segments • Stack Segment
– Storage for – used to set aside
variables storage for the
– Variable stack
addresses are
– Stack addresses
computed as
offsets from start are computed as
of this segment offsets into this
• Code Segment segment
– contains
executable
instructions
Segment-control Directives
• Specify the beginning and end of a
segment
• Assign them attributes
– A start address
– Segment’s class: CS, DS,SS, ES
• Format:
– Name SEGMENT [Align-types] [Combine-
type][Class]
– Name ENDS
Align-type attributes
Attribute Function
PARA Segment begins on a 16 byte address
boundary in the memory(4 LSBs of the
address are equal to 0)
BYTE Segment begins anywhere in memory
WORD Segment begins on a word (2 bytes)
address boundary in memory (LSB of
the address is 0)
PAGE Segment begins on a 256 byte address
boundary in memory (8 LSBs of the
address are equal to 0)
Combine-type attributes
Attribute Function
(None) Every segment has its own segment
base address. (By default)
PUBLIC Concatenates segments with the
same name
COMMON Load the segments with the same
name from same address
STACK Same with PUBLIC, but limited to
stack segment
AT (expression) Locates the segment from the
address calculated from expression
Class attributes
‘.data’: data segment
‘.code’: code segment
‘.stack’: stack segment
ASSUME
Format:
ASSUME name of segment register: name of segment
Meaning:
Assign the segment registers to hold the address of
segment

Example: ;the register CS holds the


ASSUME CS:Cde, SS:Stk, DS:Dta base address for segment
Cde, SS holds the base
address for segment Stk, DS
holds the base address for
segment Dta
Full Segment Definitions
Stk segment para stack ‘.stack'
db 256 dup (?)
Stk ends

Dta segment word public


db 'Example data segment'
Dta ends

Cde segment para ‘.code'


(continued)
Full Segment Definitions
(continued)
Cde segment para ‘.code'
assume cs:Cde, ds:Dta, ss:Stk
a proc
mov ax,dta ;init segment reg
mov ds,ax
;code followed by exit to DOS
a endp
Cde ends
end a
Modular Programming Directives
• Modules: Large programs are broken into
small segments

• Procedure: A section of program can be


called for execution from other modules
– Near procedure: Called from the same code
segment, only the content of IP is changed.
– Far procedure: Called from any code segment,
both the content of CS and IP are changed.
Modular Programming Directives
Directive Function
Proc-name PROC [NEAR] Defines the beginning of a near
procedure (By default)
Proc-name PROC FAR Defines the beginning of a far
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
Examples
Near procedure Far procedure
PUBLIC SUB NEAR PUBLIC SUB FAR
SUB NEAR PROC SUB FAR PROC FAR
…… ……
…… ……
RET RET
SUB NEAR ENDP SUB FAR ENDP
Example
Module 1 Module 2
PUBLIC SUB
CSEG1 SEGMENT EXTERN SUB:FAR
…… CSEG2 SEGMENT
SUB PROC FAR ……
MOV AX,BX CALL SUB
…… ……
RET
CSEG2 ENDS
SUB ENDP
…… SUB is in another
CSEG1 ENDS module, so it must be
declared by EXTERN in
this module before use.
Memory usage control directives
• Format : ORG address
• Meaning: Specify the starting
address of a specific part of memory.

• Format: ORG $+address


• Meaning: The starting address is
current offset address+address
The end-of-program directive
• Format: END
• Meaning: The end of the assembly
program. Tell the assemble to stop
assembling.
4.5 Other Data-related operators and
directives
• Operators
– Directives
– Not executable
– Results got when assemble or link.
• OFFSET, PTR, TYPE, LENGTH, SIZE,
LABEL
Offset Operator
• OFFSET operator returns the offset of a
data label.
– A variable Mybyte in the data segment
under offset operator
Example
bVal BYTE ?
wVal WORD ?
dVal DD ?
dVal2 DW ?
Assuming bVal located at offset 4000H, what’s
the content of SI after each instruction
executed?
MOV SI, OFFSET bVal ; SI=4000H
MOV SI, OFFSET wVal ; SI=4001H
MOV SI, OFFSET dVal
; SI=4003H
MOV SI, OFFSET dVal2
; SI=4007H
PTR operator
• Override the declared size of an operand
• Example
– myDouble DD 12345678H
– MOV AX, myDouble ; ERROR
MOV AX, WORD PTR my double ; lower-byte into AX
MOV AX, WORD PTR [myDouble+2] ; 1234H into
AX
MOV BL, BYTE PTR myDouble ; 78H into BL
Type Operator
• Returns the size of
a variable. (In byte)
• Example:
– var1 BYTE ? Expression Value
– var2 WORD ? TYPE var1 1
TYPE var2 2
– var3 DD ?
TYPE var3 4
– var4 DQ ? TYPE var4 8
LENGTH Operator

• Counts the number of


elements in the array
• Example
– byte1 BYTE 10,20,30 EXPRESSION VALUE
– array1 WORD 30 LENGTH byte1 3
DUP(?), 0,0
LENGTH array1 30+2
– array2 WORD 5
DUP(3DUP(?)) LENGTH array2 5x3
– array3 DD 1, 2,3, 4 LENGTH array3 4
– digStr BYTE ‘12345678’, LENGTH digStr 9
0
Size Operator
• Returns the size of the variables,
equivalent to multiplying LENGTH by
TYPE.
• Example:
– intArray WORD 32 DUP(0)
– MOV AX, SIZE intArray; AX=64
LABEL operator

• Give the new attribute to the variable.


• Example:
– val16 LABEL WORD
– val32 DW 12345678H
– MOV AX, val16 ; AX=5678H
– MOV DX,[val16+2] ; DX=1234H
Explanations: val16 is an alias for the same storage
location named val32.
Review
• Assembly language format:
– (LABEL:) INSTRUCTION (;comments)
• Directives
– Symbolic
– Variables
– Segment
– Modular programming
– Memory usage
• Operators
– OFFSET, PTR,TYPE, LENGTH, SIZEOF, LABEL
4.6 Programming

04/21/20
4.6.1 Fundamental Programming
Constructs
• Four basic programming construct
– Sequential construct
– Branch construct
– Cycle construct
– Subroutine call construct

• Easy to read with basic programming


construct
1. Sequential construct
• Operation
performed one after Start

the other
• No branches Operation 1

• No jumps
• No subroutine call
Operation 2

End
Example
Calculate the average value of two data x and y. The result should be
put into z. MOV BL,2
DATA SEGMENT DIV BL ; Q(AX/BL)->AL
x db 95D MOV z, AL
y db 87D RET
z db ? MAIN ENDP
DATA ENDS CODE ENDS
CODE SEGMENT END
MAIN PROC FAR
ASSUME CS:CODE, DS:DATA
START: MOV AL, x
ADD AL, y
MOV AH,0
ADC AH,0 ; put the carry
information
into AH
2. Branch construct
IF-THEN-ELSE SWITCH
Example
• Make a program to
do the function as Given the x, decide if
indicated: it’s negative or
positive or zero, then
give the value to y
1 x0 respectively.

y0 x0
 1 x  0

DATA SEGMENT
x DB ?
y DB ?
DATA ENDS
CODE SEGMENT
MAIN PROC FAR
ASSUME CS:CODE, DS:DATA
START: MOV AL, x
CMP AL, 0 ; move data x into AL
JGE POS ; compare AL with 0
MOV y, -1D
RET
; if x>=0, jump to POS
POS: JE ZEO ;otherwise, assign -1 to y
MOV y, 1 ; if x=0, jump to ZEO
RET
ZEO: MOV y,0 ; otherwise, assign 1 to y
RET
MAIN ENDP
CODE ENDS
; assign 0 to y
END START
3. Cycle construct
First execute last judge First judge last execute
Example
• Question: Calculate the
number of negative
number in an array
• Assuming the first
element of array is the
number of the data, the
second element will be
used to store the result.
The data starts from the
third element of the array.
• Solution: Using one
register as counter of
negative numbers; one
register as counter of
data; DI as pointer to the
data in array.
DATA SEGMENT INC BL ;otherwise, inc BL
ARRAY DB 200 DUP(?) POSI: INC DI ; positive, change the
DATA ENDS pointer
CODE SEGMENT LOOP AGAIN;
ASSUME CS:CODE, DS:DATA MOV [SI+1], BL; move the
MAIN PROC FAR number to the second element of
START: LEA DI, ARRAY; DI point to ARRAY array
MOV SI, DI; SI point to ARRAY RET
MOV CL, [DI]; number of data into MAIN ENDP
CL CODE ENDS
XOR CH, CH; let CH=0 END START
MOV BL, 0; use BL as counter and
set 0
INC DI; change the pointer
INC DI; change the pointer
AGAIN: TEST BYTE PTR[DI], 80H; test the
SF
JZ POS ; if zero, jump to POS
4. Subroutine call construct
• Subroutine
– A dependent
procedure used for
lots of times.
• Call the subroutine
until the task has
been done and then
return to the
previous location of
the programming.
Example
Write a program to calculate the average value of the data
in the ARRAY. PADD PROC NEAR
PUSH AX
DATA SEGMENT PUSH CX
PUSH DX
ARRAY DW 30 DUP (?) PUSH SI ; push the data into stack to protect break
COUNT DW 30 point
LEA SI, ARRAY ; get the effect address of ARRAY
SUM DW 2 DUP (?) MOV CX, COUNT ; use CX as counter
DATA ENDS MOV DX, 0
AGAIN: ADD AX, [SI]; (AX)=(AX)+([SI])
CODE SEGMENT JNC NEXT
INC DX ; use DX to store carry information
MAIN PROC FAR NEXT: INC SI
ASSUME CS:CODE, DS:DATA INC SI ; go to next data
LOOP AGAIN
START: CALL PADD MOV SUM+2, DX
RET POP SI
POP DX
MAIN ENDP POP CX
POP AX
RET
PADD ENDP
CODE ENDS
END
4.6.3 Debug program
Bugs: errors in the program
Debug: the process of removing them
Two types:
Syntax error: No following the rules of coding
Execution error: Error in the logic behind the
development of the program.
“Debug ” is an environment for one to
learn and debug assembly language
programs.
Content of internal register
State of CPU
4.6.4 Interrupt Calls
• Two types
– Hardware interrupt
– Software interrupt
• Interrupt vectors table
– Pointed by interrupts
– Address of interrupt entry (CS:IP)
Interrupt instructions
• Three instructions for programmer:
– INT
• Software interrupt instruction
– INTO
• Overflow interrupt
– INT 3
• Special software interrupt as interrupt
instruction is 1 byte
INT N
• 0<=N<=256, N*4=vector table address
• Steps:
– Pushes the flags onto the stack
– Clear trace flag and interrupt-enable flag
– Push the current CS register onto the stack
– Fetches the new CS location from the vector
table
– Push the current IP onto the stack
– Fetches the new IP location from the vector table
– Jump to this new location
INTO
• Encountered when overflow happens
(OF=1)
• Vector table address is 0010H
• An INTO appears after every addition and
subtraction
– OF=0, no operation executed.
– OF=1, go interrupt.
INT 3
• Breakpoint interrupt.
• Interrupt instruction is 1 byte.
• Often used for debug.
– Display the contents of all register
IRET
• Interrupt return instruction
– Pop stack data back into IP
– Pop stack data back into CS
– Pop stack data back into flags.
• Resume the program interrupted
before.
Dos Function Calls
• Control the IBM-PC and its compatible
system
– Reading and writing data to the disk and
managing the keyboard and displays.
• Format
– MOV AH, call number (Table 4.10)
– INT 21H or INT 10H
INT 21H DOS function calls
INT 10H BIOS function calls
4.7 SAMPLES AND EXAMPLES
4.7.1 Computational Routines

1. Binary addition
– Ex: Calculate the sum of two string data
DATA SEGMENT
M1 DB 20 DUP (?)
M2 DB 20 DUP(?)
M3 DW 20 DUP(0)
DATA ENDS
CODE SEGMENTS
ASSUME CS:CODE, DS:DATA
LEA SI, M1 ; get the effect address of M1,M2 and M3
LEA DI, M2
LEA BX, M3
MOV CX, 20 ; use CX as counter
AA1:MOV AL, [SI] ; move data of array 1 into AL
; add the data of array 2 with array 1
ADD AL, [DI]
; move the result into array 3
MOV [BX], AL
; save the carry information in the higher-byte of
ADC BYTE PTR[BX+1],0 array 3
INC SI ; point to next data of array 1
INC DI ; point to next data of array 2
ADD BX, 2 ; point the next data location of array 3
LOOP AA1 ; do the addition again
MOV AX, 4C00H ; move the function number of interrupt into AH
INT 21H ; finish
CODE ENDS
END START
2. Binary subtraction
WAVE1 DW 2048 DUP(?)
WAVE2 DW 2048 DUP(?)
……
SUBWAVE PRCO FAR
LEA SI, WAVE1 ; init pointer to beginning of WAVE1
LEA DI, WAVE2 ; init pointer for WAVE2
MOV CX, 2048 ; init loop counter
SUBEM: MOV AX, [SI] ;get data from WAVE1
SUB [DI], AX ; subtract and replace WAVE2 data
ADD SI, 2 ; advance WAVE1 pointer
ADD DI,2 ; advance WAVE2 pointer
LOOP SUBEM ; do all the subtraction
RET
SUBWAVE ENDP
3. BCD addition
• BCD binary
– 1 digit using 4 bits
– 1 byte storage: 0~99
– 2 byte storage: 0~9999
• Eliminate the round-off error
– 0.7D=0.101100110011…
– 0.10110011…=0.69999…
• BCD adjust:
– 34298: 00 03 43 98H
– 7571364: 07 57 13 64H
DAA: Decimal adjust after addition
• Adjust result into packed BCD code after
BCD addition
– If lower 4 bit of AL >=9 or AF=1, AL=AL+06H
and set AF=1
– If high 4 bit of AL>=9 or CF=1, AL=AL+60H
and set CF=1.
– If neither, Clear CF and AF.
• Two BCD number addition, NUMA and NUMB
NUMA DB 4 DP (?)
NUMB DB 4 DP(?)
ADDBCD PRCO FAR
MOV SI,3 ; init pointer to LSB
MOV CX,4 ; init loop counter
CLC ; clear carry to start
DECIADD: MOV AL, NUMA [SI] ; get first BCD number
ADC AL, NUMB[SI] ; add second BCD number
DAA ; correct result into BCD
MOV NUMB [SI], AL ; save result
DEC SI ; point to next pair to digits
LOOP DECIADD ; do all the addition
RET
ADDBCD ENDP
4. BCD subtraction
NUMA DB 4 DUP(?)
NUMB DB 4 DUP(?)

AMINUSB PROC FAR


XCHG SI, DI ; swap pointers
BMINUSA: MOV CX, 4 ; init loop counter
ADD SI, 3 ; adjust pointer to end of BCD numbers
ADD DI, 3 ; clear carry flag to start
CLC
DECISUB: MOV AL, [DI] ; get first number
SBB AL, [SI] ; subtract second number with borrow
DAS ; adjust result into BCD
MOV [DI], AL
; save result
DEC SI
DEC DI ; point to next pair to digits
LOOP DECISUB ; do all the addition
RET
AMINUSB ENDP
4.7.2 Number conversion
MUL BL ; (AL)*(BL)->(AX)
MOV CX,AX ; save the result
Convert 3-digit ASCII MOV AL, TEN
172 into unsigned 8- SUB AL,30H

bit number MOV BL, 10


MUL BL
Code: ADD CX,AX
BINVAL DB ? MOV AL, ONE
HUN DB 31H; ASCII code for 1 SUB AL, 30H
TEN DB 37H; ASCII code for 7 ADD CL,AL
ONE DB 32H; ASCII code for 2 MOV BINVAL, CL
DTOB PROC FAR RET
MOV AL,HUN ; get hundreds DTOB ENDP
digit
SUB AL,30H ; convert to binary
MOV BL,100
2. Binary to decimal
Ex: Convert 81H into ADD AL, 30H ; convert into ASCII code
MOV HUN, AL ; save hundred digit
decimal number in ASCII XCHG AL, AH ;get reminder
code SUB AH, AH; prepare for division
MOV BL, 10
Program DIV BL ; get ten digit
BINVAL DB 81H ADD AL, 30H ; convert into ASCII code
HUN DB ? MOV TEN, AL ; save ten digit
TEN DB ? ADD AH, 30H ; convert to ASCII code
MOV ONE, AH ; save one digit
ONE DB ?
RET
…… BTOD ENDP
BTOD PROC FAR
MOV AL, BINVAL
SUB AH, AH; prepare for
division
MOV BL,100;
DIV BL ; Q(AX)/(BL)->(AL),
R(AX)/(BL)->(AH)
4.7.3 String operation
 1. String display
 Assuming DX has been loaded with the starting addressing of string.
 Program:

SENDOUT PROC FAR


MOV SI, DX ; use SI as pointer
GETCHAR: MOV DL, [SI] ; read a character
CMP DL, ‘$’ ; end of string?
JZ EXIT ; jump if finish
MOV AH, 2 ; display character function
INT 21H ; DOS call
INC SI ; point to next string character
JMP GETCHAR ; and repeat
EXIT RET
SENTOUT ENDP
2. Compare strings
Comparing two strings, returning the CF=1 if the
strings are the same.
STRINGA DB ‘alphabetic’
STRINGB DB ‘alphabet’
……
CHKSTRING PROC FAR
MOV SI, 0 ;init character pointer
MOV CX, 10 ; init loop counter
CHECKCHAR: MOV AL, STRINGA[SI] ; get character from STRINGA
CMP AL, STRINGB[SI]; compare with STRINGB character
JNZ NOMATCH ; even one difference causes failure
INC SI ; point to next character
LOOP CHECHCHAR
STC
RET
NOMATCH CLC
RET
CHKSTRING ENDP
4.7.4 Items sorting
Bubble sort
Comparisons and swaps of numbers
Repeatedly compare one element with the next element.
If the second element is smaller than the first, the two numbers
will be swapped.
Steps:
Start the comparison from the first element and the second one
Repeatedly with every two elements, from first two to last two
Repeatedly with every two elements, except last one
Repeatedly with every two elements, except last two
Repeatedly with every two elements, until no elements to be
compared
Examples: 7 10 6 3 9
Program
VALUES DB 7,10,6,3,9
NVALS DW 5
…………
SORT PROC FAR
MOV DX, NVALS ; use DX as counter
DEC DX ;
DOPASS: MOV CX,DX
MOV SI, 0
CHECK: MOV AL, VALUES[SI]
CMP VALUES[SI+1], AL ; compare two adjacent elements, (VALUS[SI+1]-(AL))
JNC NOSWAP ; no carry, means VALUES[SI+1]>=(AL), no need swap
MOV BL, VALUES[SI+1] ; swap two adjacent elements
MOV VALUES[SI+1], AL
MOV VALUES[SI], BL
NOSWAP: INC SI ; no swap, move to next two elements
LOOP CHECK ; compare again
DEC DX
JNZ DOPASS ; compare from the first two elements again unless DX is zero
RET
SORT ENDP
Macros
• A set of instructions
• Used as instruction once defined
• Format:
MarcoName MARCO (parameters)
(instructions)
ENDM
Example
DISP_MSG MACRO
MOV AH, 9 ; display string function
INT 21H ; DOS call, display (DS:DX)
ENDM

LEA DX, ABC


DISP_MSG
LEA DX, DEF
DISP_MSG
Properties
• 1. Used directly by the marconame, no
need CALL and RET like subroutine.
• 2. Marco do not save memory space,
but subroutine did.
• 3. Marco save running time, but
subroutine did not, as call and return
from subroutine need protect and
restore break points.
Summary

• Three different classes of statements


– Executable instructions
• Tell CPU what to do
– Directives or pseudo-ops
• Provide information to the assembler
– Macros
• Name a group of statements and refer them by macros
name
Assignments
1. Give two differences between an instruction
statement and a directive statement.
2. What will processor do when the statement
BLOCK_1 DB 128 DUP(?) is processed by the
assembler?
3. Write the directives to define a data segment
called DATA_SEG that is aligned on a word-
address boundary, overlaps other segments with
same name.
4. What’s software interrupt and what are functions
in programming?
5. Write a program to do 1+2+3+……N, until the
sum exceeds 1000. Put the number of data into CN
and the sum into SUM.

You might also like