Académique Documents
Professionnel Documents
Culture Documents
Assembly language unlocks the secrets of computer hardware and software. Assembler will
converts source code to machine own language. Microsoft Macro Assembler (MASM), a product
of Microsoft Corporation includes several features which makes programming efficient and
productive. This lab sheet gives an overview of how to use MASM for assembling the 8086
programs.
MASM is a command line assembler. User has to first create a file and write assembly program using
DOS editor before using the assembler. It does not have provisions to create, edit, save and debug
programs. All the commands accepted by the assembler are entered at the DOS prompt.
i.e. C:\masm\
After typing the program assemble the program using masm command.
Create a file with .asm extension by typing the following command at the DOS command prompt.
C:\masm611\bin>EDIT sourcefilename.asm
Run the assembler to create the .com or .exe file by typing the following command at the command
prompt:
C:\masm611\bin>MASM sourcefilename.asm.
The Source window contains the text of your program in assembly language and the line numbers. When
CodeView executes one command in your executable module (.COM), it highlights the corresponding
line in your source code in the Source window. To execute one command, press the F8 or F10 key.
The Register window reflects the current state of the registers and flags. The registers are represented by
name (AX, BX and so on) and their hexadecimal values. Flag values however have symbolic denotation
(NZ, OV and so on). The Register window is situated on the right side of the screen. This window appears
when you press the F2 key and disappears if you press it again.
The Command window is intended for controlling the debugging process. The cursor is usually located in
this window, indicating that you can type in commands.
Command q is to quit the code view. This command finishes the debugging process, and returns control to
MS-DOS. The next most useful command is the h command, which means Help. Use this command to
get online information about CodeView commands, parameters and features. These two commands are
practically all you need to start working with CodeView.
(a) The curly brackets indicate those items that are not present or are optional in some statements.
(b) The name field is used for instruction labels, procedure names, segment names, macro names,
names of variables, and names of constants.
(c) MASM 6.1 accepts identifier names up to 247 characters long. All characters are significant,
whereas under MASM 5.1, names are significant to 31 characters only. Names may consist of
letters, digits, and the following 6 special characters: ? . @ _ $ % .If a period is used; it must
be the first character. Names may not begin with a digit.
(d) Instruction mnemonics, directive mnemonics, register names, operator names and other words
are reserved.
Syntax of an instruction:
{label:} mnemonic {operand { , operand} } {; comment}
The curly brackets indicate those items that are not present or are optional in some instructions. Thus
an instruction may have zero, one, or two operands.
Operators:
The 8086/8088 Assembly language has a number of operators. An operator acts on an operand or
operands to produce a value at assembly time. Examples are: + , - , *, / , DUP, and OFFSET
Comments:
A semicolon starts a comment. A comment may follow a statement or it may be on a separate line.
Multiple-line comments can be written by using the COMMENT directive. The syntax is:
COMMENT delimiter {comment}
comment
...
delimiter { comment }
where delimiter is any non-blank character not appearing in comment. The curly brackets indicate an
item that is optional.
e.g.,
COMMENT *
Numbers:
(a) A binary number is suffixed by b or B.
e.g., 11010111B
(c) A hexadecimal number must begin with a decimal digit and it is suffixed by h or H
Characters:
A character is enclosed in a pair of single quotes or in a pair of double quotes.
e.g., x B
Strings:
A string is enclosed in a pair of single quotes or in a pair of double quotes.
For a string delimited by single quotes, a pair of consecutive single quotes stands for a single quote.
Data definition
Each variable has a data type and is assigned a memory address by the program. The data-defining
directives are:
Directive Description of Initializers
e.g., ALPHA DB 4
VAR1 DB ?
VAR2 DW 3AB4h
POINTER1 DD 6BA7000AH
A ? in place of an initializer indicates you do not require the assembler to initialize the variable. The
assembler allocates the space but does not write in it. Use ? for buffer areas or variables your program
will initialize at run time.Example: Type the following program using EDIT and save it with .asm
extension.
.model tiny
.code
.startup
mov ax,50h
mov bx,20h
mov dx,80h
mov cx,05h
loop1: dec cx
jnz loop1
.exit
end
The DUP operator can be used to generate multiple bytes or words with known as well as un-
initialized values.
stars db 50 dup(*)
ARRAY3 DB 30 DUP(?)
Note: If a variable name is missing in a data definition statement, memory is allocated; but no name
is associated with that memory. For example:
DB 50 DUP(?)
In MASM 6.1 and above, a comma at the end of a data definition line (except in the comment field)
implies that the line continues. For example, the following code is legal in MASM 6.1:
longstring BYTE "This string ",
"continues over two lines."
bitmasks BYTE 80h, 40h, 20h, 10h,
08h, 04h, 02h, 01h
Named constants:
The EQU (equate) directive, whose syntax is:
LF EQU 0AH
where type (for MASM Version 5.1 and lower versions) is BYTE, WORD, DWORD, QWORD,
TBYTE, NEAR, or FAR provides a way to define or redefine the type associated with a variable or a
label.
Example1:
Here ARRAY1 defines a 50-word array, and ARRAY2 defines a 100-byte array. The same memory
locations are assigned to both arrays. Thus the array can be accessed as either the byte array
ARRAY1 or the word array ARRAY2.
Example2:
BYTE1 DB ?
BYTE2 DB ?
BYTE3 DB 50H
BYTE4 DB 66H
in this example, each of the words, and each of the bytes of the double word variable VAR3 can be
accessed individually.
SEGMENT DEFINITION
An 8086/8088 assembly language program file must have the extension .asm
There are two types of 8086/8088 assembly language programs: exe-format and com-format.
An exe-format program generates executable files with extension .exe. A com-format program
generates executable files with extension .com .
An exe-format program must contain a code segment and a stack segment. It may contain a data
segment or an extra segment.
Note: In a program, the data, code, and stack segments may appear in any order. However, to avoid
forward references it is better to put the data segment before the code segment.
The memory-model may be TINY, SMALL, MEDIUM, COMPACT, LARGE, HUGE or FLAT :
memory- description
model
TINY One segment. Thus code and data together may not be greater than 64K
SMALL One code-segment. One data-segment. Thus neither code nor data may be greater
than 64K
MEDIUM More than one code-segment. One data-segment. Thus code may be greater than
64K
COMPACT One code-segment. More than one data-segment. Thus data may be greater than
64K
LARGE More than one code-segment. More than one data-segment. No array larger than
64K. Thus both code and data may be greater than 64K
HUGE More than one code-segment. More than one data-segment. Arrays may be larger
than 64K. Thus both code and data may be greater than 64K
FLAT One segment up to 4GB. All data and code (including system resources) are in a
single 32-bit segment.
All of the program models except TINY result in the creation of exe-format programs. The TINY
model creates com-format programs.
The .CODE directive may be followed by the name of the code segment.
The .STACK directive may be followed by the size of the stack segment, by default the size is 1K i.e.,
1,024 bytes.
The definition of a segment extends from a simplified segment directive up to another simplified
segment directive or up to the END directive if the defined segment is the last one.
SP
Stack segment
SS
Data segment
Code segment
CS , IP
DS , ES
The CS and IP registers are automatically initialized to point to the beginning of the code segment.
The SP register is initialized to point one byte beyond the stack segment.
The DS and ES registers are initialized to point to the beginning of the PSP (Program Segment Prefix)
segment.
This is a 100H (i.e., 256) byte segment that DOS automatically prefaces to a program when that
program is loaded in memory.
Thus if a program contains a data segment, the DS register must be initialized by the programmer to
point to the beginning of that data segment.
Similarly if a program contains an extra segment, the ES register must be initialized by the
programmer to point to the beginning of that extra segment.
Initialization of DS
Note: The instructions which initialize the DS register for an exe-format program with simplified
segment directives are:
MOV AX , @DATA
MOV DS , AX
At load time, @DATA is replaced with the 16-bit base address of the data segment.
Thus @DATA evaluates to a constant value; such an operand is usually called an immediate operand.
MOV DS , @DATA
is invalid. Such an initialization is done indirectly using any 16-bit general-purpose register. Example:
MOV AX , @DATA
MOV DS , AX
Note: Every 8086 assembly language program must end with the END directive. This directive may
be followed by an entry label, which informs the assembler the point where program execution is to
begin. The entry label can have any valid name.
Follow the steps given below to debug the program and view the results.
Instruction Set:
Instructions Operands Description
Copy operand2 to operand1.
REG, memory
MOV memory, REG The MOV instruction cannot:
REG, REG Set the value of the CS and IP registers.
memory, immediate Copy value of one segment register to another segment
REG, immediate register (should copy to general register first).
SREG, memory Copy immediate value to segment register (should copy to
memory, SREG general register first).
REG, SREG
SREG, REG
Algorithm: operand1 = operand2
Ex:
Mov AX,BX ;Copy contents of BX to AX
Mov si,00h ;load Si with 00h
Unsigned multiply.
MUL REG Multiply the contents of REG/Memory with contents of AL register.
Memory Algorithm:
Unconditional Jump.
JMP Label Transfers control to another part of the program. 4-byte address
may be entered in this form: 1234h: 5678h, first value is a segment
second value is an offset.
JA Label Short Jump if first operand is Above second operand (as set by CMP
instruction). Unsigned.
JAE Label Short Jump if first operand is Above or Equal to second operand (as
set by CMP instruction). Unsigned.
Algorithm: if CF = 0 then jump
Jump If Below.
JB Label Short Jump if first operand is Below second operand (as set by CMP
instruction). Unsigned.
Algorithm:
if CF = 1 then jump
JBE Label Short Jump if first operand is Below second operand (as set by CMP
instruction). Unsigned.
Algorithm:
if CF = 1 then jump
Jump If Carry
Algorithm:
if CF = 1 then jump
Jump If Equal.
JE Label Short Jump if first operand is Equal to second operand (as set by
CMP instruction). Signed/Unsigned.
Algorithm:
if ZF = 1 then jump
Jump If Greater
JG Label Short Jump if first operand is Greater then second operand (as set
by CMP instruction). Signed.
Algorithm:
JGE Label Short Jump if first operand is Greater or Equal to second operand
(as set by CMP instruction). Signed.
Algorithm:
if SF = OF then jump
Jump If Less than.
JL Label Short Jump if first operand is Less then second operand (as set by
CMP instruction). Signed.
Algorithm:
JLE Label Short Jump if first operand is Less or Equal to second operand (as
set by CMP instruction). Signed.
Algorithm:
JNZ Label Short Jump if Not Zero (not equal). Set by CMP, SUB, ADD, TEST,
AND, OR, XOR instructions.
Algorithm:
if ZF = 0 then jump
Jump If Zero.
JZ Label Short Jump if Zero (equal). Set by CMP, SUB, ADD, TEST, AND,
OR, XOR instructions.
Algorithm:
if ZF = 1 then jump
Load Effective Address.
Algorithm:
LOOP Label
CX = CX - 1
if CX <> 0 then
o jump
else
o no jump, continue
Add.
REG, memory
ADD memory, REG Algorithm:
REG, REG
memory, immediate operand1 = operand1 + operand2
REG, immediate
Subtract.
REG, memory
memory, REG Algorithm:
SUB REG, REG
memory, immediate operand1 = operand1 - operand2
REG, immediate
CF = 1
Decimal adjust After Subtraction.
Corrects the result of subtraction of two packed BCD values.
REG Algorithm:
DIV Memory
when operand is a byte:
AL = AX / operand
AH = remainder (modulus)
when operand is a word:
AX = (DX AX) / operand
DX = remainder (modulus)
Shift Left.
memory, immediate
SHL REG, immediate Shift operand1 Left. The number of shifts is set by operand2.
memory, CL Algorithm:
REG, CL
Shift all bits left, the bit that goes off is set to CF.
memory, CL Algorithm:
REG, CL
Shift all bits right, the bit that goes off is set to CF.
Example:
STC ; set carry (CF=1).
MOV AL, 1Ch ; AL = 00011100b
RCL AL, 1 ; AL = 00111001b, CF=0.
RET
C O
r r
REG Algorithm:
PUSH SREG
memory SP = SP - 2
Algorithm:
AF = 1
CF = 1
else
AF = 0
CF = 0
in both cases:
clear the high nibble of AL.
Example:
MOV AX, 15 ; AH = 00, AL = 0Fh
AAA ; AH = 01, AL = 05
Algorithm:
CF = 1
else
AF = 0
CF = 0
in both cases:
clear the high nibble of AL.
Example:
MOV AX, 02FFh ; AH = 02, AL = 0FFh
AAS ; AH = 01, AL = 09
Algorithm:
AH = AL / 10
AAM No Operands AL = remainder
Example:
MOV AL, 15 ; AL = 0Fh
AAM ; AH = 01, AL = 05
Interrupt INT 21h:
Function 01h - Read character from standard input, result is stored in AL. If there is no character in the keyboard
buffer, the function waits until any key is pressed.
Example:
INT 21h
AH =02h
Example:
INT 21h
Function 06h Direct console for input/output. If DL = 0FFH on entry, then this function reads the console. If DL
= ASCII character, then this function displays the ASCII character on the console video screen.
Example:
INT 21h
String must be terminated by '$'. The string can be of any length and may contain control characters such as
carriage return (0DH) and line feed (0AH).
AH = 09h
Example:
INT 21h
Example:
INT 21h
Returns:
Example:
Mov CX, 01
INT 21h
Invoked by: DS: DX -> ASCIZ filename (no wildcards, but see notes).
AH=41h
Return:
Example:
Example:
INT 21h
INT 10h calls the BIOS functions. This interrupt often called the video services interrupt as it directly
controls the video display in a system.
Example:
MOV BH, 00
MOV DH, 06
MOV DL, 10
INT 10h
AH = 03h
Example:
Mov BH, 0
INT 10h
Invoked by: AL = number of lines by which to scroll. (00h = clear the entire screen.)