0% found this document useful (0 votes)
7 views

Embedded system program

This document provides a series of simple 8051 assembly programs for basic arithmetic and logical operations, including addition, subtraction, multiplication, and checking for even/odd and positive/negative numbers. Each program is accompanied by explanations and examples to illustrate how they work. The programs utilize various instructions and concepts such as the accumulator, registers, and conditional jumps.

Uploaded by

jhayani2008
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Embedded system program

This document provides a series of simple 8051 assembly programs for basic arithmetic and logical operations, including addition, subtraction, multiplication, and checking for even/odd and positive/negative numbers. Each program is accompanied by explanations and examples to illustrate how they work. The programs utilize various instructions and concepts such as the accumulator, registers, and conditional jumps.

Uploaded by

jhayani2008
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Here's a simple 8051 assembly program to add two 8-bit numbers stored in registers R2 and

R3, and store the result in R4:

8051 Assembly Program:


assembly
Copy
; Program to add two 8-bit numbers in R2 and R3, store result in R4

MOV R4, #0 ; Clear R4 to ensure no residual data


MOV A, R2 ; Move the content of R2 into the Accumulator
(A)
ADD A, R3 ; Add the content of R3 to the Accumulator
MOV R4, A ; Move the result from Accumulator to R4
; The result is now in R4
SJMP $ ; Infinite loop to stop execution

Explanation:

1.​ MOV R4, #0 - Initializes R4 to zero to clear any previous data.​

2.​ MOV A, R2 - Loads the value from R2 into the Accumulator A.​

3.​ ADD A, R3 - Adds the value in R3 to the value in A (accumulator).​

4.​ MOV R4, A - Moves the result from A to R4.​

5.​ SJMP $ - Creates an infinite loop to halt the program.​

Sample Input/Output:

●​ If R2 = 0x12 (18) and R3 = 0x34 (52), the result in R4 will be 0x46 (70).​

●​ If there's a carry (e.g., adding 0xFF + 0x01), the result will be 0x00 in R4, and the carry
flag will be set.​

Let me know if you need modifications, like handling carry flags or specific conditions! 🚀
You said:
more simple

ChatGPT said:

Here's a simpler version of the 8051 assembly program to add two 8-bit numbers from R2 and
R3, and store the result in R4:

Simple 8051 Assembly Program:


assembly
Copy
MOV A, R2 ; Move R2 to Accumulator
ADD A, R3 ; Add R3 to Accumulator
MOV R4, A ; Move result to R4

Explanation:

●​ MOV A, R2 - Loads the value from R2 into the Accumulator A.​

●​ ADD A, R3 - Adds the value from R3 to A.​

●​ MOV R4, A - Stores the result in R4.​

Example:

●​ R2 = 0x25 (37)​

●​ R3 = 0x34 (52)​

●​ Result in R4 = 0x59 (89)​

This is the simplest form without extra instructions. 😊


You said:
Write an 8051 program to subtract an 8-bit number in R1 from another number in R0, and store
the result in R5.

ChatGPT said:

Here's a simple 8051 assembly program to subtract an 8-bit number in R1 from another number
in R0, and store the result in R5:
8051 Assembly Program:
assembly
Copy
MOV A, R0 ; Move the value of R0 to the Accumulator
SUBB A, R1 ; Subtract the value of R1 from the Accumulator (with
borrow)
MOV R5, A ; Store the result in R5

Explanation:

●​ MOV A, R0: Loads the value from R0 into the Accumulator A.​

●​ SUBB A, R1: Subtracts the value of R1 from A, considering the carry/borrow flag (for
multi-byte operations).​

●​ MOV R5, A: Stores the result of the subtraction in R5.​

Example:

●​ R0 = 0x50 (80)​

●​ R1 = 0x30 (48)​

●​ Result in R5 = 0x20 (32)​

This is the simplest form to perform subtraction. Let me know if you need any modifications! 🚀
You said:
Write an 8051 program to multiply two 8-bit numbers stored in R6 and R7, and store the result in
A (lower byte) and B (higher byte).

ChatGPT said:

Here's a simple 8051 assembly program to multiply two 8-bit numbers stored in R6 and R7, and
store the result in A (lower byte) and B (higher byte):

8051 Assembly Program:


assembly
Copy
MOV A, R6 ; Move the first number (R6) to Accumulator A
MOV B, R7 ; Move the second number (R7) to Register B
MUL AB ; Multiply A and B (result: A = lower byte, B =
higher byte)

Explanation:

●​ MOV A, R6: Loads the value from R6 into the Accumulator A.​

●​ MOV B, R7: Loads the value from R7 into B register.​

●​ MUL AB: Multiplies A and B. The result is stored as:​

○​ A = Lower byte of the result​

○​ B = Higher byte of the result​

Example:

●​ R6 = 0x12 (18)​

●​ R7 = 0x08 (8)​

●​ Result:​

○​ A = 0x96 (150) (lower byte)​

○​ B = 0x00 (0) (higher byte)​

Let me know if you'd like more details or explanations! 🚀


You said:
Write an 8051 program to load a number into register A and check if it is zero using the JZ
instruction.

ChatGPT said:

Here's a simple 8051 assembly program to load a number into register A and check if it is zero
using the JZ (Jump if Zero) instruction:

8051 Assembly Program:


assembly
Copy
MOV A, #0x00 ; Load the number 0x00 into Accumulator A
JZ IS_ZERO ; Jump to IS_ZERO label if A is zero

; Code if A is NOT zero


MOV B, #0x01 ; Example: Do something if A is not zero
SJMP END ; Jump to END to skip the zero check part

IS_ZERO:
MOV B, #0x00 ; Example: Do something if A is zero

END:
; Program ends here

Explanation:

●​ MOV A, #0x00: Loads the number 0x00 (zero) into the Accumulator A.​

●​ JZ IS_ZERO: Checks if the Accumulator is zero. If it is, the program jumps to the
IS_ZERO label.​

●​ If A is NOT zero: The program continues to the next instruction (in this case, moves
0x01 to B).​

●​ If A is zero: The program jumps to IS_ZERO and moves 0x00 to B.​

Example:

●​ If A = 0x00 → Jumps to IS_ZERO → B = 0x00​

●​ If A = 0x05 → Does NOT jump → B = 0x01​

Let me know if you'd like any modifications or explanations! 🚀


You said:
simple

ChatGPT said:
Here's a simpler version of the 8051 program to load a number into A and check if it's zero
using the JZ instruction:

Simple 8051 Program:


assembly
Copy
MOV A, #0x00 ; Load 0 into Accumulator A
JZ ZERO ; Jump to ZERO if A is zero

; If A is NOT zero
MOV B, #0x01 ; Do something (example)

SJMP END ; Jump to END to skip ZERO part

ZERO:
MOV B, #0x00 ; Do something if A is zero

END:
; Program ends here

Key Points:

●​ MOV A, #0x00: Loads 0 into A.​

●​ JZ ZERO: Jumps to ZERO if A is zero.​

●​ MOV B, #0x01: Runs if A is NOT zero.​

●​ MOV B, #0x00: Runs if A is zero.​

Simple and easy to follow! 😊


You said:
very simple

ChatGPT said:

Here's the simplest version of the 8051 program:

Super Simple 8051 Program:


assembly
Copy
MOV A, #0 ; Load 0 into A
JZ ZERO ; Jump to ZERO if A is zero

MOV B, #1 ; If not zero, set B to 1


SJMP END ; Skip to END

ZERO:
MOV B, #0 ; If zero, set B to 0

END:
; Done

✅ How It Works:
●​ MOV A, #0 → Load 0 into A.​

●​ JZ ZERO → Jump to ZERO if A is 0.​

●​ MOV B, #1 → If not zero, set B = 1.​

●​ SJMP END → Skip the zero part.​

●​ ZERO: → If zero, set B = 0.​

Super simple and to the point! 🚀


You said:
Write an 8051 program to check if a number stored in A is even or odd using bitwise operations
and the JNC instruction. very simple

ChatGPT said:

Here's a very simple 8051 program to check if the number in A is even or odd using bitwise
operations and the JNC (Jump if No Carry) instruction.

Simple 8051 Program:


assembly
Copy
MOV C, A.0 ; Move the least significant bit (LSB) of A to the
carry flag (C)
JNC EVEN ; Jump to EVEN if the LSB is 0 (number is even)

; If LSB is 1 (number is odd)


MOV B, #1 ; Set B to 1 (odd)

SJMP END ; Skip EVEN part

EVEN:
MOV B, #0 ; Set B to 0 (even)

END:
; Done

✅ How It Works:
●​ MOV C, A.0 → Moves the least significant bit (LSB) of A to the carry flag C.​

●​ JNC EVEN → Jumps to EVEN if C = 0 (number is even).​

●​ If C = 1 (number is odd), it moves 1 to B.​

●​ If even, moves 0 to B.​

Example:

●​ A = 0x02 (even) → B = 0​

●​ A = 0x03 (odd) → B = 1​

Super simple and works like a charm! 🚀


You said:
does odd number have carry=1

ChatGPT said:

Yes, you're absolutely right to ask that! In the context of the 8051 microcontroller, when we talk
about the carry flag (C), it's typically used for operations like addition and subtraction. However,
when we're checking if a number is odd or even using the least significant bit (LSB), we don't
rely on the carry flag directly.

✅ Clarification:
●​ Odd numbers have their LSB = 1, and even numbers have LSB = 0.​

●​ When using MOV C, A.0, the LSB of A is moved to the carry flag (C):​

○​ If A is odd: C = 1​

○​ If A is even: C = 0​

So, in the program:

●​ JNC EVEN jumps to the EVEN label if C = 0 (even number).​

●​ If C = 1 (odd number), it continues to set B = 1.​

🔍 Example:
●​ A = 0x03 (00000011) → LSB = 1 → C = 1 → B = 1 (odd)​

●​ A = 0x04 (00000100) → LSB = 0 → C = 0 → B = 0 (even)​

🚀 Final Program (Correct Version):


assembly
Copy
MOV C, A.0 ; Move the LSB of A to carry flag C
JNC EVEN ; Jump to EVEN if A is even (C = 0)

; If odd
MOV B, #1 ; Set B to 1 (odd)
SJMP END ; Skip EVEN part

EVEN:
MOV B, #0 ; Set B to 0 (even)

END:
; Done

Thanks for pointing that out! 😊


You said:
Write an 8051 program to check if a number stored in A is even or odd using bitwise operations
and the JNC instruction. very simple

ChatGPT said:

Here's a very simple 8051 program to check if the number in A is even or odd using bitwise
operations and the JNC (Jump if No Carry) instruction.

Simple 8051 Program:


assembly
Copy
MOV C, A.0 ; Move the least significant bit (LSB) of A to the
carry flag (C)
JNC EVEN ; Jump to EVEN if the LSB is 0 (number is even)

; If LSB is 1 (number is odd)


MOV B, #1 ; Set B to 1 (odd)

SJMP END ; Skip EVEN part

EVEN:
MOV B, #0 ; Set B to 0 (even)

END:
; Done

✅ How It Works:
●​ MOV C, A.0 → Moves the least significant bit (LSB) of A to the carry flag C.​

●​ JNC EVEN → Jumps to EVEN if C = 0 (number is even).​

●​ If C = 1 (number is odd), it moves 1 to B.​


●​ If even, moves 0 to B.​

Example:

●​ A = 0x02 (even) → B = 0​

●​ A = 0x03 (odd) → B = 1​

This is as simple as it gets! 🚀


You said:
use bitwise

ChatGPT said:

Here’s a very simple 8051 program to check if the number in A is even or odd using bitwise
operations and the JNC instruction.

8051 Assembly Program (Using Bitwise Operations):


assembly
Copy
MOV C, A.0 ; Move the least significant bit (LSB) of A to the
carry flag (C)
JNC EVEN ; Jump to EVEN if A is even (LSB = 0)

; If LSB is 1 (odd)
MOV B, #1 ; Set B to 1 (odd)
SJMP END ; Skip EVEN part

EVEN:
MOV B, #0 ; Set B to 0 (even)

END:
; Done

✅ Explanation:
●​ MOV C, A.0: Moves the least significant bit (LSB) of A into the carry flag (C).​
●​ JNC EVEN: If C = 0 (number is even), it jumps to the EVEN label.​

●​ If the LSB is 1 (odd), it sets B = 1.​

●​ If the LSB is 0 (even), it sets B = 0.​

Example:

●​ A = 0x02 (even) → B = 0​

●​ A = 0x03 (odd) → B = 1​

This program uses bitwise operations with the LSB and JNC for simplicity! 🚀
You said:
Write an 8051 program to check if a number in A is positive or negative using the most
significant bit (MSB).

ChatGPT said:

Here's a simple 8051 assembly program to check if the number in A is positive or negative
using the most significant bit (MSB).

8051 Assembly Program:


assembly
Copy
MOV C, A.7 ; Move the MSB of A to the carry flag (C)
JNC POSITIVE ; Jump to POSITIVE if MSB = 0 (positive number)

; If MSB = 1 (negative number)


MOV B, #0 ; Set B to 0 (negative)

SJMP END ; Skip POSITIVE part

POSITIVE:
MOV B, #1 ; Set B to 1 (positive)

END:
; Done
✅ Explanation:
●​ MOV C, A.7: Moves the most significant bit (MSB) of A into the carry flag (C).​

●​ JNC POSITIVE: Jumps to POSITIVE if C = 0 (MSB = 0, which means the number is


positive).​

●​ If the MSB is 1 (number is negative), it sets B = 0.​

●​ If the MSB is 0 (number is positive), it sets B = 1.​

Example:

●​ A = 0x7F (01111111) → MSB = 0 → B = 1 (positive)​

●​ A = 0x80 (10000000) → MSB = 1 → B = 0 (negative)​

This program is simple and effectively uses the MSB to determine the sign! 🚀
You said:
Write an 8051 program to compare two numbers stored in R3 and R4 and store the larger
number in R5.

ChatGPT said:

Here's a simple 8051 assembly program to compare two numbers stored in R3 and R4, and
store the larger number in R5.

8051 Assembly Program:


assembly
Copy
MOV A, R3 ; Move the first number (R3) to Accumulator A
CJNE A, R4, CHECK ; Compare A with R4, jump to CHECK if not equal

; If A == R4
MOV R5, A ; Both numbers are equal, store A (or R4) in R5
SJMP END ; Skip CHECK part

CHECK:
MOV A, R4 ; Move R4 to Accumulator A for comparison
CJNC STORE ; If carry is clear, R4 is larger, so jump to STORE
; If R3 is larger
MOV R5, R3 ; Store R3 (larger) in R5
SJMP END ; Skip STORE part

STORE:
MOV R5, R4 ; Store R4 (larger) in R5

END:
; Done

✅ Explanation:
1.​ MOV A, R3: Load the first number from R3 into the accumulator A.​

2.​ CJNE A, R4, CHECK: Compare A with R4. If they’re not equal, jump to CHECK.​

3.​ If equal: Move A (or R4) to R5.​

4.​ In CHECK: Move R4 to A and use CJNC STORE to check if R4 is larger (carry clear =
R4 > R3).​

5.​ If R3 is larger: Store R3 in R5.​

6.​ If R4 is larger: Store R4 in R5.​

Example:

●​ R3 = 0x45 (69), R4 = 0x30 (48) → R5 = 0x45 (69)​

●​ R3 = 0x20 (32), R4 = 0x50 (80) → R5 = 0x50 (80)​

●​ R3 = 0x55 (85), R4 = 0x55 (85) → R5 = 0x55 (85)​

This program is straightforward and efficiently compares the two numbers. 🚀


You said:
simple

ChatGPT said:
Here’s a simpler version of the 8051 assembly program to compare two numbers in R3 and R4,
and store the larger number in R5:

Simple 8051 Program:


assembly
Copy
MOV A, R3 ; Load R3 into Accumulator A
CJNE A, R4, CHECK ; Compare A and R4, jump to CHECK if not equal

; If equal
MOV R5, A ; Store A (or R4) in R5
SJMP END ; Skip CHECK part

CHECK:
MOV A, R4 ; Load R4 into A
JC STORE ; If carry is set, R4 is larger, jump to STORE

; If R3 is larger
MOV R5, R3 ; Store R3 in R5
SJMP END ; Skip STORE part

STORE:
MOV R5, R4 ; Store R4 in R5

END:
; Done

✅ How It Works:
1.​ MOV A, R3 → Load R3 into A.​

2.​ CJNE A, R4, CHECK → Compare A with R4. If not equal, jump to CHECK.​

3.​ If equal: Store A in R5.​

4.​ In CHECK:​

○​ MOV A, R4 → Load R4 into A.​


○​ JC STORE → If carry = 1, R4 is larger, jump to STORE.​

5.​ If carry = 0 (R3 is larger), store R3 in R5.​

6.​ In STORE: Store R4 in R5 if R4 is larger.​

Example:

●​ R3 = 0x45 (69), R4 = 0x30 (48) → R5 = 0x45​

●​ R3 = 0x20 (32), R4 = 0x50 (80) → R5 = 0x50​

●​ R3 = 0x55 (85), R4 = 0x55 (85) → R5 = 0x55​

This version is straightforward and easy to understand. 🚀


You said:
simple

ChatGPT said:

Here’s the simplest 8051 program to compare two numbers in R3 and R4, and store the larger
number in R5:

Simple 8051 Program:


assembly
Copy
MOV A, R3 ; Load R3 into A
CJNE A, R4, CHECK ; Compare A and R4, jump if not equal

MOV R5, A ; If equal, store A in R5


SJMP END ; Skip CHECK

CHECK:
MOV A, R4 ; Load R4 into A
JC STORE ; If carry set, R4 is larger, jump to STORE

MOV R5, R3 ; If R3 is larger, store R3 in R5


SJMP END ; Skip STORE

STORE:
MOV R5, R4 ; Store R4 in R5 if it's larger

END:
; Done

✅ How It Works:
1.​ MOV A, R3 → Load R3 into A.​

2.​ CJNE A, R4, CHECK → Compare A with R4. If not equal, jump to CHECK.​

3.​ If equal: Store A in R5.​

4.​ In CHECK:​

○​ MOV A, R4 → Load R4 into A.​

○​ JC STORE → If carry = 1, R4 is larger, jump to STORE.​

5.​ If carry = 0 (R3 is larger), store R3 in R5.​

6.​ In STORE: Store R4 in R5 if R4 is larger.​

Example:

●​ R3 = 0x45 (69), R4 = 0x30 (48) → R5 = 0x45​

●​ R3 = 0x20 (32), R4 = 0x50 (80) → R5 = 0x50​

●​ R3 = 0x55 (85), R4 = 0x55 (85) → R5 = 0x55​

This is as simple as it gets! 🚀


You said:
explain

ChatGPT said:

8051 Program to Compare Two Numbers (R3 & R4) and Store the Larger in
R5
Here’s the program again for reference:

assembly
Copy
MOV A, R3 ; Load R3 into Accumulator A
CJNE A, R4, CHECK ; Compare A and R4, jump to CHECK if not equal

MOV R5, A ; If A == R4, store A (or R4) in R5


SJMP END ; Skip CHECK part

CHECK:
MOV A, R4 ; Load R4 into Accumulator A
JC STORE ; If carry is set, R4 is larger, jump to STORE

MOV R5, R3 ; If R3 is larger, store R3 in R5


SJMP END ; Skip STORE part

STORE:
MOV R5, R4 ; Store R4 in R5 if it's larger

END:
; Done

🚀 Step-by-Step Explanation:
1.​ MOV A, R3​

○​ This moves the value from R3 into the Accumulator (A).​

○​ Purpose: We're going to compare this value with R4.​

2.​ CJNE A, R4, CHECK​

○​ CJNE stands for Compare and Jump if Not Equal.​

○​ It compares A (R3) with R4.​

○​ If they are equal, the program moves to the next instruction (MOV R5, A).​
○​ If they are not equal, it jumps to the CHECK label.​

3.​ MOV R5, A (if equal)​

○​ If R3 == R4, it stores the value in R5.​

○​ Since both numbers are equal, either one can be stored.​

4.​ SJMP END​

○​ Skips the rest of the code because we’ve already found the answer.​

5.​ CHECK: (if not equal)​

○​ MOV A, R4: Now we load R4 into the Accumulator for comparison.​

○​ JC STORE: JC (Jump if Carry) checks the carry flag.​

■​ The carry flag is set if R4 > R3.​

■​ If carry = 1, we jump to STORE because R4 is larger.​

6.​ MOV R5, R3 (if R3 is larger)​

○​ If the carry is not set (meaning R3 is larger), we store R3 in R5.​

○​ SJMP END: Skips the STORE part since we’ve already made the decision.​

7.​ STORE: (if R4 is larger)​

○​ This part runs only if R4 is larger (because of the earlier JC STORE).​

○​ MOV R5, R4 stores the larger number in R5.​

8.​ END:​

○​ Marks the end of the program.​

✅ Example 1:
●​ R3 = 0x45 (69)​

●​ R4 = 0x30 (48)​

Flow:

●​ MOV A, R3 → A = 69​

●​ CJNE A, R4 → Not equal, jump to CHECK​

●​ MOV A, R4 → A = 48​

●​ JC STORE → Carry is 0, so R3 is larger​

●​ MOV R5, R3 → R5 = 69 ✅​

✅ Example 2 (Equal Numbers):


●​ R3 = 0x55 (85)​

●​ R4 = 0x55 (85)​

Flow:

●​ MOV A, R3 → A = 85​

●​ CJNE A, R4 → Equal, so move to MOV R5, A​

●​ MOV R5, A → R5 = 85 ✅​

Key Concepts:

●​ CJNE checks for equality.​

●​ JC checks if the carry flag is set, indicating a larger value.​


●​ SJMP is used to skip unnecessary code.​

Let me know if you'd like more details on any part of this! 🚀


You said:
how c=1 if r4>r3

ChatGPT said:

Great question! Let me clarify how the carry flag (C) works in the context of comparing two
numbers in the 8051 microcontroller.

🎯 How the Carry Flag (C) Works in Comparison:


When using the CJNE instruction (Compare and Jump if Not Equal), the carry flag (C) is
affected based on the comparison result:

●​ CJNE A, R4, CHECK​

○​ Compares A (R3) with R4.​

○​ If R3 > R4, the carry flag is set (C = 1).​

○​ If R3 < R4, the carry flag is clear (C = 0).​

This behavior is due to how the 8051 handles comparisons:

●​ The carry flag acts like a "greater-than" indicator when comparing unsigned numbers.​

●​ If R3 is larger, the carry flag is set because R3 - R4 doesn't cause a borrow (i.e., no
underflow).​

●​ If R4 is larger, the carry flag is clear because a borrow would be needed in subtraction.​

⚡ Simple Example:
●​ R3 = 0x45 (69)​

●​ R4 = 0x30 (48)​

Comparison:
●​ R3 - R4 = 69 - 48 = 21 (no borrow)​

●​ Carry = 1 → R3 is larger​

🚩 Another Example (R4 > R3):


●​ R3 = 0x30 (48)​

●​ R4 = 0x45 (69)​

Comparison:

●​ R3 - R4 = 48 - 69 = -21 (borrow needed)​

●​ Carry = 0 → R4 is larger​

✅ In the Program:
●​ JC STORE → Jumps to STORE if C = 1 (meaning R4 > R3).​

●​ If C = 0 → R3 > R4, and the program stores R3 in R5.​

Let me know if this clears things up! 🚀

You might also like