lecture4 assembly

56
06/11/22 1 Introduction to Assembly Language Programming

Upload: athome

Post on 10-May-2017

242 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture4 Assembly

05/03/23 1

Introduction to Assembly Language Programming

Page 2: Lecture4 Assembly

05/03/23 2

High Level Language

Compiler

Assembly Language

Assembler

Machine Code

Microprocessor Hardware

Page 3: Lecture4 Assembly

05/03/23 3

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.

Page 4: Lecture4 Assembly

05/03/23 4

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.

Page 5: Lecture4 Assembly

05/03/23 5

Programming Model0000H

0003H

0001H0002H

0005H0004H

FFFDH

0006H

FFFFHFFFEH

MEMORY

ABD EH L

CFLAG

SPPC

CPU

8 bit

8 bit 00H

03H

01H 02H

05H 04H

FDH

06H

FFH FEH

I/O

8 bit

Page 6: Lecture4 Assembly

05/03/23 6

Data TransferIMMEDIATE 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,LRp (Register Pair) : BC,DE,HL & SP

Page 7: Lecture4 Assembly

05/03/23 7

ExampleMVI A ,10 ;A=0AHMVI B ,10010001B ;B=91HMVI D ,7FH ;D=7FH

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

Page 8: Lecture4 Assembly

05/03/23 8

Example

MVI B, 55HMOV A , B MOV C , AMOV H , CMOV L , AMOV E , LHLT

Page 9: Lecture4 Assembly

05/03/23 9

DIRECT DATA TRANSFER • LDA address16• STA address16• LHLD address16• SHLD address16

Page 10: Lecture4 Assembly

05/03/23 10

Example

• LDA 3000H(3000H) (A)

• STA 2100H(A) (2100H)

x1

0000H

2100H

0001H..

2102H2101H

3000H

..

3509H

y1

..

..

Ax1

Ay1

STORE

LOAD

Page 11: Lecture4 Assembly

05/03/23 11

Example

LHLD 8000H(8000H) (L)(8000H + 1) (H)

SHLD 3500H(L) (3500H) (H) (3500H + 1)

x1

y2

0000H

3500H

0001H..

3502H3501H

8000H

..

3509H

y1

..

8001H

Hx1

L y1

STORE

LOAD

x2

y2

x2

L

H

Page 12: Lecture4 Assembly

05/03/23 12

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

Page 13: Lecture4 Assembly

05/03/23 13

ExampleLXI B , 2020HMVI A , 88HSTAX BINX BLDAX BLXI H , 3000HMOV D , MMOV M , A 88H

AAH

0000H

0003H

0001H0002H

0005H0004H

2020H

0006H

3000H

B

D88H

00H30H

20H 20HAH L

C 2021H

FFH

Page 14: Lecture4 Assembly

05/03/23 14

88HAAH

0000H

0003H

0001H0002H

0005H0004H

2020H

0006H

3000H

B

DAAH

00H30HFFH

20H 21HAH L

C 2021H

AAH

• Instruction INX – increment Register pair

• BC = 2021H

Page 15: Lecture4 Assembly

05/03/23 15

• Transfer 10 byte data from memory location 3000h• To memory location 3500h using LDA & STA

LDA 3000HSTA 3500H

.

.LDA 3009HSTA 3509H

x1

x2

..x10

..

0000H

3000H

0001H..

..3001H

3500H

3009H

3509H

x1

x10

..

..

Page 16: Lecture4 Assembly

05/03/23 16

• Transfer 10 byte data from memory location 3000h• To memory location 3500h

MVI H,10LXI B , 3000HLXI D , 3500H

LOOP: LDAX BSTAX DINX BINX DDCR HJNZ LOOPHLT

x1

x2

..x10

..

0000H

3000H

0001H..

..3001H

3500H

3009H

3509H

x1

x10

..

..

Page 17: Lecture4 Assembly

05/03/23 17

Arithmetic Operation

• ALU • FLAG• CPU REGISTER

Page 18: Lecture4 Assembly

05/03/23 18

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)

Page 19: Lecture4 Assembly

05/03/23 19

EXAMPLE

• ADI 99H ; A contains 88 (H)• register A 1 0 0 0 1 0 0 0 1 3 6 decimal constant 1 0

0 1 1 0 0 1 1 5 3 decimal _____________ _____ register A 1 0 0 1 0 0 0 0 1 2 8 9 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

Page 20: Lecture4 Assembly

05/03/23 20

EXAMPLE

• ADC B ; A contains 88 (H) B contains 99 (H); CY =1

CY 1register A 1 0 0 0 1 0 0 0register B 1 0 0 1 1 0 0 1

_____________register A 1 0 0 1 0 0 0 1 0

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

Page 21: Lecture4 Assembly

05/03/23 21

SUBTRACTION

• SUI data8(A) - data8 (A)• SUB reg (A) - (reg) (A)

• SBI data8(A) - data8 - CY (A)• SBB reg (A) - (reg) - CY (A)

Page 22: Lecture4 Assembly

05/03/23 22

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

Page 23: Lecture4 Assembly

05/03/23 23

Logic InstructionAND

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

AND Register/Memory With Accumulator• ANA reg (A) Λ (Reg) (A)

Page 24: Lecture4 Assembly

05/03/23 24

OR

OR Immediate With Accumulator • ORI data8 (A) V Data8 (A)

OR Register/Memory With Accumulator• ORA reg (A) V (Reg) (A)

Page 25: Lecture4 Assembly

05/03/23 25

EXCLUSIVE-OR

EX-OR Immediate With Accumulator • XRI data8 (A) ⊕ Data8 (A)

EX-OR Register/Memory With Accumulator• XRA reg (A) ⊕ (Reg) (A)

Page 26: Lecture4 Assembly

05/03/23 26

COMPLEMENT THE ACCUMULATOR

• CMA ( ) (A)

COMPLEMENT THE CARRY STATUS

• CMC ( ) (CY)

A

A

CY

Page 27: Lecture4 Assembly

05/03/23 27

COMPARE

Compare Accumulator With Immediate Data

• CPI data8 (A) – data8

Compare Accumulator With Register/Memory

• CMP reg (A) – (reg)

Note: Only flag affected

Page 28: Lecture4 Assembly

05/03/23 28

Rotate

Rotate Accumulator Right Through Carry• RAR (A0) (CY)

(An+1) (An)

(CY) (A7)A0A1A2A3A4A5A6A7CY

Page 29: Lecture4 Assembly

05/03/23 29

Rotate

Rotate Accumulator Left Through Carry• RAL (A7) (CY)

(An) (An+1)

(CY) (A0)A0A1A2A3A4A5A6A7CY

Page 30: Lecture4 Assembly

05/03/23 30

Rotate

Rotate Accumulator Right• RRC (A0) (A7)

(An+1) (An)

(A0) (CY)A0A1A2A3A4A5A6A7CY

Page 31: Lecture4 Assembly

05/03/23 31

Rotate

Rotate Accumulator Left• RLC (A7) (A0)

(An) (An+1)

(A7) (CY)A0A1A2A3A4A5A6A7CY

Page 32: Lecture4 Assembly

05/03/23 32

Branch InstructionUnconditional Jump• JMP address16(Byte 3) (Byte 2) (PC)

Conditional Jump• J Condition address16If (Condition= true)

(Byte 3) (Byte 2) (PC)

Page 33: Lecture4 Assembly

05/03/23 33

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

Page 34: Lecture4 Assembly

05/03/23 34

Example 1Check Zero Flag

MVI B, 255LOOP:DCR B

JNZ LOOP ;if Z == 0 then goto ;LOOP

Page 35: Lecture4 Assembly

05/03/23 35

Example 2 – Find the smallest value between two number

(A) = x1 ; (B) = x2

LOOP: CMP B ;(A) – (B)JNC EXIT;if CY == 0 then EXITJMP STORE

EXIT: MOV A, BSTORE:STA 2050H

Page 36: Lecture4 Assembly

05/03/23 36

Unconditional Call Subroutine • CALL address16

(PCH) ((SP) –1)(PCL) ((SP) –2)(SP) – 2 (SP) (Byte 3)(Byte 2) (PC)

Page 37: Lecture4 Assembly

05/03/23 37

Conditional Call Subroutine C Condition address16

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

Page 38: Lecture4 Assembly

05/03/23 38

• 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

Page 39: Lecture4 Assembly

05/03/23 39

Return From Subroutine• RET

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

Page 40: Lecture4 Assembly

05/03/23 40

Return From Subroutine (Conditional)

• R ConditionIf (Condition = True)((SP)) (PCL)((SP) + 1) (PCH)(SP) + 2 (SP)

Page 41: Lecture4 Assembly

05/03/23 41

• 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

Page 42: Lecture4 Assembly

05/03/23 42

ExampleLXI SP, 3FF0H ;init Stack PointerMVI A, 80HOUT 83H ;Init 8255, all port as output

REPEAT: MVI A,0OUT 80HCALL DELAY ;Call subroutine MVI A,1OUT 80HCALL DELAY ;Call subroutine JMP REPEAT

DELAY: MVI B, 0 ;SubroutineLOOP: DCR B

JNZ LOOPRETEND

Page 43: Lecture4 Assembly

05/03/23 43

I/O,Stack, Machine Control Instruction

Stack OperationWrite The Content of Register Pair Onto The Stack• PUSH rp(reg high) ((SP) –1)(reg low) ((SP) –2)(SP) – 2 (SP)

Page 44: Lecture4 Assembly

05/03/23 44

Write The Content of Accumulator & Flag Status Onto The Stack

• PUSH PSW(A) ((SP) –1)(Flag) ((SP) –2)(SP) – 2 (SP)

Page 45: Lecture4 Assembly

05/03/23 45

Retreive The Content of Register Pair From The Stack

• POP rp((SP)) (reg low)((SP) + 1) (reg high)(SP) + 2 (SP)

Page 46: Lecture4 Assembly

05/03/23 46

Retreive The Content of Accumulator & Flag Status From The Stack

• POP PSW((SP)) (Flag)((SP) + 1) (A)(SP) + 2 (SP)

Page 47: Lecture4 Assembly

05/03/23 47

STACK OPERATIONLecture 2

(Revision)

Page 48: Lecture4 Assembly

05/03/23 48

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

Page 49: Lecture4 Assembly

05/03/23 49

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.

Page 50: Lecture4 Assembly

05/03/23 50

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

Page 51: Lecture4 Assembly

05/03/23 51

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.

Page 52: Lecture4 Assembly

05/03/23 52

ExampleWrite a program to exchange the contents of BC register with DE register

Program 1 Program 2

MOV H,B PUSH BMOV L,C PUSH DMOV B,D POP BMOV C,E POP D MOV D,HMOV E,L

Page 53: Lecture4 Assembly

05/03/23 53

Input/Output Operation Input From The Port• IN Port_Address

(port) (A)

Output To Port• OUT Port_Address

(A) (Port)

Page 54: Lecture4 Assembly

05/03/23 54

Example

Input From The Port

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

Output To PortMVI A, 01HOUT 81H ;Write 01H to Port 81H

Page 55: Lecture4 Assembly

05/03/23 55

Interrupt

RIM Read interrupt maskSIM Set Interrupt maskDI Disable InterruptEI Enable Interrupt

(Detail discussion in interrupt topic)

Page 56: Lecture4 Assembly

05/03/23 56

NEXT WEEK

ASSEMBLY LANGUAGE PROGRAMMING