0% found this document useful (0 votes)
34 views10 pages

8051 Assembly Programming Exercises

The document contains a series of 14 tutorial questions and corresponding assembly programs for the 8051 microcontroller, covering various operations such as incrementing/decrementing numbers, comparing values, performing arithmetic operations, and generating patterns. Each program includes a brief explanation, input, solution steps, output, and application context. The programs serve educational purposes for understanding assembly language and microcontroller functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views10 pages

8051 Assembly Programming Exercises

The document contains a series of 14 tutorial questions and corresponding assembly programs for the 8051 microcontroller, covering various operations such as incrementing/decrementing numbers, comparing values, performing arithmetic operations, and generating patterns. Each program includes a brief explanation, input, solution steps, output, and application context. The programs serve educational purposes for understanding assembly language and microcontroller functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

8051-TUTORIAL QUESTIONS

1. Write an 8051-assembly program to increment and decrement a number stored in a


register.

2. Write an 8051-assembly program to compare two numbers and set a flag if they are
equal.

3. Write an 8051-assembly program to compute the square of a number using


multiplication.

4. Write an 8051-assembly program to compute the remainder of a division using


subtraction.

5. Write an 8051-assembly program to perform bitwise AND, OR, XOR, and NOT on two
numbers.

6. Write an 8051-assembly program to check whether a given number is even or odd.

7. Write an 8051-assembly program to swap two numbers without using a third register.

8. Write an 8051-assembly program to generate a delayed pulse at Port 1.2.

9. Write an 8051-assembly program to implement a counter that counts from 0 to 9


repeatedly.

10. Write an 8051-assembly program to implement an odd number counter from 1 to 15.

11. Write an 8051-assembly program to implement an even number counter from 0 to 14.

12. Write an 8051-assembly program to create a running LED pattern on Port 1.

13. Write an 8051-assembly program to convert a Binary-Coded Decimal (BCD) to Binary.

14. Write an 8051-assembly program to convert a Binary number to BCD format.


SOLUTION

PROGRAM 1: Increment and Decrement a Number

Write an 8051-assembly program to increment and decrement a number stored in a


register.

PROGRAM:

8000: MOV A, #05H


8002: INC A
8004: MOV R0, A
8006: DEC A
8008: DEC A
800A: MOV R1, A
800C: END

EXPLANATION: The program increments the number 05H stored in A and saves it in R0.
It then decrements twice and stores the result in R1.

Input: A = 05H

Solution:

➢ Increment A (A = A + 1) and store in R0


➢ Decrement A twice (A = A - 2) and store in R1

Output: R0 = 06H, R1 = 04H

APPLICATION: Used in counters, loop iterations, and arithmetic operations.

PROGRAM 2: Compare Two Numbers

Write an 8051-assembly program to compare two numbers and set a flag if they are
equal.

PROGRAM:

8000: MOV A, #35H


8002: MOV B, #35H
8004: CLR C
8005: SUBB A, B
8007: JNZ 800B
8009: SETB P1.0
800B: END
EXPLANATION: The program subtracts B from A. If the result is zero, the flag P1.0 is set
to indicate equality.

Input: A = 35H, B = 35H

Solution:

➢ Subtract B from A (A - B)
➢ If result is zero, set flag P1.0

Output: P1.0 is set if A = B

APPLICATION: Used in decision-making, conditional execution, and control applications.

PROGRAM 3: Find the Square of a Number

Write an 8051-assembly program to compute the square of a number using


multiplication.

PROGRAM:

8000: MOV A, #06H


8002: MOV B, A
8004: MUL AB
8006: MOV R0, A
8008: END

EXPLANATION: The number 06H is multiplied by itself, and the result is stored in R0.

Input: A = 06H

Solution:

➢ Multiply A by itself (A * A)
➢ Store result in R0

Output: R0 = 24H (6 × 6)

APPLICATION: Used in power calculations, control systems, and digital signal processing.

PROGRAM 4: Find the Modulus (Remainder) Without Using DIV

Write an 8051-assembly program to compute the remainder of a division using


subtraction.
PROGRAM:

8000: MOV A, #13H


8002: MOV B, #05H
8004: CLR C
8005: SUBB A, B
8007: JNC 8005
8009: MOV R0, A
800B: END

EXPLANATION: The program continuously subtracts B from A until the result is less than
B. The remainder is stored in R0.

Input: A = 13H, B = 05H

Solution:

➢ Repeatedly subtract B from A until A < B


➢ Store the remainder in R0

Output: R0 = 03H (19 ÷ 5 gives remainder 3)

APPLICATION: Used in modular arithmetic, cyclic operations, and encryption algorithms.

PROGRAM 5: Bitwise AND, OR, XOR, and NOT Operations

Write an 8051-assembly program to perform bitwise AND, OR, XOR, and NOT on two
numbers.

PROGRAM:

8000: MOV A, #0F5H


8002: MOV B, #0A3H
8004: ANL A, B
8006: MOV R0, A
8008: MOV A, #0F5H
800A: ORL A, B
800C: MOV R1, A
800E: MOV A, #0F5H
8010: XRL A, B
8012: MOV R2, A
8014: CPL A
8016: MOV R3, A
8018: END
EXPLANATION: Performs AND, OR, XOR, and NOT operations between A and B.

Input: A = 0F5H, B = 0A3H

Solution:

➢ Perform AND, OR, XOR operations on A and B


➢ Perform NOT operation on A (complement bits of A)

Output: R0 = A1H (AND), R1 = F7H (OR), R2 = 56H (XOR), R3 = 0A9H


(NOT)

APPLICATION: Used in logic circuits, encryption, and data masking.

PROGRAM 6: Check If a Number is Even or Odd

Write an 8051-assembly program to check whether a given number is even or odd.

PROGRAM:

8000: MOV A, #0FH


8002: ANL A, #01H
8004: JNZ 8008
8006: SETB P1.0
8008: END

EXPLANATION: The program checks the least significant bit (LSB). If LSB = 1, it's odd;
otherwise, it's even.

Input: A = 0FH

Solution:

➢ Check the least significant bit (LSB)


➢ If LSB = 1, number is odd; otherwise, even

Output: P1.0 is set for even numbers

APPLICATION: Used in parity checking, control algorithms, and digital communication.

PROGRAM 7: Swap Two Numbers Without Using a Third Register

Write an 8051-assembly program to swap two numbers without using an extra register.

PROGRAM:
8000: MOV A, #05H
8002: MOV B, #09H
8004: XRL A, B
8006: XRL B, A
8008: XRL A, B
800A: END
EXPLANATION: XOR swapping technique is used to exchange values without a third
register.

Input: A = 05H, B = 09H

Solution:

➢ Use XOR swapping method: A = A XOR B, B = A XOR B, A = A


XOR B

Output: A = 09H, B = 05H

APPLICATION: Used in memory optimization, cryptographic algorithms, and data


manipulation.

PROGRAM 8: Generate a Delayed Pulse

Write an 8051-assembly program to generate a delayed pulse at Port 1.2.

PROGRAM:

8000: SETB P1.2


8002: MOV R7, #0FFH
8004: DJNZ R7, 8004
8006: CLR P1.2
8008: SJMP 8000

EXPLANATION: A simple delay loop is used to generate a delayed pulse on P1.2.

Input: None

Solution:

➢ Set P1.2 HIGH, introduce a delay, then set it LOW

Output: A periodic pulse is generated at P1.2

APPLICATION: Used in sensor triggering, time-based control, and embedded signal


generation.
PROGRAM 9: Modulo-10 Counter (0-9 Repeating Counter)

Write an 8051-assembly program to implement a counter that counts from 0 to 9


repeatedly.

PROGRAM:

8000: MOV R0, #00H


8002: MOV P1, R0
8004: INC R0
8006: CJNE R0, #0AH, 8002
8008: MOV R0, #00H
800A: SJMP 8002

EXPLANATION: Increments R0 from 00H to 09H, then resets to 00H.

Input: None

Solution:

➢ Increment a counter from 0 to 9


➢ Reset counter to 0 after reaching 9

Output: A continuously repeating count from 0 to 9 on Port 1

APPLICATION: Used in digital counters, loop-based systems, and clock-based


applications.

PROGRAM 10: Odd Number Counter (1, 3, 5, ... 15)

Write an 805- assembly program to implement an odd number counter from 1 to 15.

PROGRAM:

8000: MOV R0, #01H


8002: MOV P1, R0
8004: ADD A, #02H
8006: MOV R0, A
8008: CJNE A, #11H, 8002
800A: MOV R0, #01H
800C: SJMP 8002

EXPLANATION: Increments by 2 to count odd numbers up to 15.

Input: None
Solution:

➢ Start from 1 and increment by 2


➢ Reset to 1 after reaching 15

Output: A continuously repeating count of odd numbers from 1 to 15

APPLICATION: Used in pattern generation, numerical processing, and interval-based


control.

PROGRAM 11: Even Number Counter (0, 2, 4, ... 14)

Write an 8051-assembly program to implement an even number counter from 0 to 14.

PROGRAM:

8000: MOV R0, #00H


8002: MOV P1, R0
8004: ADD A, #02H
8006: MOV R0, A
8008: CJNE A, #10H, 8002
800A: MOV R0, #00H
800C: SJMP 8002

EXPLANATION: Increments by 2 to count even numbers up to 14.

Input: None

Solution:

➢ Start from 0 and increment by 2


➢ Reset to 0 after reaching 14

Output: A continuously repeating count of even numbers from 0 to 14

APPLICATION: Used in data indexing, sequential control, and specific interval-based


systems.

PROGRAM 12: LED Chaser (Running LED Pattern)

Write an 8051-assembly program to create a running LED pattern on Port 1.

PROGRAM:

8000: MOV A, #01H


8002: MOV P1, A
8004: RL A
8006: CJNE A, #00H, 8002
8008: SJMP 8002

EXPLANATION: Rotates an LED pattern to create a chaser effect.

Input: None

Solution:

➢ Rotate an LED bit from left to right

Output: LEDs appear to move from left to right continuously

APPLICATION: Used in decorative lighting, visual indicators, and status displays.

PROGRAM 13: BCD to Binary Conversion

Write an 8051-assembly program to convert a Binary-Coded Decimal (BCD) to Binary.

PROGRAM:

8000: MOV A, #27H


8002: ANL A, #0FH
8004: MOV B, #0AH
8006: MUL AB
8008: MOV R0, A
800A: MOV A, #02H
800C: ADD A, R0
800E: MOV P1, A
8010: SJMP 8010

EXPLANATION: Converts a BCD number (e.g., 27H) into a binary equivalent (27).

Input: A = 27H

Solution:

➢ Extract upper nibble (2) and lower nibble (7)


➢ Multiply upper nibble by 10 and add to lower nibble
(2 * 10 + 7 = 19)

Output: Binary equivalent = 19 displayed on Port 1


APPLICATION: Used in numerical processing, arithmetic operations, and digital
communication.

PROGRAM 14: Binary to BCD Conversion

Write an 805-assembly program to convert a Binary number to BCD format.

PROGRAM:

8000: MOV A, #27H


8002: MOV B, #0AH
8004: DIV AB
8006: MOV R0, A
8008: SWAP A
800A: ORL A, R0
800C: MOV P1, A
800E: SJMP 800E

EXPLANATION: Converts a binary number into a BCD equivalent.

Input: A = 19H

Solution:

➢ Divide number by 10 to get quotient (1) and remainder (9)


➢ Store quotient in upper nibble and remainder in lower nibble
(19H = 0001 1001 in BCD)

Output: BCD equivalent = 19H displayed on Port 1

APPLICATION: Used in numerical displays, calculators, and embedded arithmetic


operations.

You might also like