Unit
Unit
Syllabus:
⦁ One key benefit of using assembly language is that it allows programmers to write
highly optimized code for the specific hardware a program will run on. This is
because assembly language instructions map directly to the machine language
instructions that the CPU can execute. By writing code optimized for the
hardware, programmers can create programs that run faster and more efficiently
than those written in higher-level languages.
1
Programming with Intel 8085 Microprocessor
Instruction and Data Format
Instructions
An instruction is a statement that becomes executable when a program is
assembled. Instructions are translated by the assembler into machine language bytes,
which are loaded and executed by the CPU at runtime. An instruction contains four basic
parts:
⦁ Label (optional)
⦁ Instruction mnemonic (required)
⦁ Operand(s) (usually required)
⦁ Comment (optional)
The fields in the square brackets are optional. A basic instruction has two parts, the
first one is the name of the instruction (or the mnemonic), which is to be executed, and
the second are the operands or the parameters of the command.
Label
A label is an identifier that acts as a place marker for instructions and data. A label placed
just before an instruction implies the instruction’s address. Similarly, a label placed just
before a variable implies the variable’s address.
⦁ Data Label:
A data label identifies the location of a variable, providing a convenient way to
reference the variable in code. The following, for example, defines a variable
named count:
So, the line count DWORD 100 is telling the assembler to reserve 4 bytes of
memory to store the variable count and initialize it with the value 100. This memory
space can be used to store and manipulate the variable throughout the program.
⦁ Code Label:
A label in the code area of a program (where instructions are located) must
end with a colon (:) character. Code labels are used as targets of jumping and
looping instructions.
For example, the following JMP (jump) instruction transfers control to the
location marked by the label named target, creating a loop:
target:
mov ax,bx
...
jmp target
Label names are created using the rules for identifiers one can use the same
code label more than once in a program as long as each label is unique within its
enclosing procedure. (A procedure is like a function.)
Instruction Mnemonic
An instruction mnemonic is a short word that identifies an instruction. In English,
a mnemonic is a device that assists memory. Similarly, assembly language instruction
mnemonics such as mov, add, and sub provide hints about the type of operation they
perform.
Following are examples of instruction mnemonics:
3
⦁ mul: Multiply two values
⦁ jmp: Jump to a new location
⦁ call: Call a procedure
Operands
Assembly language instructions can have between zero and three operands, each of
which can be a register, memory operand, constant expression, or input-output port.
Zero operand:
The STC and NOP instruction, for example, has no operands:
One operand:
The INC instruction has one operand:
inc eax ; add 1 to EAX
Two operand:
The MOV instruction has two operands:
mov count,ebx ; move EBX to count
In a two-operand instruction, the first operand is called the destination. The second
operand is the source.
In general, the contents of the destination operand are modified by the instruction. In a
MOV instruction, for example, data is copied from the source to the destination.
Three operand:
The IMUL instruction has 3 operands, in which the first operand is the destination, and
the following 2 operands are source operands:
imul eax,ebx,5
In this case, EBX is multiplied by 5, and the product is stored in the EAX register.
4
Comments
Comments are an important way for the writer of a program to communicate information
about the program’s design to a person reading the source code. The following
information is typically included at the top of a program listing:
⦁ Description of the program’s purpose
⦁ Names of persons who created and/or revised the program
⦁ Program creation and revision dates
⦁ Technical notes about the program’s implementation
COMMENT !
This line is a comment.
This line is also a comment.
Instruction set:
5
A binary command that is used to perform a function in the microprocessor over
provided data is known as instruction. In another words it is a binary pattern designed
inside a microprocessor to perform a specific function.
Every instruction includes two parts like Opcode & the Operand where Opcode is used to
specify the function to be executed & operand gives the data to be functioned on.
Instruction format of 8085 microprocessor is given below.
Opcode :
Represents the operation to be performed on the operand. It is also called mnemonic.
Operand :
Data or address is given in this part .That is the operand ( or data) may include 8-bit
or 16-bit data, on the internal register, a memory location, or an 8-bit or 16-bit address.
If the operand is an 8-bit data, only Operand-1 is present in the instruction. If the
operand is a 16-bit data or address, Operand-1 and Operand-2 are specified in the
instruction. Both Operand-1 and Operand-2 are optional.
6
Classification of instructions based on size
There are three groups of instructions in 8085 microprocessor based on the length or size
of the instruction. They are,
Example :
⦁ MOV C,A
⦁ ADD B
7
Add the contents of the register B to the contents of the accumulator
⦁ CMA
This type of instruction has Opcode and one operand. The first byte represents the
Opcode and the second byte represents the 8-bit operand data or 8-bit port address.
Example :
⦁ MVI A, 50H
Load an 8-bit data byte in the accumulator
8
This type of instruction has Opcode and two operands. The first byte represents the
Opcode, the second byte presents the lower order 8-bits of data or address and the third
byte represents the higher order 8-bits of data or address.
Example :
⦁ JMP 2085H
Transfer the program sequence to the memory location 2085H. Here, C3 is 1st
byte, 85 2nd byte, and 20 3rd byte.
⦁ LDA 2050H
Opcode- LDA
Operand- 2050H
Hex Code- 3A, 50,20
9
Example:
Mov r, M
This instruction transfers data present within the memory (M) to the
register (r). But, the memory location address must be there within the HL
register.
⦁ Logical Instruction
Logical instructions are mainly used to perform different operations like
logical or Boolean over the data available in either memory or register. These
10
instructions will modify the flag bits based on the operation executed.
Example: CMP B
⦁ Branching Instruction
These types of instructions are mainly used to transfer or switch the
microprocessor from one location to another. So, it simply changes the general
sequential flow.
JMP address (Jump unconditionally)
This type of instruction is mainly used to transfer the series of the current
program to that location of memory whose 16-bit address can be simply specified
within the operand of the instruction.
Example: JMP 2014H
⦁ Control Instruction
These instructions are mainly used to control the microprocessor operations.
These instructions are discussed below.
NOP (No operation)
NOP stands for no operation. Once the 8085 microprocessor gets this instruction,
then it does not perform any operation based on execution.
More Examples
⦁ Data Transfer:- MOV, IN, OUT, STA,LDA, LXI, LDAX, STAX, XCHG
⦁ Arithmetic and Logic:- ADD, SUB, INR, DCR, AND, OR, XOR, CMP, RLC,
RRC, RAL, RAR
⦁ Branching:- JMP, JNZ, JZ, JNC, JC, CALL
⦁ Stack:- PUSH, POP
11
Data Transfer
MOV
⦁ MOV Rd,Rs
⦁ This instruction copies data from source register Rs to the Destination register
Rd.
⦁ The source register Rs and destination register Rd can be any general purpose
register like A,B,C,D,E,H or L.
⦁ The contents of source register remain unchanged.
Example:
MOV B,C
⦁ MOV R, M
⦁ This instruction copies data from memory M to register R.
⦁ The term M specifies the HL memory pointer. The contents of HL register pair
are used as the address of the memory location. The contents of that memory
location are transferred to the specified register R.
⦁ The register R can be any general purpose register like A, B, C, D, E, H or L.
Mnemonic MOV R, M
Operation R=M or R= (HL)
No of bytes 1 byte.
Machine Cycles 2 (0F + MR)
T-states 4+3=7
Example
MOV C, M.
⦁ This instruction will copy the data from the memory location pointed by HL
register pair to C register.
⦁ Let the contents of the HL register pair be F000H, register C =20H. At the address
F000H : 10H is stored. The HL register pair contents are used as address. i.e. HL
12
= F000H.
⦁ The contents of memory location F000H are copied to the C register. So contents
of C register will change from 20H to 10H
⦁ The contents of memory location remain unchanged.
Note
⦁ Whenever the term M Comes in an instruction, its memory pointer. The address
will be given by the HL register Pair.
⦁ Brackets around HL specify that the contents are used as address. Hence forth the
brackets will be used to specify the contents used as address.
⦁ MOV M, R
⦁ This instruction will copy the data from the register to memory M.
⦁ The HL register pair is used as the memory pointer. The contents of the
specified register are copied to that memory location pointed by the HL
register pair.
⦁ The specified register may be any general purpose register A, B, C, D, E,
H or L.
⦁ The contents of the specified register remains unchanged.
Mnemonic MOV M, R
Operation M=R or (HL)=R
No of bytes 1 byte.
Machine Cycles 2 (0F + MR)
T-states 4+3=7
Example:
MOV M,C
⦁ Let the contents of HL pair are E200 H, register C = 20H, at address E200 : 10H
is stored. On the instruction MOV M, C the data is transferred from C register to
memory.
⦁ The contents of the register C are copied to memory location E200 H, so contents
of memory location C200 H will change from 10H to 20H.
⦁ MVI R, Data
⦁ This instruction moves the 8 bit immediate data to the specified register. The
data is specified within the instruction.
13
⦁ It is a two byte instruction, so first byte of instruction will be OPCODE and
second byte will be 8 bit data.
⦁ The register R may be any general purpose register like A, B, C, D, E, H or L.
Example
MVI D, 07H
⦁ This instruction will load the immediate data 07 H in register D
⦁ Let the contents of register D= 10H. Then after the execution of instruction MVI
D, 07H the contents of register D will be changed from 10H to 07H.
⦁ MVI M, data
⦁ This instruction moves immediate data to memory.
⦁ The HL register pair is used as memory pointer. The contents of HL register
pair are used memory address and the immediate data is transferred to that
memory location.
Example
⦁ MVI H, 10H
⦁ MVI L, 00H
⦁ MVI M, 20H
14
⦁ LXI Rp 16-bit Data
⦁ This instruction will load register pair with 16-bit data.
⦁ This instruction loads 16-bit data specified within the instruction to the
specified register pair or stack pointer.
⦁ In the instruction only high order register is specified or register pair. i.e. if
HL pair is to be loaded only H regıster will be specified in the instruction.
⦁ The register pair Rp, can be BC, DE, HL register pairs or stack pointer SP.
Example
i) LXI H, 2030 H.
⦁ Load HL pair with 2030 H. 20H will be loaded in the H register and 30F in the L
register.
ii) LXI SP, 7FFFH.
⦁ This instruction will load stack pointer SP with 7FFFH
⦁ LDA Address
Load accumulator direct from memory
⦁ This instruction copies the contents of the memory location whose address is
specified in the instruction to the accumulator.
⦁ The contents of the memory location remain unchanged.
Example:
LDA 5820H
⦁ This instruction will load the accumulator with the contents of memory location
5820H.
⦁ Let initially A = FO H, contents of memory location 5820H = 15H.
⦁ Then after the execution of instruction LDA 5820H, the accumulator will be
loaded with 15H. The contents of accumulator will change from FOH to 15H.
8. STA Address
15
Store accumulator direct to memory:
⦁ This instruction will store the contents of the accumulator to the memory location
specified in the instruction.
⦁ The contents of the Accumulator remain unchanged.
⦁ It is a 3 byte instruction. The first byte is the opcode, second byte is lower order
address and the third byte is higher order address.
Example:
STA 5820H
⦁ This instruction will store the contents of accumulator at memory location 5820H.
9.LHLD Address
Load HL pair directly from memory:
⦁ This instruction loads the contents of the memory location to the H and L
registers. The address of memory is specified along with the instruction.
⦁ The contents of memory location whose address is specified in the instruction are
transferred to L register and the contents of the next memory location i.e. (address
+ 1) to the H register.
⦁ This instruction is used to load the H and L registers from memory.
⦁ It is a 3 byte instruction. The first byte is the opcode, second byte is the lower
order address and the third byte is the higher order address.
Example
LHLD 4000 H.
⦁ Load HL pair from memory locations 4000H and 4001H.
⦁ Let H = 05 H, L= 04 H, at memory locations 4000 H and 4001 H the data 20H,
30H is stored.
⦁ The instruction LHLD Will load the contents of memory location 4000H to the L
16
register, and the contents of memory location 4001H to the H register.
⦁ So the contents of register L will change from 04 to 20 H and contents of register
H will change from 05 H to 30 H.
Example
SHLD 4000H.
⦁ Store HL pair to memory locations 4000 and 4001.
⦁ Let H = 05H, L = 04H, at memory locations 4000H and 4001FH the data 20H and
30H is stored and the instruction SHLD 4000 H 15 executed the contents of
register L are copied to memory location 4000 H and contents of register H are
copied memory location 4001 H.
11. LDAX Rp
Load accumulator indirect by using a memory pointer.
⦁ This instruction copies the contents of the memory location to the accumulator.
⦁ The address of memory location is given by Rp, register pair specified along with
the instruction.
⦁ The register pair Rp, can be BC or DE only.
⦁ The contents of the memory location remain unchanged.
Mnemonic LDAX Rp
Operation A=(Rp)
No of bytes 1 byte.
Machine Cycles 2 (OF + MR)
17
T-states 7 (4 + 3)
Example:
LDAX B
⦁ This instruction will load accumulator with the contents of memory location
whose address is given by the BC register pair.
⦁ Let A = 1F H, B = 20H, C = 25H, at memory location 2025: 56H is stored.
Then after the execution of instruction LDAX B, the accumulator will be
loaded with the contents of memory location 2025 i.e. 56 H.
12. STAX Rp
Store accumulator indirect by using a memory pointer.
⦁ This instruction copies the contents of accumulator to memory location.
⦁ The address of the memory location is given by the Rp, register pair specified
in the instruction.
⦁ The register pair Rp, can be a valid register pair like BC or DE only.
⦁ The contents of accumulator remain unchanged.
Mnemonic STAX Rp
Operation (Rp)=A
No of bytes 1 byte.
Machine Cycles 2 (OF + MW)
T-states 7 (4 + 3)
Example:
STAX B
⦁ This instruction will store the contents of accumulator to the memory location,
whose address is given by the BC register pair.
⦁ Let A = 1F H, B= 26H, C = 08H, at memory location 2608: 10 is stored.
⦁ Then after the execution of STAX B instruction, the memory location 2608
will contain 1FH.
13. XCHG
Exchange the contents of HL with DE pair.
⦁ This instruction exchanges the contents of H register with D register and L
register with E register.
Mnemonic XCHG
Operation HD
No of bytes 1 byte.
18
Machine Cycles 1 (OF)
T-states 1(4)
Example:
XCHG
⦁ Let H = 12H, L = 11H, D = 30H, E=40H and the instruction XCHA is executed.
ADD R
19
⦁ This instruction adds the contents of register R and accumulator. The
result is stored in the accumulator.
⦁ The register R can be any general-purpose register like A, B, C, D, E,
H, or L.
⦁ In addition to the result in the accumulator, all the flags are modified to
reflect the result of the operation.
Mnemonics ADD R
Operation A=A+
R
T-states 4
Example
ADD C
Let A = 47 H, C = 51 H and instruction ADD C is executed.
ADD M
Add memory location contents to the accumulator.
⦁ This instruction uses the HL register pair as a memory pointer. The
contents of memory location addressed by the HL pair are added to
the contents of the accumulator. The result is stored in the
accumulator.
⦁ To reflect the status of the result all the flags are modified.
20
⦁ The contents of the memory location remain unchanged.
Mnemonics ADD M
Operation A = A + M or A = A + (HL)
No. of Byte 1 byte
Machine Cycle 2 (OF + MR)
T-states 4+3=7
Example
ADD M
⦁ SUB
Any 8 bit data or the contents of a register or contents of a memory location
can be subtracted from the contents of the accumulator.
SUB R
Subtract register from the accumulator
⦁ This instruction subtracts the contents of the specified register from the
contents of the accumulator and the result is stored in the accumulator.
⦁ The contents of the registers R are not altered.
⦁ The register R can be any general-purpose register like A, B, C, D, E, H, or
L.
21
Note:
The subtraction is performed by using 2's complement method. 2's
complement is done by the microprocessor itself. The programmer only
specifies the two data bytes.
Mnemonics SUB R
Operation A=A–R
No. of Byte 1 byte
Machine Cycle 1 (OF)
T-states 4
Example
SUB B
Let,
A = 37H,
B = 40H
A = 0011 0111
B = 0100 0000
2’s complement of B = 1100 0000
SUB M
Subtract data in memory from accumulator
⦁ This instruction will subtracts the contents of memory location pointed by
HL register pair from the contents of the accumulator . The result is stored
in the accumulator.
⦁ The contents of the registers R are not altered. The HL register pair is used
as a memory pointer.
⦁ The subtraction is done in the same way as the SUB R instruction.
⦁ All the flags are modified to reflect the result of operation
22
Mnemonics SUB M
A=A–M
Operation Or
A=A-(HL)
No. of Byte 1 byte
Machine Cycle 1 (OF+MR)
T-states 7(4+3)
Example
SUB M
A = A-(HL)
Let A=50H, H=C2 H, L=00H at memory location C200 H:20H is stored and
instruction SUB M is executed.
⦁ INR
INR R
Increment specified register
⦁ This instruction increments the contents of specified register by 1 and
result is stored in the same register.
⦁ The register R can be any general purpose register like A, B, C, D, E, H or
L.
⦁ All the flags except the carry flag are modified.
23
Mnemonics INR R
Operation R=R+1
No. of Byte 1 byte
Machine Cycle 1 (OF)
T-states 4
Example
INR E
E--> E+1
INR M
Increment contents of memory location contents by one.
⦁ This instruction increments the contents of memory location addressed by
HL register pair by 1 and result is stored back at the same memory
location.
⦁ All other flags except carry are affected.
Mnemonics INR M
(HL)=(HL)+1
Operation Or
(M)=(M)+1
No. of Byte 1 byte
Machine Cycle 1 (OF+MR+MW)
T-states 10(4+3+3)
Example
INR M
Let H=20H,L = 02 H, at memory location 2002 : 05 is stored, flag
INR M register = 00x1x0x1 and the instruction INR M is executed.
24
⦁ DCR
The contents of the designated register or memory are decremented by 1 and
their result is stored at the same place.
DCR R
Decrement Specified register contents by one
⦁ This instruction decrements register contents by 1 and result is stored in
the same register.
⦁ Register can be any general purpose register like A,B,C,D,H or L.
Mnemonics DCR R
Operation R=R-1
No. of Byte 1 byte
Machine Cycle 1 (of)
T-states 4
Example:
DCR B
B=B-1
Let B=2f , flag register =01x0x0x1 and the instruction DCR B is executed
DCR M
Decrement data in memory
⦁ This instruction decrements the contents of memory location addressed by
HL register pair by 1. The result is stored back at the
⦁ same memory location. The HL register pair acts as the memory pointer.
Mnemonics DCR M
M=M-1 or
Operation
(HL)=(HL)-1
No. of Byte 1 byte
Machine Cycle 3(of+mr+mw)
T-states 10(4+3+3)
Example:
25
DCR M
Let H=20H,L=02H, at memory location 2002 : 3E data is stored, flag register = 01x 0x
0x1 and instruction DCR M is executed.
⦁ AND
⦁ OR
⦁ XOR
⦁ CMP
CMP R
Compare register with accumulator.
⦁ This instruction compares the contents of the accumulator with the content of
the register specified.
⦁ The operation of comparing is performed by subtracting the register contents
from the accumulator contents.
⦁ The contents of the register or accumulator are not altered.
Mnemonic CMP R
Operation A compare R
26
No. of Bytes 1 byte
Machine cycles 1 (OF)
T-states 4
Example:
⦁ CMP B
⦁ CMP C
⦁ CMP D
⦁ Let A = 20H, B = 10H. Now if the instruction CMP B is executed. Then the status
of flags will be CY = 0 and Z = 0 to indicate A > B.
⦁ Let A = 10H, C = 10H. Now if the instruction CMP C is executed. Then the status
of flags will be CY = 0 and Z = 1 to indicate A = B.
⦁ Let A = 10H, D = 20H. Now if the instruction CMP D is executed. Then the status
of flags will be CY = 1 and Z = 0 to indicate A < B.
CMP M:
Compare memory with accumulator.
⦁ This instruction compares the contents of the accumulator and memory location
contents. The HL register pair gives the address of the memory location.
⦁ The contents of the accumulator and memory location are not altered.
Mnemonic CMP M
Operation (A – M)
No. of Bytes 1 byte
27
Machine cycles 2 (OF + MR)
T-states 7 ( 4+3)
The S, P, and AC flags are modified to reflect the status of subtraction, or Z, and CY
are used to indicate the result of the comparison.
Example
CMP M
Let A = 20H, H = C0H, L = 02H at memory location C002: 10H is stored, and the
instruction CMP M is executed.
The flag status will be as follows:
CY = 0, Z = 0, P = 0 S = 0, AC = 0
⦁ RLC
⦁ Rotate Accumulator left.
⦁ This instruction will rotate the content of the accumulator to the left by 1
bit. i.e. it shifts the bits left by one position. B0 will be transferred to B1,
b1 to B2, and so on B6 to b7, B7 to B0 as well as the carry flag.
Mnemonic RLC
For n = 0 to 6, Bn+1 = Bn
Operation
B0 = CY = B7
No. of Bytes 1 byte
Machine cycles 1 (OF )
T-states 4
Example:
28
Let A = 1F H and instruction RLC is executed. the content of the accumulator will be
rotated by 1 bit to the left and the result will be stored in the accumulator
i.e. A = 3E H
⦁ RRC
Rotate Accumulator Right.
⦁ This instruction rotates the contents of the accumulator to the right by one
position.
Mnemonic RRC
For n = 0 to 6, Bn = Bn+1
Operation
B7 = CY = B0
No. of Bytes 1 byte
Machine cycles 1 (OF )
T-states 4
Example
A = 1CH and
CY = 1
After the execution of the instruction, the accumulator contents will be 0E H and the
29
carry flag will be reset.
⦁ RAL
⦁ Rotate Accumulator left through Carry.
⦁ This instruction will rotate the content of the accumulator to the left by 1-
bit position along with carry. B7 is placed in CY and CY is placed in bit
B0.
Mnemonic RAL
For n = 0 to 6, Bn+1 = Bn
Operation CY = B7
B0 = CY
No. of Bytes 1 byte
Machine cycles 1 (OF )
T-states 4
Example:
Let A = 0EH, CY =1. After execution of the instruction, the contents of the accumulator
will be 1DH ad CY = 0.
30
⦁ RAR
⦁ Rotate Accumulator Right through Carry.
⦁ This instruction will rotate the content of the accumulator right by 1-bit position
along with carry. B0 is placed in CY and CY is placed in bit B7.
Mnemonic RAR
For n = 0 to 6, Bn = Bn+1
Operation B7 = CY
CY = B0
No. of Bytes 1 byte
Machine
1 (OF )
cycles
T-states 4
Example:
Let A = 0EH and CY = 1 and the instruction RAR is executed. After execution of the
instruction accumulator contents will be (1000 0111) 87H and the carry flag will be reset.
31
Branching:- JMP, JNZ, JZ, JNC, JC, CALL
The Branch Instructions are the most powerful instructions because they allow the
microprocessor to change the sequence of a program, either unconditionally or under
certain test conditions.
These are classified into 3 categories:
⦁ JUMP instructions
⦁ CALL and RETURN instructions
⦁ RESTART instructions.
JMP Instruction:
The jump instruction transfers the program sequence to the memory address given
in the operand based on the specified flag. Jump instructions are 2 types:
⦁ Unconditional Jump Instructions and
⦁ Conditional Jump Instructions.
32
⦁ The conditional jump instruction checks the flag conditions and makes decisions
to change or not to change the sequence of a program.
⦁ Out of five flags, 4 (CY, Z, S, and P) flags are used by conditional JMP, but the
AC flag is not used.
CALL Instruction:
⦁ The call instruction transfers the program sequence to the memory address given
in the operand. It is used in the main program to call a subroutine.
⦁ When a subroutine is called, the contents of PC, which is the address of the
instruction following the CALL instruction, is stored on the stack and the program
execution is transferred to the subroutine address. That is before transferring, the
address of the next instruction after CALL is pushed onto the stack.
33
Unconditional CALL Instructions:
⦁ It transfers the program sequence to the memory address given in the operand.
⦁ When it is executed, the microprocessor will store the address of the next
instruction in STACK and the PC is located with a subroutine address.
⦁ 3-byte instruction
⦁ Immediate/Register indirect mode
⦁ 5 MC
⦁ 18 T-States
⦁ No flags are affected
Example:
CALL 2050H
Conditional CALL Instructions:
⦁ In this case, the program is transferred to the subroutine if the condition is met .
⦁ If the condition is not met then the main program is continued
⦁ In this instruction, all flags are used except the AC flag.
34
CPE 16-bit Call subroutine if parity flag is set ( P=1, even parity)
That is Calls if parity flag is 1
Example: CPE 2050
CPO 16-bit Call subroutine if parity flag is reset ( P=0, odd parity)
That is Calls if parity flag is 0
Example :CPO 2050
⦁ An 8-bit stack pointer is used to hold the address of the most recent stack entry.
This location which has the most recent entry is called as the top of the stack.
⦁ When the information is written on the stack, the operation is called PUSH. When
the information is read from the stack, the operation is called POP.
⦁ The stack works on the principle of Last in First Out or First in Last Out.
⦁ Above figure shows Stack plates. If we want to remove the first stack 4 plate,
then we will have to remove all the plates 3 above the first i.e. we have to remove
the fourth plate, third plate, second plate and then finally 2 the first plate. This
indicates that the first plate pushed onto the stack is the last one to be popped
from the stack. This operation is called as First In Last Out (FILO).
⦁ The stack is implemented with the help of special memory pointer register called
as the stack pointer. It is of 8 bit. The stack pointer's contents are automatically
adjusted to point to the stack. The memory location that is currently pointed by the
stack pointer is called as top of stack. As shown in Fig. above.
35
⦁ When data is to be stored on the stack, the SP increments before storing the data
on stack and when the data is to be retrieved/popped from the stack the SP
decrements to point next available byte of stored data.
Hence in short,
(a) Stack area is usually defined in RAM OLY.
(b) Stack is used to store information (data) temporarily.
(c) Stack is a part of memory defined in program by THE USER.
(b) STACK, the name itself, informs about the way data is stored i.e. stack of data
PUSH Rp
Push the contents of register pair on to the stack.
⦁ This instruction is used to write 16 bit data on the stack.
⦁ When this instruction is executed the contents of the specified register pair are
copied on the stack in the following sequence.
⦁ The stack pointer is decremented by one and the contents of higher order
register of the specified register pair are copied to the memory location
pointed the stack pointer.
⦁ The stack pointer is again decremented by 1 and the contents of lower
order of register pair are copied to memory location pointed by the stack
pointer.
⦁ The register pair can be any 16 bit register pair like BC, DE, HL or PSW (program
status word).
⦁ The PSW is program status word with A register as higher order register and flog
register as low order register.
⦁ Only higher order register is to be specified within the instruction.
Mnemonics PUSH Rp
SP = SP — 1
SP = Higher order Rp
Operation
SP = SP — 1
SP = Lower order Rp
No. of Byte 1 byte
Machine Cycle 3 (OF + MW + MW)
T-states 12 (6 + 3 + 3)
Example: PUSH B
Let BC = 3010H, and the stack pointer is initialized at FFFFH. Now if the
instruction PUSH B is executed, then initially the stock Pointer will decrement by 1. i.e.
36
(SP =FFFD). The contents of register B (30 H) will be copied to the memory location
FFFE H. The stack pointer is then again decremented by one (SP = FFFD H). The
contents of register C (10 H) will be copied to this memory location.
POP Rp
Mnemonics POP Rp
Operation Lower order Rp = (SP)
SP = SP + 1
Higher order Rp = (SP)
SP = SP + 1
No. of Byte 1 byte
Machine Cycle 3 (OF + MR + MR)
T-states 10 (4+ 3 + 3)
Example: POP B
Let BC = 3010 H, stack pointer = FFFD H at memory location FFFD H : 10 H is
stored and at memory location FFFE H : 30 H is stored and the instruction POP B is
executed. The contents of stack location FFFDH are popped off to the C register. The
stack pointer will increment by one. The contents of this location i.e. FFFEH are popped
off the B register. Then the stack pointer will again increment by 1 (i.e. SP = FFFF H).
37
8085 programs
Related to…..
Data transfer
Program 1: Write a program to load data 05H into the accumulator and data 20 H into
the register B.
Code:
MVI A,05H
MVI B,20H
HLT
Explanation:
How program looks in program memory….now assume C000H is the starting memory
location
38
Program 2: Write a program to exchange the contents of memory location D000H and
D001H .Let the contents of memory location D000H be 10 H and Contents of memory
location D001H be 20H.
Code 1:
LDA D000H
MOV B,A
LDA D001H
STA D000H
MOV A,B
STA D001H
HLT
Explanation:
Code2:
LXI H, D000H
LXI D,D001H
MOV B,M
LDAX D
MOV M,A
MOV A,B
STAX D
HLT
Explanation:
40
MOV A,B Copy the contents of register B A=10H
into accumulator
STAX D Store the contents of D001H=10H
accumulator into memory Result
location D00!H
HLT Terminate stop
Solution:
D000H=20H
D001H=23H
D002H=20H+23H
=43H
Code:
LXI H,D000H
MOV A,M
INX H
ADD M
INX H
MOV M,A
HLT
Explanation:
Instruction Comment Operation
41
LXI H,D000H Set HL as memory pointer at H=D0,L=00
memory location D000H D000:20H
MOV A,M Load First operand in A=20H
accumulator
INX H Increment HL to point next H=D0, L=01
memory location i.e. D001H D001:23H
ADD M Add second operand to first A=A+M
operand A=20H+23H
A=43H
INX H Increment HL to point next H=D0, L=02
memory location i.e. D002H
MOV M,A Store the result D002:43H
HLT Terminate the program STOP
execution
Program 5:
Subtract the contents of memory location D001H from memory location D000H and
place the result in memory location D002H. Let the contents of memory location D000H
be 50 H and Contents of memory location D001H be 20H.
Solution:
D000H=50H
D001H=20H
D002H=50H-20H
=30H
Code:
LXI H,D000H
MOV A,M
INX H
SUB M
INX H
MOV M,A
HLT
Explanation:
42
Instruction Comment Operation
LXI H,D000H Set HL as memory pointer at H=D0,L=00
memory location D000H D000:50H
MOV A,M Load First operand in A=50H
accumulator
INX H Increment HL to point next H=D0, L=01
memory location i.e. D001H D001:20H
SUB M subtract second operand from A=A-M
first operand A=50H-20H
A=30H
INX H Increment HL to point next H=D0, L=02
memory location i.e. D002H
MOV M,A Store the result D002:30H
HLT Terminate the program STOP
execution
Solution:
We have two 16bit number. Let the two number be 1234H and 4321H.
D000H=34H
D001H=12H
D002=21H
D003=43H
RESULT=1234H+4321H
=5555H
D004=55H
D005=55H
Method 1:Here we will add Two LSBs and two MSBs separately and store the results
43
Code :
LHLD D000H
XCHG
LHLD D002H
MOV A,E
ADD L
MOV L,A
MOV A,D
ADC H
MOV H,A
SHLD D004H
HLT
Explanation:
44
Method 2:
Here we will store two numbers in the register pair DE and HL. Then using DAD
instruction we will add the two 16 bit numbers. The result of addition will be stored in
HL register pair.
Code :
LHLD D000H
XCHG
LHLD D002H
DAD D
SHLD D004H
HLT
Explanation:
Program 7:
Subtract the 16 bit number in memory location D002H and D003H from the 16 bit
number in memory location D000H and D001H. The most significant eight bits of the
two number are in memory locations D001H and D003H. Store the result in memory
location D004 and D005H with the most significant bit in memory location D005H.
Solution:
45
D000H=34H
D001H=12H
D002H=00H
D003H=01H
Result=1234H-0100H=1134
D004H=34H
D005H=11H
Code:
LHLD D000H
XCHG
LHLD D002H
MOV A,E
SUB L
MOV L,A
MOV A,D
SBB H
MOV H,A
SHLD D004H
HLT
Explanation:
Instruction Comment Operation
LHLD D000H Load the first 16 bit number in H=12H,L=34H
HL register pair
XCHG Save the 16 bit number in DE D=12H, E=34H
register pair
LHLD D002H Load the second 16 bit number H=01H, L=00H
in HL register pair
MOV A,E Load the lower byte of the first A=34H
number in accumulator
SUB L Subtract lower byte of second A=A-L=34-00=34
number CY=0
MOV L,A Store the result in L register L=34H
46
SHLD D004H Store the 16 bit result in D004H=34H
memory location D004 and D005H=11H
D005H
HLT Terminate the program STOP
execution
Solution:
Let A=AAH
i.e. A=1010 1010
Code:
LDA C200H
CMA
STA C300H
HLT
Explanation:
47
Solution:
Let A=44H
i.e. A=0100 0100 => 44H
A=1011 1011
+ 1
--------------------------
1011 1100
A=1011 1100=>BCH
Code:
LDA C200H
CMA
ADI 01H
STA C300H
HLT
Code:
LXI H,4150
MOV B,M
MVI A,00
MVI D,00
48
INX H
MOV C,M
LOOP2: ADD B
JNC loop
INR D
loop: DCR C
JNZ LOOP2
STA 4152
MOV A, D
STA 4153
HLT
Code:
LXI H, 4150H
MOV B,M
MVI C,00
INX H
MOV A,M
LOOP2: CMP B
JC LOOP
SUB B
INR C
JMP LOOP2
LOOP: STA 4152
MOV A,C
STA 4153
49
HLT
Explanation:
Program 12: Write a program to transfer a block of data stored from memory location
2050H -2060H to the new memory location starting from the address 2070H.
CODE:
LXI H,2050H
LXI D,2070H
MVI B,0A
STAX D
INX H
50
INX D
DCR B
JNZ LOOP
HLT
Explanation:
Program 13: Write a program in assembly language to find largest number in array
LXI H, 4200H
MOV A, M
INX H
MOV B,M
DCR B
LOOP: INX H
CMP M
JNC SKIP
MOV A, M
SKIP: DCR B
JNZ LOOP
STA 4300H
51
HLT
Explanation:
JNZ LOOP Jump to the label LOOP if the B register is not zero
STA 4300H Store the largest value found in the A register at memory
location 4300H
Program 14: Write a program in assembly language to find smallest number in array
LXI H, 4200H
MOV A, M
INX H
MOV B, M
DCR B
LOOP: INX H
CMP M
JC SKIP
MOV A, M ;
SKIP: DCR B
52
JNZ LOOP
STA 4300H
HLT
Explanation:
JNZ LOOP Jump to the label LOOP if the B register is not zero
STA 4300H Store the smallest value found in the A register at memory
location 4300H
LXI H,5000
MOV C,M
DCR C
OUTERLOOP: MOV D,C
LXI H,5001
INNERLOOP: MOV A,M
INX H
CMP M
JC SKIP
MOV B,M
MOV M,A
53
DCX H
MOV M,B
INX H
SKIP: DCR D
JNZ INNERLOOP
DCR C
JNZ OUTERLOOP
HLT
54
MOV M,A
DCX H
MOV M,B
INX H
SKIP: DCR D
JNZ INNERLOOP
DCR C
JNZ OUTERLOOP
HLT
Code:
Instruction Comment Operation
LXI H,5000 Set pointer for array
MOV C,M Load the Count
DCR C Decrement Count
OUTERLOOP: MOV D,C
LXI H,5001 Load starting address of data array
INNERLOOP: MOV A,M copy content of memory location to
Accumulator
INX H
CMP M
JNC SKIP Jump to skip if carry not generated
MOV B,M copy content of memory location to B -
Register
MOV M,A copy content of A - Register to memory
location
DCX H Decrement content of HL pair of registers
MOV M,B
INX H Increment content of HL pair of registers
SKIP: DCR D
JNZ INNERLOOP Jump to LOOP if not Zero
DCR C Decrement Count
JNZ OUTERLOOP Jump to REPEAT if not Zero
HLT Terminate Program
Program-17:Write a program in assembly language to generate first 10 odd numbers.
Code:
MVI A,01
MVI B,02
MVI C,0A
LXI H,4200H
LOOP:ADD B
MOV M,A
INX H
DCR C
55
JNZ LOOP
HLT
Code:
MVI A,00H
MVI B,02H
MVI C,0A
LXI H,4200H
LOOP:ADD B
MOV M,A
INX H
DCR C
JNZ LOOP
HLT
Program -19: Write a program in assembly language to calculates the sum of the first 6
elements of an array located at memory address 2050H and stores the sum in register B.
Code:
LXI H, 2050H
MVI C, 06H
MVI B, 00H
MVI D, 00H
L2: MOV A, M
ADD B
MOV B, A
JNC L1
56
INR D
L1: INX H
DCR C
JNZ L2
HLT
LXI H, 2050H Load immediate data 2050H into register pair H-L
MVI C, 06H Move immediate data 06H into register C
MVI B, 00H Move immediate data 00H into register B
MVI D, 00H Move immediate data 00H into register D
L2: MOV A, M Move data from memory location pointed to by H-L
into register A
ADD B Add data in register B to A
MOV B, A Move data from A to B
JNC L1 Jump to label L1 if no carry (no overflow) after addition
INR D Increment the value in register D
L1: INX H Increment the value of H-L register pair
DCR C Decrement the value in register C
JNZ L2 Jump to label L2 if Z flag is not set (C is not zero)
HLT Halt the program
Program-19: Six bytes are stored in memory location starting at 2050H. Add all the data
bytes, save any carry generated while adding the data bytes. Display the entire sum at two
output ports and store total carry in 2070H and sum in 2071H.
Code:
LXI H, 2050H
MVI C, 06H
MVI B, 00H
MVI D, 00H
L2: MOV A, M
57
ADD B
MOV B, A
JNC L1
INR D
L1: INX H
DCR C
JNZ L2
HLT
Explanation:
LXI H, 2050H Load immediate data 2050H into register pair H-L
MVI C, 06H Move immediate data 06H into register C
MVI B, 00H Move immediate data 00H into register B
MVI D, 00H Move immediate data 00H into register D
L2: MOV A, M Move data from memory location pointed to by H-L
into register A
ADD B Add data in register B to A
MOV B, A Move data from A to B
JNC L1 Jump to label L1 if no carry (no overflow) after addition
INR D Increment the value in register D
L1: INX H Increment the value of H-L register pair
DCR C Decrement the value in register C
JNZ L2 Jump to label L2 if Z flag is not set (C is not zero)
HLT Halt the program
58
⦁ And 100’s digit into 8203H
⦁ B register for is used for 100’s and C register is used for 10’s
Code:
LDA 8200
MVI B,00H
MOV C,B
X: CPI 64H
JC LOOP1
SUI 64H
INR B
JMP X
LOOP1: CPI 0A
JC LOOP2
SUI 0A
INR C
JMP LOOP1
LOOP2: STA 8201H
MOV A,C
STA 8202H
MOV A,B
STA 8203H
HLT
Explanation:
Code:
LDA 8200H
MOV B,A
ANI 0FH
MOV D,A
MOV A,B
ANI F0
RLC
RLC
RLC
RLC
MOV C,A
MVI E,OAH
MVI A,00H
L1: ADD C
DCR E
INZ L1
ADD D
STA 820111
HLT
Explanation:
60
RLC We need 07 therefore RLC
instructions are used to get 07
RLC
RLC
RLC
MOV C,A C copied with data in Acc
MVI E,OAH E copied with OAH(10)
MVI A,00H A copied with 00
L1: ADD C ADD c with content in Acc
DCR E decrement E
INZ L1 Jump to L1 if non zero
ADD D ADD d with content in Acc
STA 820111 Store data in Acc in 8201H
HLT ends programm
Note:-Updating…….
----------------------------------------------End Of Unit-4-------------------------------------------
61
Simple Sequence Programs, Branching, Looping
Array(Sorting) and Table Processing
Decimal to BCD Conversion
Assignment:
62