You are on page 1of 56

Introduction to Assembly

Language Programming

05/20/23 1
High Level Language

Compiler

Assembly Language

Assembler

Machine Code

Microprocessor Hardware

05/20/23 2
8085A Instruction Set
 Data Transfer Instruction
 Move data between registers or between memory locations and
registers. Includes moves, loads, stores and exchanges.

Arithmetic Instruction
 Adds, Subtracts, Increments, Decrements data in registers or
memory.

Logic Instruction
 ANDs, ORs, XORs, compares, rotates or complements data in
registers or between memory and registers.

05/20/23 3
Branch/Jump Instruction
 Initiates conditional or unconditional jumps, calls, returns and
restart.

Stack, I/O and Machine Control Instruction


 Includes instructions for maintaining stack, reading from
input port, writing to output port, setting and reading
interrupt mask and clearing flags.

05/20/23 4
Programming Model
8 bit 8 bit
0000H 00H
8 bit 0001H 01H
0002H 02H
A FLAG
0003H 03H
B C
0004H 04H
D E
0005H 05H
H L 0006H 06H
SP
PC
FFFDH FDH
CPU FFFEH FEH
FFFFH FFH
05/20/23 5
MEMORY I/O
Data Transfer
IMMEDIATE DATA TRANSFER
• MVI reg , data8 ;data8  (reg)
• LXI rp ,data16;data16  (rp)

REGISTER DATA TRANSFER


• MOV reg1 , reg2 ;(reg2)  (reg1)

Reg (Register) : A,B,C,D,E,H,L


Rp (Register Pair) : BC,DE,HL & SP

05/20/23 6
Example
MVI A ,10 ;A=0AH
MVI B ,10010001B ;B=91H
MVI D ,7FH ;D=7FH

LXI B ,3 ;B=00H , C=03H


LXI H ,2345H ;H=23H , L=45H
LXI D ,100 ;D=00H , E=64H
LXI SP,3FF0H ;SPH=3FH,SPL=F0H

05/20/23 7
Example

MVI B, 55H
MOV A,B
MOV C,A
MOV H,C
MOV L,A
MOV E,L
HLT
05/20/23 8
DIRECT DATA TRANSFER
• LDA address16
• STA address16
• LHLD address16
• SHLD address16

05/20/23 9
Example
0000H
0001H
• LDA 3000H ..
STORE
(3000H)  (A) 2100H x1
A
2101H
x1
2102H
• STA 2100H ..
(A)  (2100H) LOAD ..
3000H y1
A
..
y1
3509H

05/20/23 10
Example
0000H
0001H
LHLD 8000H
STORE..
(8000H)  (L) L x1 3500H x1
(8000H + 1)  (H) H x2 3501H x2
3502H
..
SHLD 3500H LOAD..
(L)  (3500H) L y1 8000H y1
(H)  (3500H + 1) H y2 8001H y2
3509H

05/20/23 11
INDIRECT DATA TRANSFER
• LDAX B ;pointer is BC register
• LDAX D ;pointer is DE register
• STAX B ;pointer is BC register
• STAX D ;pointer is DE register
• MOV reg , M ;pointer is HL register
• MOV M , reg ;pointer is HL register
• MVI M , data8 ;pointer is HL register

05/20/23 12
Example 0000H
LXI B , 2020H 0001H
MVI A , 88H 0002H
STAX B 0003H
INX B 0004H
LDAX B 0005H
LXI H , 3000H 0006H
MOV D,M
MOV M,A 2020H 88H
B C AAH
2021H
A 88H 20H 20H
D H L 3000H FFH
30H 00H
05/20/23 13
• Instruction INX – increment 0000H
Register pair 0001H
0002H
• BC = 2021H
0003H
0004H
0005H
0006H

2020H 88H
B C AAH
2021H
A AAH 20H 21H
D FFH H L 3000H AAH
30H 00H
05/20/23 14
• Transfer 10 byte data from memory location 3000h
• To memory location 3500h using LDA & STA
0000H
LDA 3000H 0001H
STA 3500H ..
3000H x1
.
3001H x2
. .. ..
LDA 3009H 3009H x10
STA 3509H
..
3500H x1
.. ..
3509H x10
05/20/23 15
• Transfer 10 byte data from memory location 3000h
• To memory location 3500h
0000H
MVI H,10 0001H
LXI B , 3000H ..
LXI D , 3500H 3000H x1
LOOP: LDAX B
STAX D 3001H x2
INX B .. ..
INX D 3009H x10
DCR H ..
JNZ LOOP
3500H x1
HLT
.. ..
3509H x10
05/20/23 16
Arithmetic Operation

• ALU
• FLAG
• CPU REGISTER
05/20/23 17
Arithmetic Instruction
• ADDITION

• ADI data8 (A) + data8  (A)


• ADD reg (A) + (reg)  (A)
• ACI data8 (A) + data8 + CY  (A)
• ADC reg (A) + (reg) + CY  (A)
• DAD rp (HL) + (rp)  (HL)
05/20/23 18
EXAMPLE

• ADI 99H ; A contains 88 (H)


• register A 10001000 136 decimal constant 1 0
011001 153 decimal _____________ _____ register A
100100001 289 decimal

• S=0 Bit D7 = 0 after addition


• Z=0 The accumulator contains other than zero after addition
• AC = 1 There is a carry out of bit D3 to bit D4 during addition
• P=1 The accumulator contains an even number of ‘1’s after
addition
• CY = 1 There is an overflow as a result of the addition

05/20/23 19
EXAMPLE

• ADC B ; A contains 88 (H) B contains 99 (H)


; CY =1
CY 1
register A 10001000
register B 10011001
_____________
register A 100100010

• Flag : S = 0 , Z = 0, AC = 1, P = 1,CY = 1

05/20/23 20
SUBTRACTION

• SUI data8(A) - data8  (A)


• SUB reg (A) - (reg)  (A)

• SBI data8 (A) - data8 - CY  (A)


• SBB reg (A) - (reg) - CY  (A)

05/20/23 21
INCREMENT/DECREMENT

• INR reg (reg) + 1  (reg)


• DCR reg (reg) - 1  (reg)

• INX rp (rp) + 1  (rp)


• DCX rp (rp) - 1  (rp)
Note : No Flag Effected for INX & DCX

05/20/23 22
Logic Instruction
AND

AND Immediate With Accumulator


• ANI data8 (A) Λ Data8  (A)

AND Register/Memory With Accumulator


• ANA reg (A) Λ (Reg)  (A)
05/20/23 23
OR

OR Immediate With Accumulator


• ORI data8 (A) V Data8  (A)

OR Register/Memory With Accumulator


• ORA reg (A) V (Reg)  (A)

05/20/23 24
EXCLUSIVE-OR

EX-OR Immediate With Accumulator


• XRI data8 (A) ⊕ Data8  (A)

EX-OR Register/Memory With Accumulator


• XRA reg (A) ⊕ (Reg)  (A)

05/20/23 25
COMPLEMENT THE ACCUMULATOR

• CMA A (A)
( )
A

COMPLEMENT THE CARRY STATUS

• CMC ( )CY
 (CY)

05/20/23 26
COMPARE
Compare Accumulator With Immediate
Data
• CPI data8 (A) – data8
A < data8/mem  CY=1
A = data8/mem  Z=1
A > data8/mem  CY=0, Z=0
Compare Accumulator With Register/Memory

• CMP reg (A) – (reg)

Note: Only flag affected


05/20/23 27
Rotate

Rotate Accumulator Right Through Carry


• RAR (A0)  (CY)
(An+1)  (An)
(CY)  (A7)
CY A7 A6 A5 A4 A3 A2 A1 A0

05/20/23 28
Rotate

Rotate Accumulator Left Through Carry


• RAL (A7)  (CY)
(An)  (An+1)
(CY)  (A0)
CY A7 A6 A5 A4 A3 A2 A1 A0

05/20/23 29
Rotate

Rotate Accumulator Right


• RRC (A0)  (A7)
(An+1)  (An)
(A0)  (CY)
CY A7 A6 A5 A4 A3 A2 A1 A0

05/20/23 30
Rotate

Rotate Accumulator Left


• RLC (A7)  (A0)
(An)  (An+1)
(A7)  (CY)
CY A7 A6 A5 A4 A3 A2 A1 A0

05/20/23 31
Branch Instruction
Unconditional Jump
• JMP address16
(Byte 3) (Byte 2)  (PC)

Conditional Jump
• J Condition address16
If (Condition= true)
(Byte 3) (Byte 2)  (PC)
05/20/23 32
Condition
• JZ Z=1 Jump if Zero flag SET
• JNZ Z=0 Jump if Zero flag NOT SET
• JC CY=1 Jump if Carry flag SET
• JNC CY=0 Jump if Carry flag NOT SET
• JM S=1 Jump if Sign flag SET
• JP S=0 Jump if Sign flag NOT SET
• JPE P=1 Jump if Parity flag SET
• JPO P=0 Jump if Parity flag NOT SET
05/20/23 33
Example 1
Check Zero Flag

MVI B, 255
LOOP:DCR B
JNZ LOOP ;if Z == 0 then goto
;LOOP

05/20/23 34
Example 2 – Find the smallest value between
two number

(A) = x1 ; (B) = x2

LOOP: CMP B ;(A) – (B)


JNC EXIT;if CY == 0 then EXIT
JMP STORE
EXIT: MOV A, B
STORE:STA 2050H
05/20/23 35
Unconditional Call Subroutine
• CALL address16

(PCH)  ((SP) –1)


(PCL)  ((SP) –2)
(SP) – 2  (SP)
(Byte 3)(Byte 2)  (PC)

05/20/23 36
Conditional Call Subroutine
C Condition address16

If (Condition = True)
(PCH)  ((SP) –1)
(PCL)  ((SP) –2)
(SP) – 2  (SP)
(Byte 3)(Byte 2)  (PC)

05/20/23 37
• CZ Z=1 Call if Zero flag SET
• CNZ Z=0 Call if Zero flag NOT SET
• CC CY=1 Call if Carry flag SET
• CNCCY=0 Call if Carry flag NOT SET
• CM S=1 Call if Sign flag SET
• CP S=0 Call if Sign flag NOT SET
• CPE P=1 Call if Parity flag SET
• CPOP=0 Call if Parity flag NOT SET

05/20/23 38
Return From Subroutine
• RET

((SP))  (PCL)
((SP) + 1)  (PCH)
(SP) + 2  (SP)

05/20/23 39
Return From Subroutine (Conditional)
• R Condition
If (Condition = True)
((SP))  (PCL)
((SP) + 1)  (PCH)
(SP) + 2  (SP)

05/20/23 40
• RZ Z=1 Return if Zero flag SET
• RNZ Z=0 Return if Zero flag NOT SET
• RC CY=1 Return if Carry flag SET
• RNCCY=0 Return if Carry flag NOT SET
• RM S=1 Return if Sign flag SET
• RP S=0 Return if Sign flag NOT SET
• RPE P=1 Return if Parity flag SET
• RPOP=0 Return if Parity flag NOT SET

05/20/23 41
Example
ORG 0000H
LXI SP, 3FF0H ;init Stack Pointer
MVI A, 80H
OUT 83H ;Init 8255, all port as output
REPEAT: MVI A,0
OUT 80H
CALL DELAY ;Call subroutine
MVI A,1
OUT 80H
CALL DELAY ;Call subroutine
JMP REPEAT

DELAY: MVI B, 0 ;Subroutine


LOOP: DCR B
JNZ LOOP
RET
END

05/20/23 42
I/O,Stack, Machine Control
Instruction
Stack Operation
Write The Content of Register Pair Onto
The Stack
• PUSH rp
(reg high)  ((SP) –1)
(reg low)  ((SP) –2)
(SP) – 2  (SP)
05/20/23 43
Write The Content of Accumulator & Flag
Status Onto The Stack
• PUSH PSW
(A)  ((SP) –1)
(Flag)  ((SP) –2)
(SP) – 2  (SP)

05/20/23 44
Retreive The Content of Register Pair
From The Stack
• POP rp
((SP))  (reg low)
((SP) + 1)  (reg high)
(SP) + 2  (SP)

05/20/23 45
Retreive The Content of Accumulator &
Flag Status From The Stack
• POP PSW
((SP))  (Flag)
((SP) + 1)  (A)
(SP) + 2  (SP)

05/20/23 46
STACK OPERATION
Lecture 2
(Revision)

05/20/23 47
How the Stack Works
• The stack is a reserved area of memory. It operates as a last-in
first-out bank of registers.
• The memory locations, which constitute the stack, are used to
store binary information temporarily during program execution.
• The stack can be located anywhere in read/write memory, but is
usually defined such that it neither interferes with the program
memory space or the data memory space.
• The start address of the stack is specified at the initialisation
stage of the program by loading the 16-bit CPU register, called
the stack pointer, with the desired address of the start of the
stack.
– e.g LXI SP, data 16
05/20/23 48
How the Stack Works
• Data from CPU register pairs are stored in the stack area of
memory when the processor executes a push rp instruction.
• The contents of the program counter is automatically stored in
the stack area of memory whenever the processor executes a
call or restart (rst n) instruction.
• Data stored in the stack area of memory are returned to
processor register pairs when the processor executes a pop rp
instruction.
• Data is automatically transferred from the stack area of memory
to the program counter whenever the processor executes a
return (ret) instruction.

05/20/23 49
Writing to the Stack
• To execute the instruction push HL
assuming initial sp contents is 2099
H.
• The stack pointer is decremented
by 1 (sp=2098) and the contents of
H are written to this location.
• The stack pointer is decremented
by 1 (sp=2097) and the contents of
L are written to this location.

• Note : When data is written to the


stack the stack pointer is first
decremented and then the data is
written

05/20/23 50
Reading from the Stack
• To execute the instruction pop BC
assuming initial sp contents is 2097
H.
• The contents of the memory location
at the address specified by the
contents of sp is moved to register C
and sp is incremented.
• The contents of the memory location
at the address specified by the
contents of sp is moved to register B
and sp is incremented.
• Note : When data is read from the
stack the data is read first and then
the stack pointer incremented.

05/20/23 51
Example
Write a program to exchange the contents
of BC register with DE register

Program 1 Program 2

MOV H,B PUSH B


MOV L,C PUSH D
MOV B,D POP B
MOV C,E POP D
MOV D,H
MOV E,L

05/20/23 52
Input/Output Operation

Input From The Port


• IN Port_Address
(port)  (A)

Output To Port
• OUT Port_Address
(A)  (Port)
05/20/23 53
Example
Input From The Port

IN 80H ;Read from Port 80H


STA 2100H ;Store to Memory

Output To Port
MVI A, 01H
OUT 81H ;Write 01H to Port 81H

05/20/23 54
Interrupt

RIM Read interrupt mask


SIM Set Interrupt mask
DI Disable Interrupt
EI Enable Interrupt

(Detail discussion in interrupt topic)

05/20/23 55
NEXT WEEK

ASSEMBLY LANGUAGE
PROGRAMMING

05/20/23 56

You might also like