PROGRAM 1.
8051 ALP Program to move a block of n bytes of data from
source (20h) to destination (40h)
org 0000
mov r0,#20h
mov r1,#40h
mov r2,#05h
back: mov a,@r0
mov @r1,a
inc r0
inc r1
djnz r2, back
exit:sjmp exit
end
PROGRAM 2
8051 ALP to move a block of n bytes of data from source loc 2000h
to destination loc 2050h
ORG 0000h
mov dptr,#2000h
mov r0,#50h
mov r1,#20h
mov r2,#05
back: movx a,@dptr
push dph
push dpl
mov dph,r1
mov dpl,r0
movx @dptr,a
inc dptr
mov r1,dph
mov r0,dpl
pop dpl
pop dph
inc dptr
djnz r2, back
end
Program 3
8051 ALP to Exchange 5 Data between two locations i.e LocX =
20H and Loc Y=40H and Block size = 5
org 0000h
mov r0, #20h
mov r1, #40h
mov r2, #05
UP:
mov a, @r0
xch a, @r1
mov @r0, a
inc r0
inc r1
djnz r2, UP
end
Program 4
8051 ALP to Exchange Data between two locations of Internal RAM
org 0000h
mov r0, #10h
mov dptr, #2000h
mov r2, #06h
back: movx a, @dptr
mov b, @r0
xch a,b
movx @dptr, a
mov @r0, b
inc r0
inc dptr
djnz r2, back
end
Arithmetic & Logical Operation Program
5. Write an ALP to add the byte in the RAM at 34h and 35h, store the
result in the register R5 (LSB) and R6 (MSB), using Indirect Addressing
Mode
org 0000h
mov r0, #34h
mov a, @r0
inc r0
mov b, @r0
add a,b
jnc skip
mov r6, #01
skip:mov r5, a
exit: sjmp exit
end
5.b) Program
org 0000h
mov r0, #34h
mov a, @r0
inc r0
mov b, @r0
add a,b
inc r0
mov @ r0, a
mov r5, a
jnc exit
mov r6, #01
inc r0
mov a, r6
mov @ r0, a
exit:sjmp exit
end
Program 6.
8051 program to subtract the byte in the RAM at 34h and 35h &
store the result in the reg R5 and R6.
Program for that the result only add and show result in register
not in memory
org 0000h
mov r0,#34h
mov a,@r0
inc r0
mov b,@r0
subb a,b
mov r5,a
jnc exit
dec r6
exit:sjmp exit
end
Program 7.
8051 ALP to multiply two 8-bit numbers stored at 30h and 31h
and store16- bit result in 32h and 33h
org 0000h
mov r0, #30h
mov a, @r0
inc r0
mov b, @r0
mul ab
inc r0
mov @r0, a
inc r0
mov @r0, b
end
Program 8.
8051 ALP to perform division operation on 8-bit numbers by
8-bit number
org 0000h
mov r0, #30h
mov a, @r0
inc r0
mov b, @r0
div ab
inc r0
mov @r0, a
inc r0
mov @r0, b
end
Program 9.
8051 ALP to separate positive and negative in given array
org 0000h
mov r0, #30h
mov r1, #50h
mov r2, #05
back:mov a, @r0
JB acc.7, skip ; for the negative numbers JNB acc.7,skip
mov @r1, a
inc r1
skip: inc r0
djnz r2, back
sjmp $
end
Program 10.
8051 ALP to separate even or odd elements in a given array
org 0000h
mov r0,#30h
mov r1,#50h
mov r2,#05
back:mov a,@r0 ; for odd number JNB acc.0,skip
JB acc.0,skip
mov @r1,a
inc r1
skip:inc r0
djnz r2,back
sjmp $
end
Program 11
ASENDING /DESENDING
org 0000h
mov R7, #04h
MAIN:mov R0, #30h
mov R6, #04h
UP: mov A, @R0
inc R0
mov B, @R0
CJNE A, B, NEXT
NEXT:
JC NOEXCHANGE ; FOR DESENDING JNC NOEXCHANGE
mov @R0, A
dec R0
mov @R0, B
inc R0
NOEXCHANGE:
DJNZ R6, UP
DJNZ R7, MAIN
SJMP $
End
Program 12
Largest and Smallest
SMALLEST
org 0000h
mov r1,#20h
mov r0,#0FFh
mov r2,#05h
AGAIN:MOV A,@R1
mov 30h,r0
CJNE a,30h,down
sjmp skip
down:jnc skip
mov r0,a
skip:inc r1
mov 40h,r1
djnz r2,again
end
Program 13
ALP FOR DECIMAL UP_COUNTER
ORG 000H
MOV A,#00H
BACK: MOV P0,A
ACALL DELAY
ADD A,#01H
DA A
SJMP BACK
DELAY: MOV R1,#05FH
L1:MOV R2,#0FFH
L2:MOV R3,#0FFH
L3:DJNZ R3,L3
DJNZ R2,L2
DJNZ R1,L1
RET
END
Program 14
ALP FOR DECIMAL DOWN_COUNTER
ORG 000H
MOV A,#99H
BACK: MOV P0,A
ACALL DELAY
ADD A,#99H
DA A
SJMP BACK
DELAY: MOV R1,#09FH
L1:MOV R2,#0FFH
L2:MOV R3,#0FFH
L3:DJNZ R3,L3
DJNZ R2,L2
DJNZ R1,L1
RET
END
; DEBUG-DEBUG-RUN
Program 15
ALP FOR DECIMAL UP_COUNTER HEXADECIMAL
ORG 000H
MOV A,#00H
BACK: MOV P0,A
ACALL DELAY
ADD A,#01H
SJMP BACK
DELAY: MOV R1,#09FH
L1:MOV R2,#0FFH
L2:MOV R3,#0FFH
L3:DJNZ R3,L3
DJNZ R2,L2
DJNZ R1,L1
RET
END
Program 16
ALP FOR DECIMAL DOWN_COUNTER HEXADECIMAL
ORG 000H
MOV A,#0FFH
BACK: MOV P0,A
ACALL DELAY
ADD A,#0FFH
SJMP BACK
DELAY: MOV R1,#05FH
L1:MOV R2,#0FFH
L2:MOV R3,#0FFH
L3:DJNZ R3,L3
DJNZ R2,L2
DJNZ R1,L1
RET
END
Program 1
8051 C Program to find sum of first 10 integer numbers
#include<reg51.h>
void main()
{
unsigned char sum=0;
unsigned char i;
for(i=0;i<=9;i++)
{
sum=sum+i;
}
ACC=sum;
P0=sum;
}
PROGRAM 2
8051 C Program to find factorial of a number
#include<reg51.h>
void main()
{
unsigned int i;
unsigned char num=5;
unsigned long factorial=1;
for(i=1;i<=num;i++)
{
factorial=factorial*i;
}
P1=factorial;
}
CALCULATION
In general, the factorial formula represented as
If n is an natural number greater than or equal to 1, then
n!=n×(n−1)×(n−2)×(n−3)×...3×2×1
For finding the exact value of 5!
that we multiply 5 by every number below it i.e.,
⇒5! =5×4×3×2×1
On multiplication, we get
⇒5!=120
Hence, the exact value of factorial 5 is 5!=120
PROGRAM 3
Write an 8051 C Program to find the square of a number (1 to
10) using look-up table
#include<reg51.h>
void main()
{
unsigned char LUT[]= {1,4,9,16,25,36,49,64,81,100};
unsigned char num, square;
for(num=0; num<11; num++)
{
square =LUT[num-1];
P0=square;
}
while(1);
}
Calculation
{1,4,9,16,25,36,49,64,81,100} = {0,1,2,3,4,5,6,7,8,9}
Program 4
7) Write a 8051 C Program to count the number of ones and zeros in
two consecutive memory location.
#include <reg51.h>
void main()
{
unsigned char array[]={0x57,0xfc};
unsigned char i,ones,zeros;
CY=0;
for(i=0;i<8;i++)
{
array[0]>>=1;
if(CY==1) ones++;
else zeros++;
}
for(i=0;i<8;i++)
{
array[1]>>=1;
if(CY==1) ones++;
else zeros++;
}
P0=ones;
P1=zeros;
while(1);
}