Chap 4
Chap 4
Provided by
Sadia Zaman Mishu
Assistant Professor, CSE Dept
RUET
Outline
From Book: Chapter 4
Operation Field
Operand Field
Comment Field
Program Data
Variables
Assembler directive, which instructs the assembler to perform some specific task (ex. Allocating
The fields do not have to be aligned in a particular column, but they must appear in the
above order.
An example of an instruction:
START: MOV CX,5 ; initialize counter
COUNTER1
@CHARACTER
.TEST
6
Operation Field
For an instruction, the operation field contains a symbolic operation code (opcode)
Opcode symbols often describe the operation’s function (ex. MOV, ADD, SUB)
Pseudo-ops are not translated into machine code, rather, they simply tell the assembler to
do something (ex. The PROC pseudo-op is used to create a procedure).
Examples:
NOP ; no operands... does nothing
INC AX ; one operand... adds 1 to the contents of AX
ADD WORD1,2 ; two operands... adds 2 to the contents of memory word WORD1
Destination operand
register or memory location Source operand
where the result is stored usually not modified
(note:some instructions by the instruction
don’t store the result)
A semicolon marks the beginning of this field, and the assembler ignores anything typed after
the semicolon.
Thus, comments are used to put the instruction into the context of the program
It is permissible to make an entire line a comment, and to use them to create space in a
program.
Hex: begins with a decimal digit and ends with ‘H’ or ‘h’
Characters & Character strings: enclosed in a single or double quotes or by there ASCII
codes.
Number Type
11011 Number Decimal
1101B Type Binary
64223 Decimal
-21843D Decimal
1,234 illegal
1BADH Hex
1B4D Illegal
FFFFH Illegal
0FFFFH Hex
Each variable has a data type and is assigned a memory address by the program
The table below shows the assembler directives that are used to define the variables
Example:
ALPHA DB 4 a memory byte is associated with the name
ALPHA, and initialized to 4.
BYT DB ? a memory byte is associated with the name
BYT, and uninitialized.
WRD DW -2 a memory word is associated with the
name WRD, and initialized to -2.
The decimal range is:
Unsigned representation: 0 to 255
Example:
B_ARRAY DB 10H,20H,30H
W_ARRAY DW 1000,40,29887,329
• Syntax:
repeat_count DUP (value)
• Example:
DELTA DB 212 DUP (?) creates an array of 212
uninitialized bytes.
8
Character String
Any array of ASCII codes can be initialized with a string of characters.
Example:
LETTERS DB 'ABC'
LETTERS DB 41H,42H,43H
21
Word Variables
name DW initial_value
WRD DW -2
• -32768 to 32767 for signed interpretation
• 0 to 65535 for unsigned interpretation
22
Arrays
• B_ARRAY DB 10H, 20H, 30H
23
Character Strings
• LETTERS DB ‘ABC’
• LETTER DB 41H, 42H, 43H
• MSG DB ‘HELLO’, 0AH, 0DH, ‘$’
• MSG DB 48H, 45H, 4CH, 4CH, 4FH, 0AH, 0DH, 24H
24
EQU
• The EQU (equates) pseudo-op is used to assign a name
to a constant.
name EQU constant
• LF EQU 0AH Same Machine
– MOV DL, 0AH Code
– MOV DL, LF
• PROMPT EQU ‘TYPE YOUR NAME’
– MSG DB ‘TYPE YOUR NAME’
– MSG DB PROMPT
26
MOV
• The MOV instruction is used to transfer data
between registers, between a register and a
memory location, or to move a number
directly into a register or memory location.
• MOV destination, source
MOV AX, WORD1
MOV AX, BX
MOV AH, ‘A’
27
XCHG
• The XCHG operation is used to exchange the
contents of two registers, or a register, and a
memory location.
• XCHG destination, source
XCHG AH, BL
XCHG AX, WORD1
28
Legal Combinations of Operands for MOV and XCHG
Destination Operand
Source Operand General Segment Memory
Register Register Location Constant
General Register Yes Yes Yes No
Segment Register Yes No Yes No
Memory Location Yes Yes No No
Constant Yes No Yes No
Destination Operand
Source Operand General Memory
Register Location
General Register Yes Yes
Memory Location Yes No
29
Restrictions on MOV and XCHG
ILLEGAL: MOV WORD1, WORD2
MOV AX, WORD2
MOV WORD1, AX
30
ADD and SUB
• The ADD and SUB instructions are used to add or
subtract the contents of two registers, a register
and a memory location, or to add (subtract) a
number to (from) a register or memory location.
ADD destination, source
SUB destination, source
ADD WORD1, AX
SUB AX, DX
ADD BL, 5
31
Restrictions on ADD and SUB
ILLEGAL: ADD BYTE1, BYTE2
32
Legal Combinations of Operands for ADD and SUB
Destination Operand
Source Operand General Memory
Register Location
General Register Yes Yes
Memory Location Yes No
33
INC and DEC
• INC (increment) is used to add 1 to the
contents of a register or memory location and
DEC (decrement) subtracts 1 form a register or
memory location.
INC destination
DEC destination
INC WORD1
DEC BYTE1
34
NEG
• NEG is used to negate the contents of the
destination.
• NEG does this by replacing the contents by its
two’s complement.
NEG destination
NEG BX
35
Type Agreement of Operands
MOV AX, BYTE1 ; illegal
MOV AH, ‘A’
MOV AX, ‘A’ ; move 0041h into AX
36
Translation of high-Level Language to
Assembly Language
Statement Translation
B=A
MOV AX, A; move A into AX
MOV B, AX; and then into B
37
Translation of high-Level Language to
Assembly Language
Statement Translation
A=5–A
MOV AX, 5; put 5 in AX
SUB AX, A; AX contains 5 – A
MOV A, AX; put it in A
A=5–A
NEG A ; A = –A
ADD A, 5 ; A = 5 – A
38
Translation of high-Level Language to
Assembly Language
Statement Translation
A=B–2xA
MOV AX, B; AX has B
SUB AX, A; AX has B – A
SUB AX, A; AX has B – 2 x A
MOV A, AX; move result to A
39
• Assembly language program occupies code, data and
stack segment in memory
40
Size of code and data, a program can have is determined by
specifying a memory model using .MODEL directive
.MODEL memory_model
Model Description
SMALL code in one segment
data in one segment
MEDIUM code in more than one segment
data in one segment
COMPACT code in one segment
data in more than one segment
LARGE code in more than one segment
data in more than one segment
no array larger than 64k bytes
HUGE code in more than one segment
data in more than one segment
arrays may be larger than 64k bytes 41
• A program’s data segment contains all the
variable definitions.
• Constant definitions are often made here as well,
but they may be placed elsewhere in the program
since no memory allocation is involved.
.data directive to declare a data segment
.DATA
WORD1 DW 2
WORD2 DW 5
MSG DB ‘THIS IS A MESSAGE’
MASK EQU 10010111B
42
• The purpose of the stack segment declaration
is to set aside a block of memory (the stack
area) to store the stack.
• The stack area should be big enough to
contain the stack at its maximum size.
.STACK 100H
• If size is omitted, by default 1kB is set aside
43
• The code segment contains a program’s
instructions.
.CODE name
• Inside a code segment, instructions are organized
as procedures.
name PROC
; body of the procedure
name ENDP
• The last line in the program should be the END
directive, followed by name of the main
procedure.
44
MAIN PROC
; instructions go here
MAIN ENDP
; other procedures go here
45
.MODEL SMALL
.STACK 100H
.DATA
; data definitions go here
.CODE
MAIN PROC
; instructions go here
MAIN ENDP
; other procedures go here
END MAIN
46
• CPU communicates with the peripherals
through IO ports
– IN and OUT instructions to access the ports
directly
• Used when fast IO is essential
• Seldom used as
– Port address varies among compluter models
– Easier to program IO with service routine
47
IO Service
routines
48
• I/O service routines
The Basic Input/Output System (BIOS) routines
The DOS routines
• The INT (interrupt) instruction is used to
invoke a DOS or BIOS routine.
• INT 16h
– invokes a BIOS routine that performs keyboard
input.
49
• INT 21h may be used to invoke a large number
of DOS functions.
• A particular function is requested by placing a
function number in the AH register and
invoking INT 21h.
50
Input:
AH = 1
Output:
AL = ASCII code if character key is pressed
= 0 if non-character key is pressed
51
MOV AH, 1 ; input key function
INT 21h ; ASCII code in AL
52
Input:
AH = 2
DL = ASCII code of the display character or
= control character
Output:
AL = ASCII code of the display character or
= control character
53
• MOV AH, 2 ; display character function
MOV DL, ‘?’ ; character is ‘?’
INT 21h ; display character
54
ASCII Code HEX Symbol Function
7 BEL beep
8 BS backspace
9 HT tab
A LF line feed (new line)
D CR carriage return (start of current
line)
55
• ECH.ASM will read a character from the
keyboard and display it at the beginning of the
next line.
• The data segment was omitted because no
variables were used.
• When a program terminates, it should return
control to DOS.
• This can be accomplished by executing INT
21h, function 4Ch.
56
TITLEECHO PROGRAM
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
; display prompt
MOV AH, 2 ; display character function
MOV DL, '?' ; character is '?'
INT21H ; display it
; input a character
MOV AH, 1 ; read character function
INT 21H ; character in AL
MOV BL, AL ; save it in BL
57
; go to a new line
MOV AH, 2 ; display character function
MOV DL, 0DH ; carriage return
INT 21H ; execute carriage return
MOV DL, 0AH ; line feed
INT 21H ; execute line feed
; display character
MOV DL, BL ; retrieve character
INT 21H ; and display it
; return to DOS
MOV AH, 4CH ; DOS exit function
INT 21H ; exit to DOS
MAIN ENDP
END MAIN
58
59
• An editor is used to create the preceding
program.
• The .ASM is the conventional extension used
to identify an assembly language source file.
60
• The Microsoft Macro Assembler (MASM) is
used to translate the source file (.ASM file)
into a machine language object file (.OBJ file).
• MASM checks the source file for syntax errors.
• If it finds any, it will display the line number of
each error and a short description.
• C:\>MASM File_Name;
61
• The Link program takes one or more object
files, fills in any missing addresses, and
combines the object files into a single
executable file (.EXE file)
• This file can be loaded into memory and run.
• C:\>LINK File_Name;
62
• To run it, just type the run file name.
• C:\>File_Name
63
Input:
DX = offset address of string.
= The string must end with a ‘$’ character.
64
• LEA is used to load effective address of a
character string.
• LEA destination, source
• MSG DB ‘HELLO!$’
LEA DX, MSG ; get message
MOV AH, 9 ; display string function
INT 21h ; display string
65
• When a program is loaded into memory, DOS
prefaces it 256 byte PSP which contains
information about the program
• DOS places segment no of PSP in DS and ES
before executing the program
• To correct this, a program containing a data
segment must start with these instructions;
MOV AX, @DATA
MOV DS, AX
66
. MODEL SMALL
.STACK 100H Print String
.DATA
MSG DB 'HELLO!$' Program
.CODE
MAIN PROC
; initialize DS
MOV AX, @DATA
MOV DS, AX ; intialize DS
; display message
LEA DX, MSG ; get message
MOV AH, 9 ; display string function
INT 21H ; display message
; return to DOS
MOV AH, 4CH
INT 21H ; DOS exit
MAIN ENDP
END MAIN 67
• CASE.ASM begins by prompting the user to
enter a lowercase letter, and on the next line
displays another message with the letter in
uppercase.
• The lowercase letters begin at 61h and the
uppercase letters start at 41h, so subtraction
of 20h from the contents of AL does the
conversion.
68
.MODEL SMALL
.STACK 100H
.DATA
CREQU0DH
LF EQU0AH
MSG1 DB 'ENTER A LOWER CASE LETTER: $'
MSG2 DB CR, LF, 'IN UPPER CASE IT IS: '
CHAR DB ?, '$'
.CODE
MAIN PROC
; intialize DS
MOV AX, @DATA ; get data segment
MOV DS, AX ; intialize DS
; print user prompt
LEA DX, MSG1 ; get first message
MOV AH, 9 ; display string function
INT 21H ; display first message
69
; input a character and convert to upper case
MOV AH, 1 ; read character function
INT 21H ; read a small letter into AL
SUB AL, 20H ; convert it to upper case
MOV CHAR, AL ; and store it
; display on the next line
LEA DX, MSG2 ; get second message
MOV AH, 9 ; display string function
INT 21H ; display message and upper case
letter in front
; DOS exit
MOV AH, 4CH
INT 21H ; DOS exit
MAIN ENDP
END MAIN
70