ASSEMBLY for 8086
Reference: Yu/Marut
Chapter: 1,
Types of Programming Language
• Machine Language:
It contains bit strings. A CPU can only execute machine
language instructions.
• Assembly Language:
It uses symbolic names to represent operations,
registers, and memory locations.
A program written in assembly language must be
converted to machine language before the CPU can
execute it. Assembler translates assembly language
statements into machine language instructions.
• High-Level Language:
It is more like natural language text than assembly
language. Compiler translates high-level language
into machine language instructions.
Assembly vs. HLL
Advantages of HLL Advantages of Assembly
• Easier to write • Closer to Machine code
• Easier to • Access to specific
understand memory location
• Requires fewer • Helps understand how
statements computer actually
• Better control works
• Doesn’t depend on
particular machine
Remember
• ASSEMBLY language program is machine
specific
• ASSEMBLER converts it into machine
code
• Not case-sensitive
Syntax
• Each line of an ASSEMBLY program
contains a statement
• A statement can either be an instruction
or an assembler directive
• Instruction: MOV, SHL, JMP etc
• Assembler Directive: ORG, ASSUME,
END, PROC etc (Pseudo Op-Code)
Syntax
General syntax for a statement is
Name: Instruction Operand(s) ; Comment
Example:
MYLABEL : MOV AX,BX ; move the
contents of BX into AX
Name Field
• Used for instruction labels, procedure
names and variable names
• Assembler translates names into memory
addresses.
• 1 to 31 characters long
• Can contain letters, digits and following
special characters
. ? _ @ $ %
• Period(.) is used at the beginning only
• Blanks are not allowed
• Names may not begin with a digit
Name Field
Examples of legal names: Examples of illegal names:
COUNTER1 TWO WORDS
@character
2abc
SUM_OF_DIGITS
A45.28
$1000
YOU&ME
DONE?
Can you tell why each of
.TEST these is illegal?
Operation Field
• For an instruction, the operation field contains a
symbolic operation code (opcode). The
assembler translates opcode into machine
language code. Opcode symbols describe the
operation’s function; for example: MOV, ADD,
SUB.
• In an assembler directive, the operation field
contains a pseudo-opcode. Pseudo-ops are not
translated into machine code, they simply tell
the assembler to do something. For example:
PROC pseudo-op is used to create a procedure.
Operand Field
• The operand field specifies the data that are to be
acted on by the operation.
• Operand: There can be 1,2 or no operand at all. For
two operands, generally, the format is:
Destination, Source
One of the operands can be a data.
• Example:
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
Comment Field
• The comment field is used to say something about
what a statement does.
• Comment : Anything after (;)
• The assembler ignores anything typed after the
semicolon.
• An entire line can be made as a comment to create
space in program.
• Example:
;
; initialize registers
;
MOV AX, 0
MOV BX, 0
Program Data
• The processor operates only on binary data. The
assembler translates all data representation into
binary numbers.
• In an assembly program we may express data as
binary, decimal, hex numbers, or as characters.
Numbers:
• Binary: Bit string followed by “B” or “b”. For
example: 1010B, 1000b.
• Decimal: String of decimal digits. May or may not end
with “D” or “d”. For example: 1500, 3500D, 5000d.
• Hex: Must begin with a decimal digit and end with
“H” or “h”. For example: 0ABCH, 58B0h.
Legal & Illegal Numbers
Number Type
11011 Decimal
11011B binary
64223 decimal
-21843D decimal
1,234 Illegal; contains a non-digit character
1B4DH hex
1B4D Illegal hex number, doesn’t end in H
FFFFH Illegal hex number, doesn’t begin with a decimal digit
0FFFFH hex
Characters
• Characters & character strings must be enclosed in
single or double quotes; for example: “A” or ‘hello’.
• Characters are translated into their ASCII codes by
assembler. For example, the ASCII code for “A” is 41 h.
Data-defining Pseudo-ops:
Pseudo-op Stands for
DB Define byte
DW Define word
DD Define doubleword (two consecutive
words)
DQ Define quadword (four consecutive words)
DT Define tenbytes (ten consecutive bytes)
Syntax
• Hex number cant start with an alphabet
• +/- sign is allowed
• String or character is allowed as data
CORRECT
MOV AX, ‘ABCD’
MOV AX,0ABCDH
INCORRECT
MOV AX, ABCD
MOV AX, ABCDH
Variables
Byte 8 bits DB
Word 16 bits DW
Double Word 32 bits DD
Quad Word 64 bits DQ
Ten Bytes 80 bits DT
MYBYTE DB 15
MYWORD DW ?
MYARRAY DB 4,5,6,?,?,?
MYSTRING DB ‘abrakadabra’
Named Constant
• To make assembly code easier to understand, a
symbolic name may be given for a constant quantity.
For this, EQU pseudo-op is used.
Name EQU constant
MYCONSTANT EQU 5
• Constant can be a string as well. Example:
MSG DB ‘TYPE YOUR NAME’
Can be replaced with:
PROMPT EQU ‘TYPE YOUR NAME’
MSG DB PROMPT
• No memory is allocated for EQU names.
MOV
MOV destination, source
Source Destination Operand
Operand General Segment Memory Constant
Register Register Location
General X
Register
Segment X X
Register
Memory X X
Location
Constant X X
-no change in flag
Example:
MOV AX, WORD1; MOV AX,BX; MOV AH, ‘A’
XCHG
XCHG destination, source
Source Destination
Operand Operand
General Memory
Register Location
General
Register
Memory X
Location
-no change in flag
Example: XCHG AH, BL ; XCHG AX, WORD1
ADD/SUB
ADD destination, source
Source Destination
Operand Operand
General Memory
Register Location
General
Register
Memory X
Location
Constant
-all flags are updated
Example: ADD AH, BL ; SUB AX, WORD1 ; ADD BL, 5
INC/DEC
• Single operand instruction
• INC destination, DEC destination
• Destination is either a register or a
memory location.
• Updates all flags but CF
INC AX ; ax++
DEC MYBYTE; mybyte - -
NEG
• Single operand instruction
• NEG destination
• Destination is either a register or a memory
location.
• Replaces the contents by 2’s complement.
• Updates all flags
CF=1 unless result is 0
OF=1 if word destination is 8000h or byte
destination is 80h
• NEG AX
Type Agreement of Operands
• The operands of preceding two-operand
instructions must be of the same type;
both bytes or both words.
MOV AX, MYBYTE ; illegal
MOV AH, ‘A’ ; ah=41H
MOV AX, ‘A’ ; ax =0041H
ORG
• ORG stands for origin
• Assembler directive
• Displacement from the start of a segment
ORG 1000H
ORG $+1000H