Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Simple Programs in 8051 Assembly Language

By Himanshu Choudhary January 2, 2013

Here some simple assembly language programs for 8051 microcontroller are given to understand the operation of different instructions and to understand the logic behind particular program. First the statement of the program that describes what should be done is given. Then the solution is given which describes the logic how it will be done and last the code is given with necessary comments.

Statement 1: – exchange the content of FFh and FF00h

Solution: – here one is internal memory location and other is memory external location. so first the content of ext memory location FF00h is loaded in acc. then the content of int memory location FFh is saved first and then content of acc is transferred to FFh. now saved content of FFh is loaded in acc and then it is transferred to FF00h.

                   Mov dptr, #0FF00h     ; take the address in dptr

                        Movx a, @dptr            ; get the content of 0050h in a

                        Mov r0, 0FFh              ; save the content of 50h in r0

                        Mov 0FFh, a                ; move a to 50h

                        Mov a, r0                     ; get content of 50h in a

                        Movx @dptr, a            ; move it to 0050h

 

Duplication and Subtraction

Statement 2: – store the higher nibble of r7 in to both nibbles of r6

Solution: –first we shall get the upper nibble of r7 in r6. Then we swap nibbles of r7 and make OR operation with r6 so the upper and lower nibbles are duplicated             

Mov a, r7                     ; get the content in acc

                        Anl a, #0F0h                ; mask lower bit

                        Mov r6, a                     ; send it to r6

                        Swap a             ; xchange upper and lower nibbles of acc

                        Orl a, r6                       ; OR operation

                        Mov r6, a                     ; finally load content in r6

 

Statement 3: – treat r6-r7 and r4-r5 as two 16 bit registers. Perform subtraction between them. Store the result in 20h (lower byte) and 21h (higher byte).

Solution: – first we shall clear the carry. Then subtract the lower bytes afterward then subtract higher bytes.

                   Clr c                             ; clear carry

                        Mov a, r4                     ; get first lower byte

                        Subb a, r6                    ; subtract it with other

                        Mov 20h, a                  ; store the result

                        Mov a, r5                     ; get the first higher byte

                        Subb a, r7                    ; subtract from other

                        Mov 21h, a                  ; store the higher byte

 

Division & Data Transfer

Statement 4: – divide the content of r0 by r1. Store the result in r2 (answer) and r3 (reminder). Then restore the original content of r0.

Solution:-after getting answer to restore original content we have to multiply answer with divider and then add reminder in that.

                   Mov a, r0                     ; get the content of r0 and r1

                        Mov b, r1                     ; in register A and B

                        Div ab                          ; divide A by B

                        Mov r2, a                     ; store result in r2

                        Mov r3, b                     ; and reminder in r3

                        Mov b, r1                     ; again get content of r1 in B

                        Mul ab                         ; multiply it by answer

                        Add a, r3                     ; add reminder in new answer

                        Mov r0, a                     ; finally restore the content of r0

 

Statement 5: – transfer the block of data from 20h to 30h to external location 1020h to 1030h. 

Solution: – here we have to transfer 10 data bytes from internal to external RAM. So first, we need one counter. Then we need two pointers one for source second for destination.

                   Mov r7, #0Ah              ; initialize counter by 10d

                        Mov r0, #20h               ; get initial source location

                        Mov dptr, #1020h        ; get initial destination location

            Nxt:     Mov a, @r0                  ; get first content in acc

                        Movx @dptr, a            ; move it to external location

                        Inc r0                           ; increment source location

                        Inc dptr                        ; increase destination location

Djnz r7, nxt      ; decrease r7. if zero then over otherwise move next

Various Comparison Programs

Statement 6: – find out how many equal bytes between two memory blocks 10h to 20h and 20h to 30h.

Solution: – here we shall compare each byte one by one from both blocks. Increase the count every time when equal bytes are found

                   Mov r7, #0Ah              ; initialize counter by 10d

                        Mov r0, #10h               ; get initial location of block1

                        Mov r1, #20h               ; get initial location of block2

                        Mov r6, #00h               ; equal byte counter. Starts from zero

            Nxt:    Mov a, @r0                   ; get content of block 1 in acc

                        Mov b, a                      ; move it to B

                        Mov a, @r1                 ; get content of block 2 in acc

                        Cjne a, b, nomatch       ; compare both if equal

                        Inc r6                           ; increment the counter

Nomatch:         inc r0                            ; otherwise go for second number

                        Inc r1  

                        djnz r7, nxt       ; decrease r7. if zero then over otherwise move next

Statement 7: – given block of 100h to 200h. Find out how many bytes from this block are greater then the number in r2 and less then number in r3. Store the count in r4.

Solution: – in this program, we shall take each byte one by one from given block. Now here two limits are given higher limit in r3 and lower limit in r2. So we check first higher limit and then lower limit if the byte is in between these limits then count will be incremented.

                   Mov dptr, #0100h        ; get initial location

                        Mov r7, #0FFh            ; counter

                        Mov r4, #00h               ; number counter

                        Mov 20h, r2                 ; get the upper and lower limits in

                        Mov 21h, r3                 ; 20h and 21h

            Nxt:      Movx a, @dptr            ; get the content in acc

                        Cjne a, 21h, lower        ; check the upper limit first

                        Sjmp out                      ; if number is larger

            Lower: jnc out                          ; jump out

                        Cjne a, 20h, limit          ; check lower limit

                        Sjmp out                      ; if number is lower

            Limit:  jc out                             ; jump out

                        Inc r4                           ; if number within limit increment count

            Out:     inc dptr             ; get next location

                        Djnz r7, nxt                  ; repeat until block completes

Interrupt Counting, Subroutines & Scan n Mulitply

Statement 8:- the crystal frequency is given as 12 MHz. Make a subroutine that will generate delay of exact 1 ms. Use this delay to generate square wave of 50 Hz on pin P2.0

Solution: – 50 Hz means 20 ms. And because of square wave 10 ms ontime and 10 ms offtime. So for 10 ms we shall send 1 to port pin and for another 10 ms send 0 in continuous loop.

          <_x0021_xml:namespace prefix=”st1″ ns=”urn:schemas-microsoft-com:office:smarttags”/>Loop:   Setb p2.0                     ; send 1 to port pin

                        Mov r6, #0Ah              ; load 10d in r6

                        Acall delay                   ; call 1 ms delay ×10 = 10 ms

                        Clr p2.0                       ; send 0 to port pin

                        Mov r6, #0Ah              ; load 10d in r6

                        Acall delay                   ; call 1 ms delay ×10 = 10 ms

                        Sjmp loop                    ; continuous loop

                 Delay:                                  ; load count 250d

            Lp2:     Mov r7, #0FAh           

Lp1:     Nop                             ; 1 cycle

Nop                             ; 1+1=2 cycles

Djnz r7, lp1                  ; 1+1+2 = 4 cycles

Djnz r6, lp2                  ; 4×250 = 1000 cycles = 1000 µs = 1 ms

                        ret       

         

Statement 9:-count number of interrupts arriving on external interrupt pin INT1. Stop whencounter overflows and disable the interrupt. Give the indication on pinP0.0

Solution: –as we know whenever interrupt occurs the PC jumps to one particular location where it’s ISR is written. So we have to just write one ISR that will do the job

Movr2, #00h                ; initialize the counter

Movie, #84h                 ; enable external interrupt 1

           Here:     Sjmp here             ; continuous loop

 

Org 0013h                               ; interrupt 1location

Incr2                            ; increment the count

Cjner2, #00h, out         ; check whether it overflows

Movie, #00h                 ; if yes then disable interrupt

Clr       p0.0                 ; and give indication

            Out      : reti                              ; otherwise keep counting

Statement 10: –continuously scan port P0. If data is other then FFh write a subroutine that will multiply it with 10d and send it to port P1

Solution: –here we have to use polling method. We shall continuously pole port P0 if there is any data other then FFh. If there is data we shall call subroutine

 

Again:    Mov p0, #0ffh  ; initialize port P0 as input port

Loop:   Mov a, p0                     ; get the data in acc

Cjne a, #0FFh, dat       ; compare it with FFh

Sjmp loop                    ; if same keep looping

Dat:      acall multi;                    if different call subroutine

Sjmp again                     ; again start polling

       Multi

Mov b,#10d                 ; load 10d in register B

Mul ab                         ; multiply it with received data

Mov p1, a                    ; send the result to P1

Ret                               ;return to main program         


Filed Under: Tutorials
Tagged With: 8051, assembly, microcontroller, program
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Power Efficiency
Discover proven strategies for power conversion, wide bandgap devices, and motor control — balancing performance, cost, and sustainability across industrial, automotive, and IoT systems.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions.

  • Why is military EMC so strict?
  • Cannot prototype a power supply
  • Phased array antenna never on full power for long?
  • TPS63070 Power Supply
  • Question about resistors values impact on lead reading

RSS Electro-Tech-Online.com Discussions

  • need help in photodetection TIA circuit
  • Need to solder a really delicate ribbon for an electric reel need advice
  • Help please! BLDC driver circuit using the IR2136s and the STP80NF06 MOSFETS
  • Measuring controller current output with a meter
  • Anyone In The US Ordered From AliExpress Recently?

Featured Tutorials

Real Time Hardware Filter Design

  • Practical implementation of bandpass and band reject filters
    Practical implementation of bandpass and band reject filters
  • Practical application of hardware filters with real-life examples
    Practical application of hardware filters with real-life examples
  • A filter design example
    A filter design example
  • Types of filter responses
    Types of filter responses
  • What are the two types of hardware filters?
    What are the two types of hardware filters?
  • What are hardware filters and their types?
    What are hardware filters and their types?
More Tutorials >

Recent Articles

  • Microchip extends single pair Ethernet lineup with software-less endpoint
  • Infineon MCU targets high-voltage BMS in electric vehicles
  • SemiQ releases expanded SiC MOSFET lineup with detailed thermal and switching data
  • TDK adds 1000 W models to dc-dc converter series
  • LEMO introduces resin-free IP68 connectors for compact equipment

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe