You are on page 1of 130

Prepared by

SHIHABUDEEN H

Prepared by
SHIHABUDEEN H

MICROPROCESSORS-8086
1.Architecture
2.Programming
3.Interfacing

For good study of microprocessors two types of


models are used :

Programmers model :- this model shows


features , such as internal registers, address
,data & control buses ; that we need to program
the device.
The hardware model:- this model shows the
pin diagram and the signals to/from this pins to
understand how a microcomputer system is
built around.

A microcomputer system is one which


uses a microprocessor as its cpu
In addition the microcomputer also has
a memory unit, input/output devices and
system buses.
The system buses are of three types:
1.Address bus
2.Data bus
3.Control bus
Physically buses are group of wires

The 8086 has


1.20 address lines
2.16 data lines
3.4-10 control lines.

Address bus
To address 1,048,,576 (220 ) memory
locations/ports.
Sends out the address of memory location
thats is to be written to or read from
Sends out the address of ports thats is to be
written to or read from

Data bus
To manipulate and/or operate on 16-bits(2bytes) of data at a time.
Read /write data from/to memory and port
through data bus

Data is three states 0,1,Z


Control bus

To generate necessary control signals for


proper working of address bus and data bus

8086 has 16-bit ALU; this means 16-bit numbers are


directly processed by 8086. Which carries out addition,
subtraction, AND, OR, XOR, increment, decrement,
complement, or shift binary numbers.
it has 16-bit data bus, so it can read data or write data
to memory or i/o ports either 16 bits or 8 bits at a time.
it has 20 address lines, so it can address up to 220 i.E.
1048576 = 1mbytes of memory (words i.E. 16 bit
numbers are stored in consecutive memory locations).
Due to the 1mbytes memory size multiprogramming
is made feasible as well as several multiprogramming
features have been incorporated in 8086 design.

8086 includes few features, which enhance


multiprocessing capability (it can be used with math
coprocessors like 8087, I/O processor 8089 etc.
Operates on +5v supply and single phase (single
line) clock frequency.(Clock is generated by
separate peripheral chip 8284).
8086 comes with different versions. 8086 runs at 5
MHz, 8086-2 runs at 8 MHz, 8086-1 runs at 10 MHz.
It comes in 40-pin configuration with HMOS
technology having around 20,000 transistors in its
circuitry.

It has multiplexed address and data bus like


8085 due to which the pin count is reduced
considerably
Higher Throughput (Speed)(This is achieved
by a concept called pipelining)But the concept
of 8086s principles and structures is very
useful for understanding other advanced Intel
microprocessors

HARDWARE

8086 CPU is divided into two parts


1.Bus Interface Unit
1.Sends out addresses
2.Fetch instructions
3.Read data from ports and memory
4.Write data to ports and memory
5.All transfer of data and address needed for EU
2.Execution Unit
1.Controls BIU
2.From where to fetch instructions and data
3.Decodes instructions
4.Excecute instructions

1.Queue
While an EU is decoding an instruction or
executing an instruction which does not require
use of the buses, Fetches up to six instruction
bytes of following instructions
When EU is ready for its next instruction, it
simply reads the queue in the BIU.
This is much faster than sending out the
address to the memory and waiting for the
memory to send back the next instruction byte
First in First out register( FIFO)

Each time CPU executes an instruction it takes some steps, called


machine cycle.A machine cycle can be broken down into smaller
cycles such as instruction cycle and execution cycle.

Fetching: Before the CPU can execute an instruction, the control unit
must retrieve (or fetch) a command or data from the computers
memory.
Decoding: Before a command can be executed, the control unit must
break down (or decode) the command into instructions.
Executing: Part of the execution cycle. When the command is
executed, the CPU carries out the instructions in order by converting
them into microcode.
Storing: The CPU maybe required to store the results of an instruction
in memory.

Fetching the next instruction while the current


instruction executes is called pipelining.
The control unit begins a new machine cycle, that
is it begins executing a new instruction before the
current cycle is completed.
Executions are performed in stages, when the
first instruction completes the fetching stage, it
moves to the decode stage, and a new instruction
is fetched.
Using this technology, new microprocessors can
execute up to six instructions simultaneously

2. Segmentation

20 bit address(220 = 1048576 bytes )

Four segment registers

Holds upper 16 bit of starting address

At a time 8086 works with one segment

4 segment are any where in the 1Mb memory

Extra segment register


Code segment register
Stack segment register
Data segment register

Facilitate the use of separate memory areas for


the program, its data and the stack.
Permit a program and/or its data to be put into
different areas of memory each time the
program is executed.
Multitasking becomes easy.
Speed of execution increase
Overlapping is possible

Segment registers stored the upper 16 bit of the


segment which the BIU is currently fetching
instructions
BIU insert lower 4 bits which is always zeros
segment base = upper 16 bit address
if segment base = 2000
Actual address is 20000H

HARDWIRED ZERO

Holds the 16 bit address of the next instruction


within the current segment

This value is known as Offset

Used to locate the 20 bit address sent out by the


BIU
If the CS register contain 348AH
Base Address = 348A0H
IP = 1234H
CS 3 4 8 A 0 Implied Zero
+IP
1234
----------------------3 5 A3 4 H
Physical address = CS:IP

Extra segment :- store data


Data segment :- store data
Code segment:- store code
Stack segment:- store Address and data

Stack segment: store address and data while


subprogram executes
Stack segment register holds upper 16 bits of
stack segment
Stack pointer used to hold the 16 bit offset from
the segment base to where data is recently stored
on the stack
Recent memory location is known as top of stack
Physical address is also calculated by adding
segment base with content of stack pointer
Physical address= SS: SP
Last In First Out register(LIFO)

Control Circuitry :- directs internal operations


Instruction Decoder:-decodes fetched
instructions from memory to series of actions
Arithmetic Logic Unit(ALU):- Addition,
Subtraction, multiplication division and logical
operations(XOR,OR,AND,..)
Flag register
General Purpose Registers

Indicates the status of a microprocessor


Some Conditions produced and some controls
16 bit register
Nine active flags
6 Condition flags and 3 control flags
Condition:- Set or reset on basis of arithmetic
and logical operation
Control :-Deliberately set or reset with
specific instruction by the
programmer

Condition Flags
Carry Flag
Auxiliary carry Flag
Zero Flag
Overflow Flag
Sign Flag
Parity Flag
Control Flags
Direction Flag
Trap Flag
Interrupt Flag

U U U U OF DF IF TF SF ZF U AF U PF U CF

C (carry) holds the carry after addition or


borrow after subtraction.
also indicates error conditions
P (parity) is the count of ones in a number
expressed as even or odd. Logic 0 for odd
parity; logic 1 for even parity.
if a number contains three binary one bits, it
has odd parity; If a number contains no one
bits, it has even parity

A (auxiliary carry) holds the carry (half-carry)


after addition or the borrow after subtraction
between bit positions 3 and 4 of the result.
Z (zero) shows that the result of an arithmetic
or logic operation is zero.
S (sign) flag holds the arithmetic sign of the
result after an arithmetic or logic instruction
executes.
O (overflow) occurs when signed numbers are
added or subtracted. an overflow indicates the
result has exceeded the capacity of the machine

I (interrupt) controls operation of the INTR


(interrupt request) input pin.
D (direction) selects increment or decrement
mode for the DI and/or SI registers.
T (trap) The trap flag enables trapping through
an on-chip debugging feature.

All general registers of the 8086 microprocessor can be


used for arithmetic and logic operations. All the
registers are 16 bit wide. The general registers are:
AX (Accumulator): This is accumulator
register. It gets used in arithmetic, logic and data
transfer instructions. In manipulation and division, one
of the numbers involved must be in AX or AL.

BX (Base Register): This is base register.


BX register is an address register. It usually contain a
data pointer used for based, based indexed or register
indirect addressing.

CX (Count register): This is Count


register. This serves as a loop counter. Program loop
constructions are facilitated by it. Count register can
also be used as a counter in string manipulation and
shift/rotate instruction.

DX (Data Register): This is data register.


Data register can be used as a port number in I/O
operations. It is also used in multiplication and
division.
SP (Stack Pointer): This is stack pointer
register pointing to program stack. It is used in
conjunction with SS for accessing the stack segment.

BP (Base Pointer): This is base pointer register


pointing to data in stack segment. Unlike SP, we can use BP
to access data in the other segments. Used to store the offset
values
SI (Source Index): This is source index register
which is used to point to memory locations in the data
segment addressed by DS. By incrementing the contents of SI
one can easily access consecutive memory locations.
DI (Destination Index): This is destination index
register performs the same function as SI. There is a class of
instructions called string operations, that use DI to access the
memory locations addressed by ES.

Three level of programming


1.
Machine language

Binary numbers 0,1

It is impossible to memorize
2.
Assembly language
Four letter mnemonics are used to represent instruction
Easy to memorize
Assembler is used to convert assembly languages to
binary words

A format is there for representing an operation


3.

High level language :-compiler are used to convert higher


level languages to machine codes

A wide variety of instructions are there in 8086


Depending upon the function instructions are
divided into many types
Decoding of instruction is done at the EU
There is some format in representing an
instruction (changes with instructions)
Label field
Next:

Opcode field
MOV

Operand field
Ax,ABC2H

Comment field
;move the value to Ax

Classified into 7 categories:


1] Data Transfer
2] Arithmetic
3] Logical
4] Control
5]Processor Control Instructions
6] String Manipulation
7] Interrupt Control

Note : Data Transfer Instructions do not affect any flags


1] MOV dest, src
Note that source and destination cannot be memory
location. Also source and destination must be same
type.
2] PUSH Src: Copies word on stack.
3] POP dest: Copies word from stack into dest. Reg.
4] IN acc, port : Copies 8 or 16 bit data from port to
accumulator.
a) Fixed Port
b) Variable Port
5] OUT port, acc

6] LES Reg, Mem: Load register and extra segment


register with words from memory.
7] LDS Reg,Mem: Load register and data segment
register with words from memory.
8] LEA Reg,Src: load Effective address.
(Offset is loaded in specified register)
9] LAHF: Copy lower byte of flag register into AH
register.
10] SAHF: Copy AH register to lower byte of flag

11] XCHG dest, src: Exchange contents of source


and destination.
12] XLAT: Translate a byte in AL.
This instruction replaces the byte in AL with
byte pointed by BX.To point desired byte in
look up table instruction adds contains of BX
with AL ( BX+ AL). Goes to this location and
loads into AL.

1]ADD dest,src
2] ADC dest,src: Add with carry
3] AAA : ASCII adjust after addition.
We can add two ASCII numbers directly and
use AAA after addition so as to get result
directly in BCD. (Works with AL only)
4] DAA : Decimal adjust accumulator.
( Works with AL only)

5] SUB dest, src


6] SBB dest, src: Subtract with borrow.
7] AAS: ASCII adjust for subtraction
( same as AAA and works with AL only)
8] DAS : Decimal adjust after Subtraction.
( works with AL only)
9] MUL src
10 ] IMUL src: Multiplication of signed byte.

11] AAM: BCD adjust after multiply.


(works with AL only)
12]DIV src
If any one attempts to divide by 0 , then ?
13] IDIV: Division of signed numbers
14]AAD: BCD to Binary convert before Division.
15] DEC dest

16] INC dest


17] CWD: Convert signed word to signed double
word.
18] CBW : Convert signed byte to signed word.
(CBW and CWD works only with AL, AX and
DX)
19] NEG dest: Forms 2s complement.

1] AND dest, src


2] NOT dest: Invert each bit in destination
3] OR dest, src
4] XOR dest, src
5] RCL dest, count : Rotate left through Carry
Rotate as many times as directly specified in the
instruction. For more no.of rotations, count can
be specified in CL register.
6] RCR dest, count : Rotate right through carry
7] ROL dest, count : Rotate left ( into carry as
well as into LSB)
8] ROR dest, Count : Rotate right ( into carry as
well as into MSB)

9] SAL/ SHL dest, count : Shift left and append 0s


on right.
10] SAR dest, count : Shift right retain a copy of
the S-bit and shift all bits to right.
11]SHR dest, count : Shift right append 0s on left
12] TEST dest, src: AND logically, updates flags
but source and dest are unchanged.

13] CMP dest, src


CF, ZF and SF are used
Ex. CMP CX,BX
CF ZF
CX = BX
0
1
CX > BX
0
0
CX < BX
1
0

SF
0
0
1

1]CALL : Call a procedure


Two types of calls:
i) Near Call ( Intra segment)
ii) Far Call ( Intersegment)
2] RET : Return execution from procedure
3] JMP : Unconditional Jump to specified
destination.
Two types near and Far

4] JA / JNBE: Jump if above / Jump if not below


The terms above and below are used when we
refer to the magnitude of Unsigned number .
Used normally after CMP.
5] JAE / JNB / JNC
6] JB / JC / JNAE
7] JBE / JNA
8] JE/ JZ

9] JCXZ: Jump if CX is Zero.


10] JG / JNLE: Jump if Greater /Jump if NOT less
than or equal.
The term greater than or less than is used in
connection with two signed numbers.
11] JGE / JNL:
12] JL / JNGE :
13] JLE / JNG :
14]JNE / JNZ :

15] JNO : Jump if no overflow


16] JNS : Jump if no sign
17] JS
18] JO
19] JNP / JPO
20] JP / JPE
In all above conditional instructions the
destination of jump is in the range of -128 to +
127 bytes from the address after jump.

21] LOOP: Loop to the specified label if CX is not


equal to Zero.
The count is loaded in CX reg. Every time LOOP is
executed, CX is automatically decremented - used in
delay programs

22] LOOPE/ LOOPZ: Loop while CX is not equal to zero


and ZF = 1.
23] LOOPNE / LOOPNZ: Loop while CX not equal to
zero and ZF = 0.
In all above LOOP instructions the destination of jump is
in the range of -128 to + 127 bytes from the address after
LOOP.

1] CLC: Clear Carry flag.


2] STC :Set carry Flag
3] CMC :Complement Carry Flag
4] CLD: Clear Direction Flag.
5] STD: Set Direction Flag
6] CLI :Clear Interrupt Flag.
7] STI : Set Interrupt Flag.
8] HLT: Halt Processing.

9] NOP : No Operation
10] ESC: Escape
Executed by Co-processors and actions are
performed according to 6 bit coding in the
instruction.
11] LOCK : Assert bus lock Signal
This is a prefix instruction.
12] WAIT :Wait for test or Interrupt Signal.
Assert wait states.

1] MOVS/ MOVSB/ MOVSW


Dest string name,src string name
This instn moves data byte or word from
location in DS to location in ES.
2] REP / REPE / REPZ / REPNE / REPNZ
Repeat string instructions until specified conditions
exist.
This is prefix a instruction.

3] CMPS / CMPSB / CMPSW


Compare string bytes or string words.
4] SCAS / SCASB / SCASW
Scan a string byte or string word.
Compares byte in AL or word in AX. String address is to be loaded in DI.

5] STOS / STOSB / STOSW


Store byte or word in a string.
Copies a byte or word in AL or AX to memory location pointed by DI.
6] LODS / LODSB /LODSW
Load a byte or word in AL or AX
Copies byte or word from memory location pointed by SI into AL or
AX register.

1]INT type
2] INTO Interrupt on overflow
3] IRET Interrupt return

1.

2.
3.

4.

Way of locating data or operands


There are two types of instructions
Sequential control flow and control transfer
instruction
Control will be transmitted to next instruction
after the execution
Control is transmitted to some predefined
address
Addressing modes are sequential control flow
instruction

A] Data Category

B] Branch Category

A] Data Category
1) Immediate Addressing
2) Direct Addressing
( Segment Override prefix)
3) Register Addressing
4) Register Indirect Addressing

5) Register Relative addressing


6) Base Index addressing
7) Relative Base Index addressing
B] Branch Category :(control transfer)
1) Intra segment Direct
2) Intra segment Indirect
3) Inter segment Direct
4) Inter segment Indirect

Suppose that in a program you need to put the


number 437BH in the CX register.
The MOV CX, 437BH instruction can be used to
do this.
When it executes this instruction will put the
immediate hexadecimal number 437BH in the
16-Bit CX register.
This is referred to as immediate addressing
mode.

Register addressing mode means that the


register is the source of an operand for an
instruction.
Example: The instruction MOV CX,AX.
The destination location is specified before the
comma and the source is specified after the
comma.
Note that the content of AX are just copied to
CX, not moved.

For the simplest memory addressing mode, the


effective address is just a 16-Bit number written
directly in the instruction.
Example: MOV BL, [437AH].
The square brackets around the 437AH are shorthand
for the content of the memory location.
When executed the content of that memory location
will be copied in the BL register.
The BIU calculates the 20-Bit physical address by
adding the effective address 437AH to the segment
base address.
The effective address 10H*DS+437AH
This is called direct addressing mode.

The address of memory location is not visible in


the instruction compared to direct addressing
mode
Example: The instruction MOV CX,[AX].
The square brackets around the AX are shorthand
for the content of the memory location.
When executed the content of that memory
location which is pointed by AX will be copied in
the CX register.
The effective address 10H*DS+[AX]
This is called register indirect addressing mode

Relative to some displacement


Example MOV AX, 50H[BX]
While executing the data is available at an
effective address formed by adding an 8 bit or
16 bit displacement with the content of any of
the registers(BX)
The effective address 10H*DS+50H+[BX]

Similar to register indirect addressing mode


Example MOV AX,[SI]
The square brackets around the SI are
shorthand for the content of the memory
location.
When executed the content of that memory
location which is pointed by SI will be copied
in the AX register.
The effective address 10H*DS+[SI]
This is called indexed addressing mode

Mixing of indexed and register indirect


It is modification to indexed addressing mode
Example MOV AX,[BX][SI]
Content at the effective address is copied to AX
register
Effective address is calculated by adding the
content of base register (BX or BP) to the
content of index register(SI or DI)
The effective address 10H*DS+[BX]+[SI]

Most complex addressing mode


Example MOV AX,50H[BX][SI]
Content at the effective address is copied to AX
register
Effective address is calculated by adding 8 or
16 bit displacement to sum of the content of
base register (BX or BP) to the content of index
register(SI or DI)
The effective address 10H*DS+50H+[BX]+[SI]

The control is to be transferred is in the same


segment
The address to which control is to be
transferred is appear directly in the instruction
The given displacement is added to the current
content of IP

The control is to be transferred is in the same


segment
The address to which the control is to be
transferred is not appear directly in the
instruction
The displacement is found in some register or
in some memory location

The control is to be transferred is in a different


segment
The address to which control is to be
transferred is appear directly in the instruction
(CS:IP or DS : IP)
The given displacement is Calculated by using
(CS:IP or DS : IP)

The control is to be transferred is in a different


segment
The address to which control is to be
transferred is not appear directly in the
instruction
The branch address is the content of memory
location containing four bytes IP (LSB), IP
(MSB) ,CS (LSB), CS (MSB) sequentially
The starting address is represented using some
addressing mode except immediate addressing
mode

DEBUG.com is a DOS utility that facilitate the


debugging and trouble shooting features of
assembly language programs
C:\users\user pc
Cd..
C:\users>
Cd..
C:\>
C:\>debug

-? :- displays all the commands


-R :- displays all registers and flags
-R reg
:- old content : new content
:- display specified register contents and
modify with the entered new contents
eg: R ax
-D seg : offset 1 offset 2
:- display memory contents in segment
from offset 1to offset 2
eg: D 3000 3005
-D :- display 128 memory location of RAM
starting from the current display pointer

-E :-enter hex data at current display pointer


-E seg : offset 1
:- Enter hex data at seg: offset 1 by byte.
The memory pointer is incremented by space
key, data entry is to be completed by enter key
eg: E 3000
-F seg: offset 1 offset 2 BYTE
:- fill the memory locations starting from
seg: offset 1 to offset 2 by the byte BYTE
eg: F 3000 3005 0A
-F seg: offset 1 offset 2 BYTE 1 BYTE 2 BYTE 3
:- fill the memory locations starting from
seg: offset 1 to offset 2 by the byte
BYTE 1 BYTE 2 BYTE 3
eg: F 3000 3002 0A 23 0B

-A :- assemble from the current CS:IP


-A seg: offset
eg: A 3000
:- Assemble the entered
instruction from seg : offset address
-U
:- un assemble from the current CS:IP
-U seg: offset
eg: U 3000
:- Un assemble the entered
instruction from seg : offset address
-G
:- Execute from the current CS:IP
-G= seg: offset1 offset 2
eg: G= 3000 3005
:- Execute the instructions from offset 1
to offset 2 in the current CS

-S seg: offset1 offset 2 BYTE/BYTES

:- search a BYTE or BYTES separated by


in the memory block seg: offset 1 to
offset 2 & display all the offset at which
the byte is found (eg: S 3000 3005 0A/000A
)
-Q
:- qui the debug & return to DOS
-T seg: offset
:- trace the program execution by
single stepping starting from the address seg:offset
-M seg: offset 1 offset 2 NB
:- move NB bytes
from offset one to offset 2 (eg: M 3000 3005 0A)
-C seg: offset 1 offset 2 NB
:- compare NB Bytes
in offset 1 with offset 2 & display the offsets at
which the comparison is true(eg: C 3000 3005 0A)

Write a program to add two 16 Write a program to transfer


bit numbers
a block of data (8 bit)

mov ax,0006
mov bx,0007
add ax,bx
mov [3000],ax
hlt

mov si,2000
mov di,3000
mov cx,000a
L1:mov al,[si]
mov [di],al
inc si
inc di
dec cx
jnz L1
hlt

mov si,4000
mov cx,[si]
mov ax,0000
inc si
inc si
mov ax,[si]
cmp ax,0000
jz L1

cmp ax,0001
jz L1
L3: dec cx
jz L2
mul cx
jmp L3
L1: mov ax,0001
L2: mov [3000],ax
hlt

Write a program to find the


largest among a set of 10 nos

mov si,2000
mov cx ,000a
mov ax,[si]
L1: inc si
inc si
dec cx
jz end
cmp ax,[si]
jnc L1
mov ax, [si]
jmp L1
END: mov si,3000
mov [si],ax
hlt

Write a program to find the


square root of a given no.

mov si,2000
mov al,[si]
mov bl,01
mov cl,00
L1: sub al,bl
jc L2
inc cl
inc bl
inc bl
jmp L1
L2: mov si,3000
mov [si],cl
hlt

mov si,4000
mov cx,[2000]
mov ax,0000
mov [si],ax
dec cx
jz END
mov bx,0001
inc si
inc si
mov [si],bx
dec cx

jz END
L1: inc si
inc si
add ax,bx
mov [si],ax
mov ax,bx
mov bx,[si]
dec cx
jnz L1
END:hlt

Programs which can be run to perform some


functions on the user program
Some manuals are there to help the
programmer

Program which allows the user to create file


containing assembly language statements
eg:- PC write ,word store
The editor comes with assemblers
As you type the program ,the editor stores the
ASCII codes of the letters and no's in successive
RAM locations
Editor let you to insert new line &move everything
down
After typing you can save the program as a source
file with an extension .ASM
eg:- sum.asm

Program used to translate assembly language


mnemonics to corresponding binary words
It reads the source file
On first pass, determines the displacement of
data items & offset of labels etc.
On Second pass it produce the binary codes
and inserts the displacement and offset etc
calculated in the first pass

It generates two files


Object file with extension .OBJ contain binary
codes
the second file called list file with extension
.LST contain instruction along with binary
codes & offset for each instruction
Assembler list the typing or syntax error in the
source program

Program used to join several object files into


one large object file for writing large program
We can divide the large program into smaller
modules
Each module can be individually written,
tested, debugged.
When all modules works, their object modules
can be linked together to form large program

On IBM PC we must run link program on our


.OBJ file(even it contains only one module)
It produces a link file which contains the binary
codes for all modules
It also produces a link map file which contains
the address information about the linked files
Link files have an extension .EXE

Program used to assign the specific address of


where the segments of object code are to be
loaded into memory
A locator program called EXE2BIN comes with
DOS converts .EXE file to a .BIN file which has
physical address

If the program does not need an external


hardware ,you can use a debugger to run and
debug your program .
It allows to load the object code to system
memory ,execute and debug it
You can change the contents of registers and
memory locations

The program allows the uses to stop the


execution after each instruction
It allows you to insert break points in the
program
It allows you to find the problems in the source
program
A basic debugger is there with DOS

Mixture of hardware and software


Used to test & debug the software and hard
ware of an external system
A multi wire cable connects the host system to
the system being developed
Through this connection the software of the
emulator allows the user to download the
object code program into the system being test
& run it

It allows us to load ,run ,examine, change the


contents of memory and register and insert
break point
Emulator takes the snap shot of the contents of
register & activity on address and data bus and
state of flags as instruction executes
This trace data is stored in a large RAM
A print out of trace data can be taken to see the
result of program execution

Assemble is used to convert the mnemonics of


instruction along with the data into their
equivalent object code modules
These object modules are then converted to
executable by using linker and loader
programs
Hence mnemonics are directly used in the
program

MASM needs the source program as its input


&provides an object file(.OBJ).
The LINK accepts the object file provides an
EXE file.
To write the ,use a text editor and save it with
an extension .ASM .
The MASM must be installed in your PC

C:\users\user pc
Cd..
C:\users>
Cd..
C:\>
D:
D:\> cd masm
D:\masm

Step 1: Edit
Step 2: Enter or type the program
Step 3: Save as filename.asm
Step 4: Quit editor
Step 5: Assembling process
D:\masm>
D:\masm> masm filename.asm
.LINK file created

Step 6: Linking process


D:\masm> LINK filename.obj
.EXE file created
Step 7: Execution process
D:\masm> filename
Step 8: Final output. This will open the
output window

Some predefined alphabetical strings called


directive
Used to give hints to the assembler while doing
assembling process
Another type of hints which helps to assign a
particular constant with a label or initialize
particular memory locations or labels with
constants is called operator

1] ASSUME
Used to tell assembler the name of logical segment.
eg: ASSUME CS: Code, DS:DATA
2] END
3] DB :- Define byte. Used to reserve byte/bytes in
memory. User can also initialize these bytes with
ASCII codes of characters specified as string
eg: Ranks DB 01H,02H 03H,04H
mes DB good morning

4] DW:-Define word. Reserves no of memory


words
eg: words DW 1234 H,5678H
wdata DW 5 dup(6666H)
5] DD Define Double Word
6] DQ Define Quad Word
7] DT Define Ten Bytes

eg:PROC procedure name

8] PROC Procedure
Procedure name ENDP
9] ENDP :- End of procedure
eg: EVEN
eg: DATA SEGMENT
10] ENDS:- End of segment
PROC ROOT
11] EQU
Assign a value or a symbol
ROOT ENDP
12] EVEN:
DATA ENDS
Align on even memory
address.
eg: CREATE EQU ADD
13] OFFSET
LABEL EQU 0003
14] PTR Pointer
eg: MOV SI ,OFFSET LIST
eg: MOV AL,BYTEPTR[SI]
:- specify data type

15]ORG :-starts the memory allotment for the


segment from declared address
16]LABEL
Ex: AGAIN LABEL FAR

17] PUBLIC
Links modules together

eg: ORG 2000


eg: mod1 segment
public Factorial
mod1 ends
mod2 segment
extern factorial
mod2 ends

18] EXTRN
Tells the assembler that the names or labels following
this directive is in some other assembly module as
PUBLIC

19] GROUP:-Grouping of logical segments.


eg: program group CODE,DATA
Assume CS:program ,DS: program
20] NAME
To give specific name to module.
eg: JMP SHORT LABEL
21] INCLUDE
eg: Mov ax, type string
Include source code from file.
22] SEGMENT
23] SHORT
Operator that tells assembler about short displacement.
24] TYPE :-Type of variable whether byte or word.
for byte data type=1 for word=2 for double word =4
25] GLOBAL,LOCAL

DOS acts as a user interface with computer


hardware resources( memory, CRT display
Hard disc, Floppy disc etc. )
Handled with the help of the instruction
INT 21H in DOS
Under this INTERRUPT ,the specific resources
are selected depending on the value in AH
Order is following :- MOV AH,XXH
INT 21 H

If AH = 09H
Displays the string pointed by DS:DX
If AH = 01H
The keyboard entry is accepted & its value is store
in AL register
If AH = 4CH
This will bring the computer to DOS prompt
If AH = 0AH
Reads the string from keyboard and stores to the
location pointed by DS: DX. Function will
terminate when enter key is pressed
If AH = 02H
It will send a character in DL to CRT display

Group of numbers or characters


12321122H
ASDFGHJKL
asdfghjkl

A set of repeated instructions in program


A CALL instruction is used to call procedure
This will send the processor to the starting address
of procedure while execution
Stack segment is used store the current execution
details for further execution
A RET instruction return execution control to the
next instruction in main line
While using procedure the machine codes
corresponding to the repeated instruction is put in
memory only one
Disadvantage is the time required for call and
return process

When repeated instruction are too short we


can use a MACRO
MACRO is a group of instruction with a name
normally given at the starting of a program
Each time we call macro, the assembler will
insert the group of instruction in the place of
call
Assembler generates the machine code each
time

So program take up more memory than


procedure
But it saves the overhead time involved in
calling and returning the procedure
Syntax
Macro name MACRO parameters

ENDM

After studying the problem, decide the logical


segment required
Almost all program require DATA & CODE
segments
The labels DATA & CODE is reserved by
MASM as the names of code and data
segments
All the data are declared in DATA segment
DATA ENDS marks the end of data segment
CODE segment contains the instruction

The label START is starting point of execution


sequence
The ASSUME directive informs the assembler
that the label CODE is used for code segment &
DATA for data segment
CS automatically loads with address
corresponding to the label CODE at the time of
execution
But DS is to be loaded by the programmer

So the first two lines performs this task


MOV AX,DATA
MOV DS, AX
The function of INT 21 H with 4CH in Ah is
used as last instruction which returns the
computer to DOS prompt
The statement CODE ENDS marks the end of
code segment
END START denotes the end of procedure
stated with label START

DATA SEGMENT

DATA ENDS
CODE SEGMENT
ASSUEME CS:CODE,DS:DATA
START: MOV AX, DATA
MOV DS,AX

MOV AH,4CH
INT 21H
CODE ENDS
END START

While displaying messages . They must be


terminated using $
The characters 0AH and 0DH are used as the
time feed &carriage feed for displaying
purpose
0AH brings the cursor to next position
0DH brings the cursor to next line
eg: MSG0 DB Hello$
MSG1 DB 0DH ,0AH Good Morning$

Wap to print hello


data segment
string db hello$
data ends
code segment
assume cs: code, ds: data
start: mov ax , data
mov ds , ax
mov dx , offset string
mov ah,09h
int 21h
mov ah,4ch
int 21h
code ends
end start

Wap to print hello using


macro

print macro args


mov dx, offset args
mov ah,09h
int 21h
endm
data segment
m1 db hello$
m2 db 0dh,0ah,good morning$
data ends
code segment
assume cs: code, ds: data
start: mov ax , data
mov ds , ax
print m1
print m2
mov ah,4ch
int 21h
code ends
end start

data segment
x db 01h
y db 02h
r db 02dup(?)
data ends
code segment
assume cs: code, ds: data
start: mov ax , data
mov ds , ax
mov ax , x

add ax , y
mov di, offset r
mov [di] , ax
mov ah,4ch
int 21h
code ends
end start

print macro msg


mov dx, offset msg
mov ah,09h
int 21h
endm
data segment
cr equ 0dh
lf equ 0ah
msg1 db enter the 1st no:$
msg2 db cr,lf, enter 2nd no:$
msg3 db cr,lf,sum=$
sum dw 0000h
outs dw 10 dup(?)
data ends

code segment
assume cs: code, ds: data
start: mov ax , data
mov ds , ax
print msg1
call read
add sum , cx
print msg2
call read
add sum,cx

print msg3
mov ax , sum
call disp
print outs
mov ah,4ch
int 21h

read proc
mov ax,0000h
mov bx,000ah
mov cx,0000h
L1: mov ah,01h
int 21h
cmp al.0dh
jz L2
mov ah,00h
sub al,30h
push ax
mov ax,cx
mul bx
mov cx,ax
pop ax
add cx,ax
jmp L1
L2: ret
read endp

disp proc
mov bx,000ah
mov cx,0000h
mov di , offset outs
mov dx,0024h
push dx
inc cx
L3: mov dx,0000h
div bx
add dx,0030h
push dx
inc cx
cmp ax,0000h
jz L3
L4: pop [di]
inc di
loop L4
ret
disp endp
code ends
end start

print macro msg


mov dx, offset msg
mov ah,09h
int 21h
endm
data segment
cr equ 0dh
lf equ 0ah
msg1 db enter the no:$
msg2 db cr,lf, factorial=1$
msg3 db cr,lf, factorial=$
outs dw 10 dup(?)
data ends

code segment
assume cs: code, ds: data
start: mov ax , data
mov ds , ax
print msg1
call read
cmp cx,0000h
jz L5
mov bx,0000h
mov ax,0001h

L6: push cx
inc bx
mul bx
pop cx
dec cx
jnz L6
print msg3
call disp

print outs
jmp L7
L5: print msg2
L7: mov ah,4ch
int 21h
read proc
disp proc
code ends
end start

print macro msg


mov dx, offset msg
mov ah,09h
int 21h
endm
data segment
cr equ 0dh
lf equ 0ah
m1 db enter the no:$
m2 db cr,lf, prime$
m3 db cr,lf, not prime$
m4 db cr,lf, neither
prime nor composite$
data ends

code segment
assume cs: code, ds: data
start: mov ax , data
mov ds , ax
print m1
call read
cmp cx,0000h
jz L5
cmp cx,0001h
jz L5
mov bx , cx

L7: mov dx,0000h


push cx
dec bx
cmp bx,0001h
jz L6
pop ax
mov cx,ax
div bx
cmp dx,0000h
jnz L7

print m3
jmp L8
L6: print m2
jmp L8
L5: print m4
L8: mov ah,4ch
int 21h
read proc
disp proc
code ends
end start

print macro msg


mov dx, offset msg
mov ah,09h
int 21h
endm
data segment
cr equ 0dh
lf equ 0ah
m1 db enter the string$
m2 db cr,lf, entered
string$
m3 db cr,lf, reversed
string$
outs1 dw 10 dup(?)
outs2 dw 10 dup(?)
data ends

code segment
assume cs: code, ds: data
start: mov ax , data
mov ds , ax
print m1
mov si , offset outs1
mov cx, 0000h
L1:
mov ah,01h
int 21h
cmp al,0dh
jz L2

mov [si],al
inc si
inc cx
jmp L1
print m2
print outs1
L2: mov di , offset outs2
L3: dec si
mov al,[si]
mov [di],al
inc di
loop L3

mov al,24h
mov [di],al
print m3
print outs2
mov ah,4ch
int 21h
code ends
end start

You might also like