Skip to main content

8051 Simple Programs

Sum of 8-bit Numbers Stored in Memory

Here  we will discuss about 8051 programs but we can't discuss about all of the 8051 instructions. For programming 8051 we should know about assembler directives as well as instruction set. Click here to download Atmel c51 user guide that will discuss about 8051(c51 family microcontroller) instruction set, assembler directives, c51 cross assembler from Atmel. 

Program

ORG 00H
MOV R0,#50H   ;get memory location in memory pointer R0
MOV R1,#51H   ;get memory location on memory pointer register R1
MOV A,@R0      ;get content of memory location 50H to accumulator
ADD A,@R1        ;add content of A with content of memory location 51H and store result in A
MOV R0,#52H    ;get 52H to memory pointer R0
MOV@R0,A         ;copy content of A to memory location 52H
END

Add 16-bit Numbers

Program

ORG 00H
MOV DPTR,#2040H  ; get 2040H into DPTR
MOV A,#2BH              ;get lower byte of second 16-bit number on accumulator
MOV R0,#20H            ;get higher byte of second 16-bit number on accumulator
ADD A,82H                  ;[A]+[DPL]
MOV 82H,A                 ;save result of lower byte addition
MOV A,R0                    ;get higher byte of second number in A
ADDC A,83H                 ;[A]+[DPH]
MOV 83H,A                 ;Save result of higher byte addition
END

Unpack Packed Numbers

Program

ORG 00H
MOV R0,50H        ;get packed number from memory location 50H
MOV A,R0             ;copy packed number to accumulator
ANL A,#0FH        ; mask upper nibble of packed number
MOV 51H,A          ;save lower digit to 51H
MOV A,R0             ;get packed number again
ANL A,#0F0H       ;mask lower nibble of packed number
SWAP A                 ;exchange lower and upper nibbles
MOV 52H,A          ;save upper digit
END

Unpack the Packed BCD Numbers

Program

ORG 00H
MOV A,5OH          ;get the packed BCD number from memory location 50H
MOV 0F0H,A         ;save the packed BCD number to register B
ANL A,#0FH          ;mask upper nibble of BCD number
MOV R0,A              ;save the lower digit to R0
MOV A,0F0H         ;get the packed BCD number from register B to A
ANL A,#0F0H        ;mask lower nibble of BCD number
SWAP A                  ;exchange lower and upper nibbles
MOV R1,A              ;save the upper digit
END

Multiplication and Division

Program

ORG 00H
MOV A,51H      ;get content of memory location 51H to accumulator
MOV 0F0H,52H;get content of memory location 52H to B register
MUL AB              ;multiply content of A with content of B
MOV 53H,A        ;get lower order byte of product in memory location 53H
MOV54H,0F0H  ;get higher order byte of product in memory location in  54H
MOV A,51H         ;get content of memory location 51H to accumulator
MOV 0F0H,52H  ;get content of memory location 52H to register B
DIV AB                  ;divide content of register A with register B
MOV 55H,A          ;Copy quotient of result to memory location 55H
MOV 56H,0F0H   ;copy remainder of result to memory location 56H
END
😨Caution: The two legal operations that can be done with B register is MUL AB and DIV AB. For  using any other operation we must use address of B register 0F0H.

Note:After executing MUL AB Instruction the lower byte of 16-bit product is left in Accumulator and the high order byte in register B. If the product is greater than 255(FFH) the overflow(OV) flag is set; otherwise it is cleared.

For example, the largest possible product is FE01H when both A and B contain FFH. That is A contains 01H and register B contains FEh after multiplication of FFH by FFH. The OV flag is set to 1 to indicate that register B contains the high-order byte of the product; carry flag is zero.

Find Largest Number

Program

              ORG 00H
              MOV DPTR,#2000H;initialize pointer to memory where numbers are stored
               MOV R0,#0AH        ; initialize counter
               MOV R3,#00H      ;maximum=0
AGAIN: MOV A,@DPTR      ;get the number from memory
               CJNE A,R3,NE        ;compare number wi maximum number
               AJMP SKIP             ;if equal go to SKIP
        NE: JC SKIP                   ;if not equal check for carry, if carry go to skip
               MOV R3,A              ;otherwise maximum=[[DPTR]]
    SKIP: INC DPTR              ; Increment memory pointer
               DJNZ R0,AGAIN   ; Decrement count, if count=0 stop otherwise go to AGAIN
               END

Sort n Numbers in Ascending Order

Program

     ORG 00H
     MOV R2,50H         ;get n in R2
     DEC R2                    ;get actual count on register R2
L3:MOV 03,O2           ;get actual count from R2 to R3
     MOV R0,#60H      ;get memory location to be pointed on pointer register R0
L4:MOV A,@R0          ;copy [[R0]] into Accumulator
      INC R0                    ;Increment R0 to access next memory location
      MOV 05,@R0        ; copy [[R0]] to register R5
      CJNE A,05,L1       ;Compare[A] with [R5] and jump if not equal to L1
L1:JC L2                  ;Is [A]<[R5]? Yes jump to L2. If there is carry go to next step.
      MOV 04,@R0       ;copy [[R0]] to R4. Now small number is in R4. Since [[R0]]=[R5]
      MOV @R0,A         ;copy large number from A to current memory location pointed by R0.
      DEC R0                 ;accessing previous memory location
       MOV @R0,04      ;copy smallest number to [[R0]]
       INC R0                  ;comeback to current memory location
L2:DJNZ R2,L4
      DJNZ R2,L3          ;compare nc2 times using these two loops
       END
Note: Here I used 02, 03, 04, 05 etc. instead 02H, 03H, 04H, 05H etc. due to there is compiler gets confused is programmer is asking to manipulate address of register banks ranging from 00H to 1FH or bit addressable area starts from 20H as shown in figure of internal RAM organization of 8051.

 I assembled this program using ASM51.exe and simulated. It works well.

Sort n Numbers in Descending Order

Program

     ORG 00H
     MOV R2,50H         ;get n in R2
     DEC R2                    ;get actual count on register R2
L3:MOV 03,O2           ;get actual count from R2 to R3
     MOV R0,#60H      ;get memory location to be pointed on pointer register R0
L4:MOV A,@R0          ;copy [[R0]] into Accumulator
      INC R0                    ;Increment R0 to access next memory location
      MOV 05,@R0        ; copy [[R0]] to register R5
      CJNE A,05,L1       ;Compare[A] with [R5] and jump if not equal to L1
L1:JNC L2                  ;Is [A]>[R5]? Yes jump to L2. If there is no carry go to next step.
      MOV 04,@R0       ;copy [[R0]] to R4. Now large number is in R4. Since [[R0]]=[R5]
      MOV @R0,A         ;copy small number from A to current memory location pointed by R0.
      DEC R0                 ;accessing previous memory location
       MOV @R0,04      ;copy largest number to [[R0]]
       INC R0                  ;comeback to current memory location
L2:DJNZ R2,L4
      DJNZ R2,L3          ;compare nC2 times using these two loops
       END
Note: Here I used 02, 03, 04, 05 etc. instead 02H, 03H, 04H, 05H etc. due to there is compiler gets confused is programmer is asking to manipulate address of register banks ranging from 00H to 1FH or bit addressable area starts from 20H as shown in figure. I assembled this program using ASM51.exe and simulated. It works well.

Largest of n Number

Program

     ORG 00H
     MOV R2,50H       ;get n from memory location 50H to register R2
     DEC R2                 ;get actual count on R2 since 2 numbers are compared
      MOV R0,#60H    ;get memory location to be pointed on pointer register R0
      MOV A,@R0        ;get [[R0]] in Accumulator
      MOV R1,#61H     ;get memory location to be pointed  on pointer register R1
L1:MOV 00,@R1       ;get[[R1]] in register R0
      CJNE A,00,L2      ;compare [A] with [R0] and jump to label L2
L2:JNC L3                     ;is carry is not generated? Yes; jump to L3.If carry generated go to next step
       MOV A,@R1        ;if there is no carry generated then [A]>[R0]. So move [[R1]]=[R0] which is largest number into Accumulator
L3:INC R1                   ;access next memory location.
       DJNZ R2,L1         ;[R0]-1: is [R0]=0? No, jump to L1
       MOV 51H,A          ;save largest number
       END
Note: Figure shows internal RAM organization of 8051. In this figure you can see the address 00H two times(at bit addressable area 20H as well as at the address of Bank 0 register R0). So if we use MOV 00H,@R0 compiler get confused, is programmer is asking to move the [[R0]] into Bank 0 register R0 or to move [[R0]] into bit addressable area 20H.0. So here I used 00H instead 00 to indicate register R0. I assembled this program using ASM51.exe and simulated it. However it works well.

Smallest of n Numbers

Program

     ORG 00H
     MOV R2,50H       ;get n from memory location 50H to register R2
     DEC R2                 ;get actual count on R2 since 2 numbers are compared
      MOV R0,#60H    ;get memory location to be pointed on pointer register R0
      MOV A,@R0        ;get [[R0]] in Accumulator
      MOV R1,#61H     ;get memory location to be pointed  on pointer register R1
L1:MOV 00,@R1       ;get[[R1]] in register R0
      CJNE A,00,L2      ;compare [A] with [R0] and jump to label L2
L2:JC L3                     ;is carry generated? Yes; jump to L3.If no carry generated go to next step
       MOV A,@R1        ;if there is no carry generated then [A]>[R0]. So move [[R1]]=[R0] which is smallest number into Accumulator
L3:INC R1                   ;access next memory location.
       DJNZ R2,L1         ;[R0]-1: is [R0]=0? No, jump to L1
       MOV 51H,A          ;save smallest number
       END
Note: The first figure shows internal RAM organization of 8051. In this figure you can see the address 00H two times(at bit addressable area 20H as well as at the address of Bank 0 register R0). So if we use MOV 00H,@R0 compiler get confused, is programmer is asking to move the [[R0]] into Bank 0 register R0 or to move [[R0]] into bit addressable area 20H.0. So here I used 00H instead 00 to indicate register R0. I assembled this program using ASM51.exe and simulated it. However it works well.

Hexadecimal to Decimal Conversion

Program

Fortunately here we have instructions like MUL AB and DIV AB unlike 8085 microprocessor.
ORG 00H
MOV A,52H         ;get hexadecimal number from memory location 52H
MOV 0F0H,#64H;load register B with 64H= 100 in decimal
DIV AB                 ;divide number with 100
MOV 60H,A         ;save number of hundreds(quotient of previous division)
MOV A,0F0H       ;Get remainder on A
MOV 0F0H,#0AH;load register B with 0AH=10 in decimal
DIV AB                   ;divide number with 10
MOV 61H,A           ;save tens of the number
MOV 62H,0F0H    ; save number of ones of the number
END

Comments

  1. Write 8051 assembly language program to add 10 BCD numbers and store the result in BCD in register R1 pls do give answer to this question

    ReplyDelete
  2. Write an ALP to check the given array is sorted in ascending order if not then sort the given number in ascending order.

    ReplyDelete
  3. May I know the memory address for every programs?????

    ReplyDelete
  4. When it comes to programming the 8051 microcontroller, there are several simple programs that you can write to get started. These programs are typically used for learning purposes and can help you understand the basics of the 8051 architecture and instruction set.
    best cheap camera for photography

    ReplyDelete

Post a Comment

Popular posts from this blog

Introduction to 8051 embedded C

For 8051 we need to include the file reg51.h. This file contains the all the definitions of 8051 registers. With this information C compiler produces hex file that can be downloaded into the ROM of the microcontroller. It is important to note that the size of the hex file produced by the assembly language is much larger than the hex file produced by C compiler. Apart from this fact, there is many reasons for writing programs in C instead of assembly: ●It is much easier and less time consuming to write programs in C assembly. ●C is more flexible; it is easier to modify and update. ●Programming in C allows to use code available in function libraries. ●Program written inC for one microcontroller is portable to other microcontrollers with little or no modifications. Data Types in 8051 Embedded C The table shown below lists the data types that are available in typical C51 compiler. The gives information about the size of the data variable and it's value range. Data type ...

Frequency of Oscillation of RC Phase Shift Oscillator

Derivation of Frequency of Oscillation We have to find out the transfer function of RC feedback network. Feedback Circuit of RC Phase Shift Oscillator Applying KVL to various loops on the figure, we get, $$I_1 \left(R+\frac{1}{j \omega C }\right) -I_2R=V_i \text{ ....(1)}$$ $$-I_1R+I_2\left (2R+\frac {1}{j\omega C}\right)-I_3R=0\text{ ... (2)}$$ $$0-I_2R+I_3\left(2R+ \frac{1}{j\omega C}\right)=0\text{ ...(3)}$$ Replacing \(j\omega\) with \(s\) and writing equations in the matrix form, $$\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}\begin{bmatrix}I_1\\I_2\\I_3\end{bmatrix}=\begin{bmatrix}V_i\\0\\0\end{bmatrix}$$ Using Cramer's rule to find out \(I_3\), $$\text{Let, }D=\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}$$ \(|D|=\begin{vmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{...