Faculty of Engineering
Computer Engineering Department
Islamic University of Gaza
Assembly Language Lab # 2
Assembly Language Fundamentals
Eng. Alaa.I.Haniya
Assembly Language Fundamentals
Assembly Language :
Assembly Language is a programming language that is very similar to machine language, but
uses symbols instead of binary numbers. It is converted by the assembler (e.g. Tasm and Masm)
into executable machine-language programs.
To make programs in assembly language, you must know some information about the 8086
microprocessor. The 8086 contains 14 registers. Each register is 16 bits long. See Figure (1)
Assembly Language Fundamentals
Figure (1): Registers of 8086 microprocessor
Assembly Language Lab # 2
Each register has different usage as shown in Table (1) below. The general purpose registers
can be "split". You have the AH and the AL register for example. AH contains the high byte of AX
and AL contains the low byte. You also have: BH, BL, CH, CL, DL, DH So if for example. DX
contains the value 1234h DH would be 12h and DL would be 34h.
1
Assembly Language Fundamentals | 2/20/2013
Table(1): Registers of 8086 microprocessor and their purposes
And a 16-bit FLAG Register. The FLAGS Register consists of 9 status bits. These bits are also
called flags, because they can either be SET (1) or NOT SET (0). All these flags have a name
and purpose.
Abr. Name Description
OF Overflow if set ,an instruction generates an invalid signed result
Flag
DF Direction used for string operations to check direction
Flag
IF Interrupt Flag if set, interrupt are enabled, else disabled
TF Trap Flag if set, CPU can work in single step mode
SF Sign Flag if set, resulting number of calculation is negative
Assembly Language Lab # 2
ZF Zero Flag if set, resulting number of calculation is zero
AF Auxiliary is set when an operation produces a carryout from bit 3 to bit 4
Carry
PF Parity Flag is set when an instruction generates an even number of 1 bits in the low
byte of the destination operand.
CF Carry Flag is set when the result of an unsigned arithmetic operation is
too large to fit into the destination.
2
Table(2): FLAGS Register
Instruction Forms:
Assembly instructions are made up of an operation code (op-code) and a set of operands. The op-code
identifies the action to be taken. The operands identify the source and destination of the data.
The operands identify CPU registers, memory locations, or I/O ports. The complete form of an
instruction is:
Op-code destination operand, source operand
For example:
(Add 1 to register AX); one operand INC AX
(Store 100 in register AX); two operands MOV AX, 100
Assembly Language Fundamentals
Segments:
Code, Data, Stack an Extra. Within the 1 MB of memory space the 8086 defines four 64 K byte
memory blocks called the code segment, data segment, stack segment, and the extra segment.
Analysis of the Hello program:
Assembly Language Lab # 2
3
Instruction Description
DOSSEG is a directive to tell the assembler to arrange data
segment, code segment and stack segment as DOS
arrangement.
MODEL SMALL is a directive to tell the assembler to use one data
segment and one code segment.
.DATA is a directive to put in data segment.
.CODE is a directive to put in code segment.
@Data is a default address of data segment to put it in ax
register.
mov ax, @data As note we can't put data in ds register directly. So
we use intermediate register (ax) as in the mov ds,
mov ds, ax
ax
mov ah, 9 Put the service 9 in ah.
Assembly Language Fundamentals | 2/20/2013
int 21h (Interrupt 21 hexa), it has many services
like9,8,2, and each one has special work.
mov ah,4ch The two statements to terminate the execution of
the program.
int 21h
END is a directive to indicate the end of file
General-Purpose Registers:
Named storage locations inside the CPU, optimized for speed.
To Access Parts of Registers we can Use 8-bit name, 16-bit name, or 32-bit name ,this is applied
to EAX, EBX, ECX, and EDX . Assembly Language Lab # 2
4
Data Types:
Data Definition Statement:
A data definition statement sets aside storage in memory for a variable.
Assembly Language Fundamentals
Syntax:
[name] directive initializer [,initializer] . . .
value1 BYTE 10
db
All initializers become binary data in memory.
We can defines a single byte of storage, use multiple initializers , Defining Strings .
End-of-line character sequence:
0Dh = carriage return
Assembly Language Lab # 2
0Ah = line feed
5
Integer Constants
An integer constant (or integer literal) is made up of an optional leading sign, one or more digits
and an optional suffix character (called a radix) indicating the number's base:
[{+ | - }] digits [radix ]
Common radix characters:
h – hexadecimal
d – decimal
b – binary
o – octal
Examples: 30d, 6Ah, 42, 1101b, 777o
Hexadecimal beginning with letter: 0A5h
Assembly Language Fundamentals | 2/20/2013
Character Constants
A character constant is a single character enclosed in either single or double quotes. The
assembler converts it to the binary ASCII code matching the character. Examples are:
'A'
"x"
ASCII character = 1 byte.
String Constants
String constant is a string of characters enclosed in either single or double quotes:
'ABC'
"xyz" Assembly Language Lab # 2
'Say "Goodnight," Gracie'
Each character occupies a single byte.
6
Reserved Words
Assembly language has a list of words called reserved words. These have special meaning and
can only be used in their correct context. Reserved words can be any of the following:
Instruction mnemonics, such as MOV, ADD or MUL which correspond to built-in
operations performed by Intel processors.
Directives, which tell MASM how to assemble programs.
Used to declare code, data areas, select memory model, declare procedures, etc.
not case sensitive
For example .data, .code, proc.
Attributes, which provide size and usage information for variables and operands.
Examples are BYTE and WORD.
Operators, used in constant express ions. For example (+, -, …)
Predefined symbols. Such as @data which return constant integer values at assembly
time.
Identifiers
Assembly Language Fundamentals
They are not case sensitive.
The first character must be a letter (A..Z, a..z), underscore (_), @ , ?, or $. Subsequent
characters may also be digits.
An identifier cannot be the same as an assembler reserved word.
Comments
Comments can be specified in two ways:
Single-line comments, beginning with a semicolon character ( ; ). All characters following
the semicolon on the same line are ignored by the assembler and may be used to
comment the program.
Assembly Language Lab # 2
Block comments, beginning with the COMMENT directive and a user-specified symbol.
All subsequent lines of text are ignored by the assembler until the same user-specified
symbol appears. For example:
7
Using the DUP Operator:
Use DUP to allocate (create space for) an array or string.
Syntax:
Counter DUP ( argument )
Counter and argument must be constants or constant expressions.
var1 db 20 DUP(0) ; 20 bytes, all equal to zero
Assembly Language Fundamentals | 2/20/2013
var2 db 20 DUP(?) ; 20 bytes, uninitialized
var3 db 4 DUP("STACK") ; 20 bytes: "STACKSTACKSTACKSTACK"
var4 db 10,3 DUP(0),20 ; 5 bytes
Assembly Language Lab # 2
8
Homework:
1.Write an assembly language program to print all letters as follows:
AB..........YZ
Note: To print a character on the screen you have to use the int 21h with the service 2, the
character to be printed have to be in dl. For Example, the following code print A on the screen.
mov ah, 2
mov dl, 41h
int 21h
Note: Don’t use loop.
2.Print the following output on the command window using the DUP Operator:
*
**
***
****
*****
Assembly Language Fundamentals
3. Write the following message on the console using (0Ah = line feed):
This
is
my
H.W
Assembly Language Lab # 2