You are on page 1of 56

Introduction to Assembly Language Programming

5/2/2014

High Level Language

Compiler Assembly Language

Assembler

Machine Code Microprocessor Hardware


5/2/2014 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.
5/2/2014 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.

5/2/2014

Programming Model
8 bit
8 bit A B D H SP PC CPU
5/2/2014

8 bit
00H 01H

0000H 0001H FLAG C E L

0002H 0003H 0004H 0005H 0006H

02H 03H 04H 05H 06H

FFFDH FFFEH FFFFH MEMORY

FDH FEH FFH


5

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) Rp (Register Pair)
5/2/2014

: A,B,C,D,E,H,L : BC,DE,HL & SP


6

Example
MVI A ,10 MVI B ,10010001B MVI D ,7FH ;A=0AH ;B=91H ;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
5/2/2014 7

Example
MVI MOV MOV MOV MOV MOV HLT
5/2/2014

B, 55H A,B C,A H,C L,A E,L


8

DIRECT DATA TRANSFER LDA address16 STA address16 LHLD address16 SHLD address16

5/2/2014

Example LDA 3000H (3000H) (A) STA 2100H (A) (2100H)


0000H 0001H STORE .. 2100H A 2101H x1 2102H .. LOAD .. 3000H

x1

A y1

y1

.. 3509H
10

5/2/2014

Example LHLD 8000H (8000H) (L) (8000H + 1) (H) SHLD 3500H (L) (3500H) (H) (3500H + 1)
5/2/2014

0000H 0001H STORE .. L H x1 x2 3500H 3501H 3502H .. LOAD .. 8000H x1 x2

y1

y1

y2

8001H 3509H

y2

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
5/2/2014 12

Example
LXI MVI STAX INX LDAX LXI MOV MOV B , 2020H A , 88H B B B H , 3000H D,M M,A B

0000H 0001H 0002H 0003H 0004H 0005H 0006H

2020H C

A D
5/2/2014

88H

20H H 30H

20H L 00H

2021H 3000H

88H AAH FFH


13

Instruction INX increment Register pair BC = 2021H

0000H 0001H 0002H 0003H 0004H 0005H 0006H

2020H B C

A D

AAH FFH

20H H 30H

21H L 00H

2021H 3000H

88H AAH AAH


14

5/2/2014

Transfer 10 byte data from memory location 3000h To memory location 3500h using LDA & STA 0000H 0001H LDA 3000H .. STA 3500H 3000H . 3001H . .. LDA 3009H 3009H STA 3509H .. 3500H .. 3509H
5/2/2014

x1 x2 .. x10

x1 .. x10

15

Transfer 10 byte data from memory location 3000h To memory location 3500h 0000H 0001H MVI H,10 LXI B , 3000H .. LXI D , 3500H 3000H LOOP: LDAX B 3001H STAX D .. INX B 3009H INX D DCR H .. JNZ LOOP 3500H HLT .. 3509H
5/2/2014

x1 x2

.. x10 x1 .. x10
16

Arithmetic Operation

5/2/2014

ALU FLAG CPU REGISTER

17

Arithmetic Instruction
ADDITION
ADI data8 ADD reg ACI data8 ADC reg DAD rp (A) + data8 (A) (A) + (reg) (A) (A) + data8 + CY (A) (A) + (reg) + CY (A) (HL) + (rp) (HL)
18

5/2/2014

EXAMPLE ADI 99H


register A
constant

; A contains 88 (H)
10001000 10011001
_____________

136 153
_____

decimal decimal

register A
S=0 Z=0 AC = 1 P=1 CY = 1

100100001

289

decimal

Bit D7 = 0 after addition The accumulator contains other than zero after addition There is a carry out of bit D3 to bit D4 during addition The accumulator contains an even number of 1s after addition There is an overflow as a result of the addition
19

5/2/2014

EXAMPLE ADC B CY
register A register B register A

; A contains 88 (H) ; CY =1 1
10001000 10011001
_____________

B contains 99 (H)

100100010

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

5/2/2014

20

SUBTRACTION

SUI data8 SUB reg


SBI data8 SBB reg

(A) - data8 (A) (A) - (reg) (A)


(A) - data8 - CY (A) (A) - (reg) - CY (A)

5/2/2014

21

INCREMENT/DECREMENT INR reg DCR reg (reg) + 1 (reg) (reg) - 1 (reg)

INX rp (rp) + 1 (rp) DCX rp (rp) - 1 (rp) Note : No Flag Effected for INX & DCX
5/2/2014 22

Logic Instruction
AND

AND Immediate With Accumulator ANI data8 (A) Data8 (A)


AND Register/Memory With Accumulator ANA reg (A) (Reg) (A)
5/2/2014 23

OR OR Immediate With Accumulator ORI data8 (A) V Data8 (A) OR Register/Memory With Accumulator ORA reg (A) V (Reg) (A)

5/2/2014

24

EXCLUSIVE-OR EX-OR Immediate With Accumulator XRI data8 (A) Data8 (A) EX-OR Register/Memory With Accumulator XRA reg (A) (Reg) (A)
5/2/2014 25

COMPLEMENT THE ACCUMULATOR

CMA

( A ) (A)
A

COMPLEMENT THE CARRY STATUS CMC (CY ) (CY)

5/2/2014

26

COMPARE
Compare Accumulator With Immediate Data CPI data8 (A) data8

Compare Accumulator With Register/Memory CMP reg (A) (reg)

Note: Only flag affected


5/2/2014 27

Rotate

Rotate Accumulator Right Through Carry RAR (A0) (CY) (An+1) (An) (CY) (A7)
CY A7 A6 A5 A4 A3 A2 A1 A0

5/2/2014

28

Rotate

Rotate Accumulator Left Through Carry RAL (A7) (CY) (An) (An+1) (CY) (A0)
CY A7 A6 A5 A4 A3 A2 A1 A0

5/2/2014

29

Rotate

Rotate Accumulator Right RRC (A0) (A7) (An+1) (An) (A0) (CY)
CY A7 A6 A5 A4 A3 A2 A1 A0

5/2/2014

30

Rotate

Rotate Accumulator Left RLC (A7) (A0) (An) (An+1) (A7) (CY)
CY A7 A6 A5 A4 A3 A2 A1 A0

5/2/2014

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)
5/2/2014 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
5/2/2014 33

Example 1 Check Zero Flag MVI B, 255 DCR B JNZ LOOP

LOOP:

;if Z == 0 then goto ;LOOP

5/2/2014

34

Example 2 Find the smallest value between two number (A) = x1 LOOP: ; (B) = x2 ;(A) (B) ;if CY == 0 then EXIT

CMP B JNC EXIT JMP STORE EXIT: MOV A, B STORE: STA 2050H
5/2/2014

35

Unconditional Call Subroutine CALL address16 (PCH) ((SP) 1) (PCL) ((SP) 2) (SP) 2 (SP) (Byte 3)(Byte 2) (PC)

5/2/2014

36

Conditional Call Subroutine C Condition address16 If (Condition = True) (PCH) ((SP) 1) (PCL) ((SP) 2) (SP) 2 (SP) (Byte 3)(Byte 2) (PC)

5/2/2014

37

CZ CNZ CC CNC CM CP CPE CPO

Z=1 Call if Zero flag SET Z=0 Call if Zero flag NOT SET CY=1 Call if Carry flag SET CY=0 Call if Carry flag NOT SET S=1 Call if Sign flag SET S=0 Call if Sign flag NOT SET P=1 Call if Parity flag SET P=0 Call if Parity flag NOT SET

5/2/2014

38

Return From Subroutine RET ((SP)) (PCL) ((SP) + 1) (PCH) (SP) + 2 (SP)

5/2/2014

39

Return From Subroutine (Conditional) R Condition If (Condition = True) ((SP)) (PCL) ((SP) + 1) (PCH) (SP) + 2 (SP)

5/2/2014

40

RZ RNZ RC RNC RM RP RPE RPO

Z=1 Return if Zero flag SET Z=0 Return if Zero flag NOT SET CY=1 Return if Carry flag SET CY=0 Return if Carry flag NOT SET S=1 Return if Sign flag SET S=0 Return if Sign flag NOT SET P=1 Return if Parity flag SET P=0 Return if Parity flag NOT SET

5/2/2014

41

Example LXI SP, 3FF0H MVI A, 80H OUT 83H MVI A,0 OUT 80H CALL DELAY MVI A,1 OUT 80H CALL DELAY JMP REPEAT MVI B, 0 DCR B JNZ LOOP RET END ;init Stack Pointer ;Init 8255, all port as output

REPEAT:

;Call subroutine

;Call subroutine

DELAY: LOOP:

;Subroutine

5/2/2014

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)
5/2/2014 43

Write The Content of Accumulator & Flag Status Onto The Stack PUSH PSW (A) ((SP) 1) (Flag) ((SP) 2) (SP) 2 (SP)

5/2/2014

44

Retreive The Content of Register Pair From The Stack POP rp ((SP)) (reg low) ((SP) + 1) (reg high) (SP) + 2 (SP)

5/2/2014

45

Retreive The Content of Accumulator & Flag Status From The Stack POP PSW ((SP)) ((SP) + 1) (SP) + 2

(Flag) (A) (SP)

5/2/2014

46

STACK OPERATION Lecture 2 (Revision)

5/2/2014

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
5/2/2014

LXI

SP, data 16
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.
5/2/2014 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
50

5/2/2014

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.
5/2/2014 51

Example Write a program to exchange the contents of BC register with DE register Program 1 MOV H,B MOV L,C MOV B,D MOV C,E MOV D,H MOV E,L Program 2 PUSH B PUSH D POP B POP D

5/2/2014

52

Input/Output Operation Input From The Port IN Port_Address (port) (A)

Output To Port OUT Port_Address (A) (Port)


5/2/2014 53

Example

Input From The Port


IN 80H STA 2100H ;Read from Port 80H ;Store to Memory

Output To Port
MVI A, 01H OUT 81H
5/2/2014

;Write 01H to Port 81H


54

Interrupt
RIM SIM DI EI Read interrupt mask Set Interrupt mask Disable Interrupt Enable Interrupt

(Detail discussion in interrupt topic)

5/2/2014

55

NEXT WEEK ASSEMBLY LANGUAGE PROGRAMMING

5/2/2014

56

You might also like