You are on page 1of 50

ASSEMBLY for 8086

Types of Programming Language


• Machine Language
Machine code, also known as machine language, is the
elemental language of computers. It is read by the computer's central processing
unit (CPU), is composed of digital binary numbers and looks like a very long
sequence of zeros and ones.
• Assembly Language
A low-level symbolic code converted by an assembler.
• High-Level Language
A high-level language (HLL) is a programming language such as C,
FORTRAN, or Pascal that enables a programmer to write programs that are more or
less independent of a particular type of computer. Such languages are
considered high-level because they are closer to human languages and further
from machine languages
Assembly vs HLL
Advantages of HLL
 Easier to write Advantages of Assembly
 Easier to
• Closer to Machine code
understand
 Better control • Access to specific
memory location
• Helps understand how
Doesn’t depend on computer actually works
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 etc (Pseudo Op-Code)
Syntax
General syntax for a statement is
Name: Instruction Operand(s) ; Comment

Example:
MYLABEL : MOV AX,BX ; abrakadabra
Name Field
• Used for instruction labels, procedure names and variable names
• 1 to 31 characters long
• Can contain letters, digits and following special characters
•.
•?
•_
•@
•$
•%
• Period must be used at the beginning
Syntax
• Operand : There can be 1,2 or no operand at all. For two operands,
generally, they are
Destination, Source
• Comment : Anything after (;)
• One of the operands can be a data

MOV AX,10 MOV AX,10D


MOV AX, 1010B MOV AX,0AH
Syntax
• Hex number can’t start with an alphabet
• +/- sign is allowed
• String or character is allowed as data

MOV AX, ‘ABCD’


MOV AX,0ABCDH
MOV AX, ABCD
MOV AX, ABCDH
Variables
Byte 8 DB
Word 16 DW
Double Word 32 DD
Quad Word 64 DQ
Ten Bytes 80 DT
Named Constant
• Name EQU constant

MYCONSTANT EQU 5

• EQU is a pseudo-opcode
• Constant can be a string as well
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


XCHG
XCHG destination, source

Source Operand Destination Operand


General Register Memory Location
General Register
Memory Location X

-no change in flag


ADD/SUB
ADD destination, source

Source Operand Destination Operand


General Register Memory Location
General Register
Memory Location X
Constant

-all flags are updated


INC/DEC
• Single operand instruction
• INC 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.
• Updates all flags
CF=1 unless result is 0
OF=1 if word destination is 8000h or byte destination is 80h
Type Agreement of Operands
• The operands of preceding two operand instructions must be of the
same type

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
Overflow, Shift , Rotate
Overflow
Decimal Range of Decimal Range of Decimal Range of Decimal Range of
Signed Numbers in a Unsigned Numbers in Signed Numbers in a Unsigned Numbers in
Word a Word Byte a Byte

-32768 to 32767 0 to 65535 -128 to 127 0 to 255

When the result of an operation falls beyond this range, an overflow


occurs
Overflow
• Signed Overflow
• Unsigned Overflow

In an arithmetic operation both signed and unsigned overflow can


occur simultaneously.
Overflow vs Carry
• CF=1 for unsigned overflow
• OF=1 for signed overflow
• Processor does not interpret the result as either signed or unsigned
• Its up to the programmer to interpret the result and depending
upon the convention he is following, he will use a flag and overlook
the other
Flags for Logic Instructions
• SF,ZF,PF are updated after each logical instruction
• CF=0 and OF=0
• NOT does not affect any status flags
Shift Instruction
• Opcode destination, 1
• Opcode destination, CL ; cl>1
• Shifts destination bit pattern at specified direction
• Handy alternative of mul/div

CF=last bit shifted out


OF=1; if sign changes
SHL
Shift Left
SAL
Shift Arithmetic Left
SHR
SAR
Shift Arithmetic Right
ROL
• Opcode destination, 1
• Opcode destination, CL ; cl>1
• Rotates destination bit pattern at specified direction

CF=last bit shifted out


OF=1; if sign changes
ROL
ROR
RCL
RCR
STACK
STACK MANAGEMENT
• A declared ‘1-D Data Array’
• Memory block in the RAM
• Last In First Out
• Used for temporary storage
Stack operation
• Stack pointer (SP) always points to the top of the stack.
• 2 operation of stack.
a. PUSH
saves a word in stack
Always increment SP.
b. POP
Retrieves a ‘WORD’ from the stack
Always increment SP
PUSH
• Saves a ‘WORD’ in stack
• PUSH source
• Lower Byte -> Lower Address of Stack
• Higher Byte -> Higher Address of Stack
• PUSH CX will save the current value of CX in the address pointed by
SP
POP
• Retrieves a ‘WORD’ from the stack
• POP destination
• POP CX gets the word from the address pointed by SP
• Neither PUSH , nor POP works on byte type registers.
• https://www.youtube.com/watch?v=-KQpk-dIA8s
ILLUSTRATION
ILLUSTRATION OF PUSH AX
ILLUSTRATION OF PUSH AX
ILLUSTRATION OF PUSH AX
ILLUSTRATION OF PUSH BX
ILLUSTRATION OF PUSH BX
ILLUSTRATION OF PUSH BX
ILLUSTRATION OF POP CX
ILLUSTRATION OF POP CX
ILLUSTRATION OF POP AX
ILLUSTRATION OF POP AX

You might also like