EE6502_ALP

19
8085 MICROPROCESSOR PROGRAMS 1. Statement: Store the data byte 32H into memory location 4000H. Program 1: MVI A, 32H: Store 32H in the accumulator STA 4000H: Copy accumulator contents at address 4000H HLT: Terminate program execution Program 2: LXI H : Load HL with 4000H MVI M : Store 32H in memory location pointed by HL register pair(4000H) HLT : Terminate program execution 2. Statement: Exchange the contents of memory locations 2000H and 4000H Program 1: LDA 2000H : Get the contents of memory location 2000H into accumulator MOV B, A : Save the contents into B register LDA 4000H : Get the contents of memory location 4000Hinto accumulator STA 2000H : Store the contents of accumulator at address 2000H MOV A, B : Get the saved contents back into A register STA 4000H : Store the contents of accumulator at address 4000H Program 2: LXI H 2000H : Initialize HL register pair as a pointer to memory location 2000H. LXI D 4000H : Initialize DE register pair as a pointer to memory location 4000H. MOV B, M : Get the contents of memory location 2000H into B register. LDAX D : Get the contents of memory location 4000H into A register. MOV M, A : Store the contents of A register into memory location 2000H. MOV A, B : Copy the contents of B register into accumulator. STAX D : Store the contents of A register into memory location 4000H. HLT : Terminate program execution. 3: Subtract two 8-bit numbers Source program: LXI H, 4000H : HL points 4000H MOV A, M : Get first operand INX H : HL points 4001H SUB M : Subtract second operand INX H : HL points 4002H MOV M, A : Store result at 4002H. HLT : Terminate program execution

description

Assembly language program

Transcript of EE6502_ALP

Page 1: EE6502_ALP

8085 MICROPROCESSOR PROGRAMS

1. Statement: Store the data byte 32H into memory location 4000H. Program 1:

MVI A, 32H: Store 32H in the accumulator

STA 4000H: Copy accumulator contents at address 4000H

HLT: Terminate program execution

Program 2:

LXI H : Load HL with 4000H

MVI M : Store 32H in memory location pointed by HL register pair(4000H)

HLT : Terminate program execution

2. Statement: Exchange the contents of memory locations 2000H and 4000H

Program 1:

LDA 2000H : Get the contents of memory location 2000H into accumulator

MOV B, A : Save the contents into B register

LDA 4000H : Get the contents of memory location 4000Hinto accumulator

STA 2000H : Store the contents of accumulator at address 2000H

MOV A, B : Get the saved contents back into A register

STA 4000H : Store the contents of accumulator at address 4000H

Program 2:

LXI H 2000H : Initialize HL register pair as a pointer to memory location 2000H.

LXI D 4000H : Initialize DE register pair as a pointer to memory location 4000H.

MOV B, M : Get the contents of memory location 2000H into B register.

LDAX D : Get the contents of memory location 4000H into A register.

MOV M, A : Store the contents of A register into memory location 2000H.

MOV A, B : Copy the contents of B register into accumulator.

STAX D : Store the contents of A register into memory location 4000H.

HLT : Terminate program execution.

3: Subtract two 8-bit numbers

Source program:

LXI H, 4000H : HL points 4000H

MOV A, M : Get first operand

INX H : HL points 4001H

SUB M : Subtract second operand

INX H : HL points 4002H

MOV M, A : Store result at 4002H.

HLT : Terminate program execution

Page 2: EE6502_ALP

4. Statement: Find the 2's complement of the number stored at memory location

4200H and store the complemented number at memory location 4300H.

Sample problem:

(4200H) = 55H

Result = (4300H) = AAH + 1 = ABH

Source program:

LDA 4200H: Get the number

CMA: Complement the number

ADI, 01 H: Add one in the number

STA 4300H: Store the result

HLT: Terminate program execution

5.Statement: Pack the two unpacked BCD numbers stored in memory locations

4200H and 4201H and store result in memory location 4300H. Assume the least significant digit is

stored at 4200H.

Sample problem:

(4200H) = 04

(4201H) = 09

Result = (4300H) = 94

Source program

LDA 4201H : Get the Most significant BCD digit

RLC

RLC

RLC

RLC : Adjust the position of the second digit (09 is changed to 90)

ANI FOH : Make least significant BCD digit zero

MOV C, A : store the partial result

LDA 4200H : Get the lower BCD digit

ADD C : Add lower BCD digit

STA 4300H : Store the result

HLT : Terminate program execution

6. Statement: Two digit BCD number is stored in memory location 4200H. Unpack the BCD

number and store the two digits in memory locations 4300H and 4301H such that memory location

4300H will have lower BCD digit.

Sample problem

(4200H) = 58

Result = (4300H) = 08 and

(4301H) = 05

Source program

LDA 4200H : Get the packed BCD number

ANI FOH : Mask lower nibble

RRC

Page 3: EE6502_ALP

RRC

RRC

RRC : Adjust higher BCD digit as a lower digit

STA 4301H : Store the partial result

LDA 4200H : .Get the original BCD number

ANI OFH : Mask higher nibble

STA 4201H : Store the result

HLT : Terminate program execution

7.Statement: Calculate the sum of series of numbers. The length of the series is in memory location

4200H and the series begins from memory location 4201H.

a. Consider the sum to be 8 bit number. So, ignore carries. Store the sum at memory

location 4300H.

b. Consider the sum to be 16 bit number. Store the sum at memory locations 4300H

and 4301H

a. Sample problem

4200H = 04H

4201H = 10H

4202H = 45H

4203H = 33H

4204H = 22H

Result = 10 +41 + 30 + 12 = H

Source program:

LDA 4200H

MOV C, A : Initialize counter

SUB A : sum = 0

LXI H, 4201H : Initialize pointer

BACK: ADD M : SUM = SUM + data

INX H : increment pointer

DCR C : Decrement counter

JNZ BACK : if counter 0 repeat

STA 4300H : Store sum

HLT : Terminate program execution

b. Sample problem

4200H = 04H

420lH = 9AH

4202H = 52H

4203H = 89H

4204H = 3EH

Result = 9AH + 52H + 89H + 3EH = H

4300H = B3H Lower byte

4301H = 0lH Higher byte

Source program:

LDA 4200H

MOV C, A : Initialize counter

LXI H, 4201H : Initialize pointer

SUB A :Sum low = 0

MOV B, A : Sum high = 0

BACK: ADD M : Sum = sum + data

JNC SKIP

Page 4: EE6502_ALP

INR B : Add carry to MSB of SUM

SKIP: INX H : Increment pointer

DCR C : Decrement counter

JNZ BACK : Check if counter 0 repeat

STA 4300H : Store lower byte

MOV A, B

STA 4301H : Store higher byte

HLT :Terminate program exec

8. Statement: Multiply two 8-bit numbers stored in memory locations 2200H and

2201H by repetitive addition and store the result in memory locations 2300H and

2301H.

Sample problem:

(2200H) = 03H

(2201H) = B2H

Result = B2H + B2H + B2H = 216H

(2300H) = 16H

(2301H) = 02H

Source program

LDA 2200H

MOV E, A

MVI D, 00 : Get the first number in DE register pair

LDA 2201H

MOV C, A : Initialize counter

LX I H, 0000 H : Result = 0

BACK: DAD D : Result = result + first number

DCR C : Decrement count

JNZ BACK : If count 0 repeat

SHLD 2300H : Store result

HLT : Terminate program execution

9. Statement: Divide 16 bit number stored in memory locations 2200H and 2201H

by the 8 bit number stored at memory location 2202H. Store the quotient in memory

locations 2300H and 2301H and remainder in memory locations 2302H and 2303H.

Sample problem

(2200H) = 60H

(2201H) = A0H

(2202H) = l2H

Result = A060H/12H = 8E8H Quotient and 10H remainder

(2300H) = E8H

(2301H) = 08H

(2302H) = 10H

(2303H) = 00H

Source program

LHLD 2200H : Get the dividend

LDA 2202H : Get the divisor

MOV C, A

LXI D, 0000H : Quotient = 0

BACK: MOV A, L

SUB C : Subtract divisor

Page 5: EE6502_ALP

MOV L, A : Save partial result

JNC SKIP : if CY 1 jump

DCR H : Subtract borrow of previous subtraction

SKIP: INX D : Increment quotient

MOV A, H

CPI, 00 : Check if dividend < divisor

JNZ BACK : if no repeat

MOV A, L

CMP C

JNC BACK

SHLD 2302H : Store the remainder

XCHG

SHLD 2300H : Store the quotient

HLT : Terminate program execution

10. Statement: Find the number of negative elements (most significant bit 1) in a

block of data. The length of the block is in memory location 2200H and the block

Itself begins in memory location 2201H. Store the number of negative elements in

memory location 2300H

Sample problem

(2200H) = 04H

(2201H) = 56H

(2202H) = A9H

(2203H) = 73H

(2204H) = 82H

Result = 02 since 2202H and 2204H contain numbers with a MSB of 1.

Source program

LDA 2200H

MOV C, A : Initialize count

MVI B, 00 : Negative number = 0

LXI H, 2201H : Initialize pointer

BACK: MOV A, M : Get the number

ANI 80H : Check for MSB

JZ SKIP : If MSB = 1

INR B : Increment negative number count

SKIP: INX H : Increment pointer

DCR C : Decrement count

JNZ BACK : If count 0 repeat

MOV A, B

STA 2300H : Store the result

HLT : Terminate program execution

Page 6: EE6502_ALP

11.Statement:Find the largest number in a block of data. The length of the block

is in memory location 2200H and the block itself starts from memory location

2201H.

Store the maximum number in memory location 2300H. Assume that the numbers

in the block are all 8 bit unsigned binary numbers. Sample problem

(2200H) = 04H

(2201H) = 34H

(2202H) = A9H

(2203H) = 78H

(2204H) =56H

Result = (2202H) = A9H

Source program

LDA 2200H

MOV C, A: Initialize counter

XRA A : Maximum = Minimum possible value = 0

LXI H, 2201H : Initialize pointer

BACK: CMP M : Is number> maximum

JNC SKIP : Yes, replace maximum

MOV A, M

SKIP: INX H

DCR C

JNZ BACK

STA 2300H : Store maximum number

HLT : Terminate program execution

12. Statement: Write a program to sort given 10 numbers from memory location

2200H in the ascending order.

Source program:

MVI B, 09 : Initialize counter

START : LXI H, 2200H: Initialize memory pointer

MVI C, 09H : Initialize counter 2

BACK: MOV A, M : Get the number

INX H : Increment memory pointer

CMP M : Compare number with next number

JC SKIP : If less, don't interchange

JZ SKIP : If equal, don't interchange

MOV D, M

MOV M, A

DCX H

MOV M, D

INX H : Interchange two numbers

SKIP:DCR C : Decrement counter 2

JNZ BACK : If not zero, repeat

DCR B : Decrement counter 1

JNZ START

HLT : Terminate program execution

Page 7: EE6502_ALP

13. Statement: Calculate the sum of series of even numbers from the list of

numbers. The length of the list is in memory location 2200H and the series itself

begins from memory location 2201H. Assume the sum to be 8 bit number so you can

ignore carries and store the sum at memory location 2 Sample problem:

2200H= 4H

2201H= 20H

2202H= l5H

2203H= l3H

2204H= 22H

Result 22l0H= 20 + 22 = 42H

Source program:

LDA 2200H

MOV C, A : Initialize counter

MVI B, 00H : sum = 0

LXI H, 2201H : Initialize pointer

BACK: MOV A, M : Get the number

ANI 0lH : Mask Bit l to Bit7

JNZ SKIP : Don't add if number is ODD

MOV A, B : Get the sum

ADD M : SUM = SUM + data

MOV B, A : Store result in B register

SKIP: INX H : increment pointer

DCR C : Decrement counter

JNZ BACK : if counter 0 repeat

STA 2210H : store sum

HLT : Terminate program execution

14. Statement: Calculate the sum of series of odd numbers from the list of

numbers. The length of the list is in memory location 2200H and the series itself

begins from memory location 2201H. Assume the sum to be 16-bit. Store the sum at

memory locations 2300H and 2301H.

Sample problem:

2200H = 4H

2201H= 9AH

2202H= 52H

2203H= 89H

2204H= 3FH

Result = 89H + 3FH = C8H

2300H= H Lower byte

2301H = H Higher byte

Source program

LDA 2200H

MOV C, A : Initialize counter

LXI H, 2201H : Initialize pointer

MVI E, 00 : Sum low = 0

MOV D, E : Sum high = 0

BACK: MOV A, M : Get the number

ANI 0lH : Mask Bit 1 to Bit7

JZ SKIP : Don't add if number is even

MOV A, E : Get the lower byte of sum

Page 8: EE6502_ALP

ADD M : Sum = sum + data

MOV E, A : Store result in E register

JNC SKIP

INR D : Add carry to MSB of SUM

SKIP: INX H : Increment pointer

DCR C : Decrement

14. Statement:Find the square of the given numbers from memory location 6100H

and store the result from memory location 7000H

Source Program:

LXI H, 6200H : Initialize lookup table pointer

LXI D, 6100H : Initialize source memory pointer

LXI B, 7000H : Initialize destination memory pointer

BACK: LDAX D : Get the number

MOV L, A : A point to the square

MOV A, M : Get the square

STAX B : Store the result at destination memory location

INX D : Increment source memory pointer

INX B : Increment destination memory pointer

MOV A, C

CPI 05H : Check for last number

JNZ BACK : If not repeat

HLT : Terminate program execution

15. Statement: Search the given byte in the list of 50 numbers stored in the

consecutive memory locations and store the address of memory location in the

memory locations 2200H and 2201H. Assume byte is in the C register and starting

address of the list is 2000H. If byte is not found store 00 at 2200H and 2201H.

Source program:

LX I H, 2000H : Initialize memory pointer 52H

MVI B, 52H : Initialize counter

BACK: MOV A, M : Get the number

CMP C : Compare with the given byte

JZ LAST : Go last if match occurs

INX H : Increment memory pointer

DCR B : Decrement counter

JNZ B : I f not zero, repeat

LXI H, 0000H

SHLD 2200H

JMP END : Store 00 at 2200H and 2201H

LAST: SHLD 2200H : Store memory address

END: HLT : Stop

Page 9: EE6502_ALP

16. Statement: Write an assembly language program to separate even numbers

from the given list of 50 numbers and store them in the another list starting from

2300H. Assume starting address of 50 number list is 2200H

Source Program:

LXI H, 2200H : Initialize memory pointer l

LXI D, 2300H : Initialize memory pointer2

MVI C, 32H : Initialize counter

BACK:MOV A, M : Get the number

ANI 0lH : Check for even number

JNZ SKIP : If ODD, don't store

MOV A, M : Get the number

STAX D : Store the number in result list

INX D : Increment pointer 2

SKIP: INX H : Increment pointer l

DCR C : Decrement counter

JNZ BACK : If not zero, repeat

HLT : Stop

17. Statement: Write assembly language program with proper comments for the

following:

A block of data consisting of 256 bytes is stored in memory starting at 3000H.

This block is to be shifted (relocated) in memory from 3050H onwards. Do not shift

the block or part of the block anywhere else in the memory.

Source Program:

Two blocks (3000 - 30FF and 3050 - 314F) are overlapping. Therefore it

is necessary to transfer last byte first and first byte last.

MVI C, FFH : Initialize counter

LX I H, 30FFH : Initialize source memory pointer 3l4FH

LXI D, 314FH : Initialize destination memory pointer

BACK: MOV A, M : Get byte from source memory block

STAX D : Store byte in the destination memory block

DCX H : Decrement source memory pointer

DCX : Decrement destination memory pointer

DCR C : Decrement counter

JNZ BACK : If counter 0 repeat

HLT : Stop execution

Page 10: EE6502_ALP

18. 8085 PROGRAM TO OBTAIN SQUARE OF A NUMBER

LXI H, 5000 ;Initialize Look up table

LDA 5050 ; Get the data

CPI 0A ; Check input > 9

JC AFTER ;if yes error

MVI A,FF ;Error Indication

STA 5051

HLT

AFTER: MOV C,A ;Add the desired Address

MVI B,00

DAD B

MOV A,M

STA 5051 ; Store the result

HLT ; Terminate the program

LOOK UP TABLE:

5000 01

5001 04

5002 09

5003 16

5004 25

5005 36

5006 49

5007 64

5008 81

Page 11: EE6502_ALP

19) STEPPER MOTOR CONTROL PROGRAM (EXAMPLE FOR

SUBROUTINE PROGRAM) (ALSO EXAMPLE FOR STACK RELATED

OPERATION)

Page 12: EE6502_ALP

1) Discuss the (i) Arithmetic Instructions (ii) Logical Instructions (iii)Data

transfer Instructions (iv) Program and control (branching) instructions

and (v) Boolean variable instructions of 8051 microcontroller.

(i) ARITHMETIC INSTRUCTION

Mnemonics Description Bytes Instruction Cycles

ADD A, Rn A A + Rn 1 1

ADD A, direct A A + (direct) 2 1

ADD A, @Ri A A + @Ri 1 1

ADD A, #data A A + data 2 1

ADDC A, Rn A A + Rn + C 1 1

ADDC A, direct A A + (direct) + C 2 1

ADDC A, @Ri A A + @Ri + C 1 1

ADDC A, #data A A + data + C 2 1

DA A Decimal adjust accumulator 1 1

DIV AB Divide A by B

A quotient

B remainder

1 4

DEC A A A -1 1 1

DEC Rn Rn Rn - 1 1 1

DEC direct (direct) (direct) - 1 2 1

DEC @Ri @Ri @Ri - 1 1 1

INC A A A+1 1 1

INC Rn Rn Rn + 1 1 1

INC direct (direct) (direct) + 1 2 1

INC @Ri @Ri @Ri +1 1 1

INC DPTR DPTR DPTR +1 1 2

MUL AB Multiply A by B

A low byte (A*B)

B high byte (A* B)

1 4

SUBB A, Rn A A - Rn - C 1 1

SUBB A, direct A A - (direct) - C 2 1

SUBB A, @Ri A A - @Ri - C 1 1

SUBB A, #data A A - data - C 2 1

Page 13: EE6502_ALP

(ii)LOGICAL INSTRUCTION

Mnemonics Description Bytes Instruction Cycles

ANL A, Rn A A AND Rn 1 1

ANL A, direct A A AND (direct) 2 1

ANL A, @Ri A A AND @Ri 1 1

ANL A, #data A A AND data 2 1

ANL direct, A (direct) (direct) AND A 2 1

ANL direct, #data (direct) (direct) AND data 3 2

CLR A A 00H 1 1

CPL A A A 1 1

ORL A, Rn A A OR Rn 1 1

ORL A, direct A A OR (direct) 1 1

ORL A, @Ri A A OR @Ri 2 1

ORL A, #data A A OR data 1 1

ORL direct, A (direct) (direct) OR A 2 1

ORL direct, #data (direct) (direct) OR data 3 2

RL A Rotate accumulator left 1 1

RLC A Rotate accumulator left through

carry

1 1

RR A Rotate accumulator right 1 1

RRC A Rotate accumulator right through

carry

1 1

SWAP A Swap nibbles within Acumulator 1 1

XRL A, Rn A A EXOR Rn 1 1

XRL A, direct A A EXOR (direct) 1 1

XRL A, @Ri A A EXOR @Ri 2 1

XRL A, #data A A EXOR data 1 1

XRL direct, A (direct) (direct) EXOR A 2 1

XRL direct, #data (direct) (direct) EXOR data 3 2

Page 14: EE6502_ALP

(iii)DATA TRANSFER INSTRUCTIONS

Mnemonics Description Bytes Instruction

Cycles

MOV A, Rn A Rn 1 1

MOV A, direct A (direct) 2 1

MOV A, @Ri A @Ri 1 1

MOV A, #data A data 2 1

MOV Rn, A Rn A 1 1

MOV Rn, direct Rn (direct) 2 2

MOV Rn, #data Rn data 2 1

MOV direct, A (direct) A 2 1

MOV direct, Rn (direct) Rn 2 2

MOV direct1,

direct2

(direct1) (direct2) 3 2

MOV direct, @Ri (direct) @Ri 2 2

MOV direct, #data (direct) #data 3 2

MOV @Ri, A @Ri A 1 1

MOV @Ri, direct @Ri (direct) 2 2

MOV @Ri, #data @Ri data 2 1

MOV DPTR,

#data16

DPTR data16 3 2

MOVC A,

@A+DPTR

A Code byte pointed by A + DPTR 1 2

MOVC A,

@A+PC

A Code byte pointed by A + PC 1 2

MOVC A, @Ri A Code byte pointed by Ri 8-bit address) 1 2

MOVX A,

@DPTR

A External data pointed by DPTR 1 2

MOVX @Ri, A @Ri A (External data - 8bit address) 1 2

MOVX @DPTR,

A

@DPTR A(External data - 16bit address) 1 2

PUSH direct (SP) (direct) 2 2

POP direct (direct) (SP) 2 2

XCH Rn Exchange A with Rn 1 1

XCH direct Exchange A with direct byte 2 1

XCH @Ri Exchange A with indirect RAM 1 1

XCHD A, @Ri Exchange least significant nibble of A with

that of indirect RAM

1 1

Page 15: EE6502_ALP

(iv)PROGRAM AND MACHINE CONTROL (BRANCHING )INSTRUCTIONS

Mnemonics Description Bytes Instruction

Cycles

ACALL addr11 PC + 2 (SP) ; addr 11 PC 2 2

AJMP addr11 Addr11 PC 2 2

CJNE A, direct, rel Compare with A, jump (PC + rel) if not

equal

3 2

CJNE A, #data, rel Compare with A, jump (PC + rel) if not

equal

3 2

CJNE Rn, #data, rel Compare with Rn, jump (PC + rel) if not

equal

3 2

CJNE @Ri, #data, rel Compare with @Ri A, jump (PC + rel) if

not equal

3 2

DJNZ Rn, rel Decrement Rn, jump if not zero 2 2

DJNZ direct, rel Decrement (direct), jump if not zero 3 2

JC rel Jump (PC + rel) if C bit = 1 2 2

JNC rel Jump (PC + rel) if C bit = 0 2 2

JB bit, rel Jump (PC + rel) if bit = 1 3 2

JNB bit, rel Jump (PC + rel) if bit = 0 3 2

JBC bit, rel Jump (PC + rel) if bit = 1 3 2

JMP @A+DPTR A+DPTR PC 1 2

JZ rel If A=0, jump to PC + rel 2 2

JNZ rel If A ≠ 0 , jump to PC + rel 2 2

LCALL addr16 PC + 3 (SP), addr16 PC 3 2

LJMP addr 16 Addr16 PC 3 2

NOP No operation 1 1

RET (SP) PC 1 2

RETI (SP) PC, Enable Interrupt 1 2

SJMP rel PC + 2 + rel PC 2 2

JMP @A+DPTR A+DPTR PC 1 2

JZ rel If A = 0. jump PC+ rel 2 2

JNZ rel If A ≠ 0, jump PC + rel 2 2

NOP No operation 1 1

Page 16: EE6502_ALP

BOOLEAN VARIABLE INSTRUCTIONS

Mnemonics Description Bytes Instruction Cycles

CLR C C-bit 0 1 1

CLR bit bit 0 2 1

SET C C 1 1 1

SET bit bit 1 2 1

CPL C C 1 1

CPL bit bit 2 1

ANL C, /bit C C . 2 1

ANL C, bit C C. bit 2 1

ORL C, /bit C C + 2 1

ORL C, bit C C + bit 2 1

MOV C, bit C bit 2 1

MOV bit, C bit C 2 2

Page 17: EE6502_ALP

2) Write a 8051 program for (i) addition (ii) subtraction (iii) Multiplication (iv) Division (v) Ascending order (vi) Descending order.

BASIC 8051 PROGRAMS

8051 PROGRAM TO ADD TWO NUMBERS

MOV R0,#0X20 ; Move the first number to R0 register MOV R1,#0X30 ; Move the second number to R1 register MOV R2,#0X00 ; Move data 00 to R2 register MOV A,R0 ; Move the first number from R0 to accumulator

ADD A,R1; add the contents of accumulator with contents of R1register

JNC Result ;If no carry then goto Result MOV R2,#0X01 ; Make R2 content 01 to indicate Carry Result: MOV R3,A ; Move the result in accumulator to R3 register.

Here : SJMP Here

8051 PROGRAM TO SUBTRACT TWO 8 BIT NUMBERS

MOV R0,#0X20 ; Move the first number to R0 register MOV R1,#0X30 ; Move the second number to R1 register MOV R2,#0X00 ; Move the date 00 to R2 register MOV A,R1 ; Move the second number to accumulator from R1 reg SUBB A,R0 Subtract the contents of R0 register from accumulator JNC Result; If no carry then goto Result MOV R2,#0X01 ; Move 01 data to R2 register indicating carry

Result: MOV R3,A ; Move the result from accumulator to R3 register Here: SJMP Here; Control stays here

8051 PROGRAM TO SUBTRACT TWO 16 BIT NUMBERS MOV R0,#0X20 ; Move lower byte of 1st number to R0 register MOV R1,#0X30 ; Move Upper byte of 1st number to R1 register MOV R2,#0X20 ;Move Lower byte of 2nd number to R2 register MOV R3,#0X40 ;Move Upper byte of 2nd number to R3 register MOV R6,#0X00 ; Move data 00 to R6 register LCALL Adder ;Goto Adder subroutine Here SJMP Here ;Control stays here

Adder: MOV A,R2 ; Move lower byte of second number to accumulator SUBB A,R0 ;Sub the contents of accumulator to content of R0(lower byte 1 no) MOV R4,A ;Move the Lower byte of the result to R4 register MOV A,R3 ; Move the upper byte of second number to accumulator SUBB A,R1 ; Sub the contents of accumulator to content of R1(upper byte 2 no) JNC Result ;If no carry then goto the result MOV R6,#0X01 ;Indicate carry in R6 register Result:MOV R5,A ; Move the Upper byte of the result to R5 register RET

Page 18: EE6502_ALP

8051 PROGRAM TO DIVIDE TWO 8 BIT NUMBER

MOV R0,#0X60 ; Move the 1st number to R0 register MOV R1,#0X30 ; Move the second number to R1 register MOV A,R0 ; Move the first number to accumulator from R0 MOV B,R1 ; Move the second number to B register from R1 DIV AB ; Divide A/B MOV R2,A ; Move the result in accumulator to R2 register

HERE SJMP HERE

SORTING NUMBER IN ASCENDING ORDER

Sorting of array in ascending order ;Array size is 04 ;Array starting address is 70h

L0: MOV R1,#70 MOV R0,#70 MOV R2,#04 MOV R3,#00 DEC R2 L1: MOV A,@R0 MOV B,A INC R1 SUBB A,@R1 JC L2 MOV A,B XCH A,@R1 MOV @R0,A MOV R3,#01 L2: INC R0 DJNZ R2,L1 CJNE R3,#00,L0 HERE: SJMP HERE

SORTING NUMBER IN DESCENDING ORDER ;Sorting of array n descending order ;Array size is 04 ;Array starting address is 70h

L0: MOV R1,#70 MOV R0,#70 MOV R2,#04 MOV R3,#00 DEC R2 L1: MOV A,@R0 MOV B,A INC R1 SUBB A,@R1 JNC L2 MOV A,B XCH A,@R1 MOV @R0,A MOV R3,#01 L2: INC R0 DJNZ R2,L1 CJNE R3,#00,L0 HERE: SJMP HERE

Page 19: EE6502_ALP

LOOK UP TABLE PROGRAM

A look up table is a table which is predefined in the memory and the program gets some

necessary data from the table during runtime. This type of look up table is very important for

example if you want to perform some trigonometric operation (sin,cos,tan) during runtime of the

microcontroller then you need to perform a complicated calculation and it will increase the

controller overhead and will and cause the programme difficult and lengthy. But if you store the

values in memory previously then you can extract them easily during runtime which is very easy.

So here is an example of how to use look up table.

Finding Square of a Number Using Look up Table

MOVDPTR,#9000H ; Load look up table address 900H

MOVR0,#05 ;Load the number 05 in R0

MOVA,R0 ;Move data R0 to accumulator

MOVCA,@A+DPTR ;Get square of 05 from look up table and stored in

accumulator

MOVDPTR,#9100 ;Load 9100 in DPTR

MOVX@DPTR,ASend to 9100H memory location02 0

LJMP 00

LOOK UP TABLE

9000 00

9001 01

9002 04

9003 09

9004 16

9005 25

9006 36

9008 49

9009 64

9010 81