Alp 1
Alp 1
Practical no- 5
Arithmetic, logical & code conversion problems using assembly/C programming.
Program No. 1
To add elements of 2 equal length array.
Program Statement
Write a program to add elements of 2 equal length array bytes stored in internal RAM
locations 10H & 20H. Store the result in internal RAM locations 30 H.
Program
ORG 00H
MOV 10H,#03H // 1st array address and its data
MOV 11H,#04H
MOV 12H,#05H
MOV 13H,#06H
MOV 14H,#07H
MOV 20H,#01H // 2nd array address and its data
MOV 21H,#02H
MOV 22H,#03H
MOV 23H,#04H
MOV 24H,#05H
MOV R0,#10H //Memory pointer for 1st array
MOV R1,#20H //Memory pointer for 2nd array
MOV R5,#05H //Counter for 5 byte addition
SETB PSW.3 // Set 3rd bit of PSW to select register bank 1
MOV R0,#30H //Memory pointer for storing the result of addition
CLR PSW.3 // Clear 3rd bit of PSW to select register bank 0.
BACK:
MOV A,@R0 //move the contents of R0 th location in A
MOV B,@R1 //move the contents of R1 th location in B
ADD A,B // add both
SETB PSW.3 // Set 3rd bit of PSW
MOV @R0,A //move the addition result in R0th register i.e 30H first
INC R0 //Increment R0th register for Bank 1
CLR PSW.3
INC R0 //Increment R0th register for Bank 0
INC R1 //Increment R1th register
DJNZ R5,BACK //Decrement R5 & jump to BACK label if ≠0
END
For SYBSc (CS)-Sem III Grp-A Practical – Prepared by Poonam S. Chavan(Asst. Prof.-VCACS)
Result –
Before debug/execution After debug/execution
Array 1 Memory address = Array 2 Memory address = Array 3 (Addition)
Data Data
10H =03H 20H =01H 30H =04H
11H=04H 21H=02H 31H=06H
12H =05H 22H =03H 32H =08H
13H=06H 23H=04H 33H=0AH
14H=07H 24H=05H 34H=0CH
Program 2
Write an assembly language program to convert hexadecimal number to decimal.
Soln. To convert a hexadecimal number to decimal number we divide the number by 10, till its
quotient is less than 10.
e.g – (98)16 = (?)10
98/0A = F(quotient), 2 (remainder) = 2 (Lowest digit)
0F/0A = 1 (quotient), 5 (remainder) = 5 (middle digit)
1(quotient less than 10) = 1 (highest digit)
Thus, the decimal equivalent of 98H is 152.
Program Statement
This program is for converting an 8 bit hexadecimal number (98) into decimal number.
Program
ORG 00H
MOV A, #98H //copy the given no. in A reg.
MOV B, #0AH //take the divisor (10=0AH) in B reg.
DIV AB //Divide
MOV R0, B // Copy the remainder in R0(LSB)
MOV B, # 0AH // reload the divisor (10=0AH) in B reg.
DIV AB
MOV R1, B // Copy the remainder in R1(Middle bit)
MOV R2, A // Copy the quotient from A in R2 (MSB)
END
Result -
Before debug/execution After debug/execution
A = 98H A = 01H
B = 00H B = 05H
R0 = 00H R0 = 02H
R1 = 00H R1 = 05H
R2 = 00H R2 = 01H
(98)16 = (152)10
For SYBSc (CS)-Sem III Grp-A Practical – Prepared by Poonam S. Chavan(Asst. Prof.-VCACS)
Program 3
Write an assembly language program to convert Decimal number to hexadecimal.
Soln. To convert a Decimal number to hexadecimal number we divide the given number by 16,
till its quotient is less than 16.
Program Statement
This program is for converting a Decimal number (33) into hexadecimal number.
Program
ORG 00H
MOV R1, #33 //move the given decimal no.(here 33) in R1
MOV A,R1 //copy no. in A
MOV B, # 16 //take the divisor (16) in B reg
DIV AB //divide
MOV R2, B //copy the remainder in R2
MOV B, #16 //take the divisor (16) in B reg
JZ STOP //Stop if quotient is less than 16
DIV AB //else divide
MOV R3, B //copy the next remainder in R3
STOP:
END
Result -
Before debug/execution After debug/execution
A = 00H , then A = 33D =21H A = 00H
B = 00H , then B= 16D =10H B = 02H
R1 = 00H R1 = 21H
R2 = 00H R2 = 01H
R3 = 00H R3 = 02H
(33)10 = (21)16
Program 4
Program to find maximum/largest number in the array.
Program statement
Given an array of N numbers. Write a program in ALP of 8051 to find the largest number
amongst the N numbers.
Assume the length of array is stored in R1 of the register bank 0. Assume that the array begins
from memory location 30H. Store the largest number in the register R2of register bank 0.
For SYBSc (CS)-Sem III Grp-A Practical – Prepared by Poonam S. Chavan(Asst. Prof.-VCACS)
Algorithm
Step I -Initialize memory pointer at R0
Step II – Load data pointed by memory pointer i.e R0 in register A
Step III – set the counter
Step IV– copy data from A to B
Step V– Decrement the counter , Check if count ≠ 0. If yes go to step VII
Step VI - Else short jump to STP label
Step VII - Increment memory pointer R0 to next memory location.
Step VIII– load data pointed by pointer i.e. R0 in A
Step IX – Compare the two numbers i.e. A & B. If A ≠ B ; go to step X .
Else short jump to step V
Step X - If B< A ; go to step V
If B > A; go to step IV
Step XI– Store the result in register R2.
Step XII– Stop
Program
ORG 00H
MOV R0, #30H // load memory pointer at R0=30h
MOV A,@R0 //copy the data in A
MOV R1,#05H //set counter at R1=5
L1: MOV B, A //copy data from A to B
L3: DJNZ R1, L2
SJMP STP
L2: INC R0
MOV A,@R0
CJNE A, B,NEQ
SJMP L3
NEQ: JC L3
SJMP L1
STP: MOV R2,B
END
Result -
Before debug/execution After debug/execution
Memory address = Data Registers
30H =02H A = 00H A = 0AH
31H=01H B = 00H B = 0AH
32H =06H R0 = 00H R0 = 34H
33H=04H R1 = 00H R1 = 00H
34H=0AH R2 = 00H R2 = 0AH = largest no.
For SYBSc (CS)-Sem III Grp-A Practical – Prepared by Poonam S. Chavan(Asst. Prof.-VCACS)