01 Programming Part I
01 Programming Part I
01 Programming Part I
[8000 H] = 51H
[8001 H] = 19H
## [A] <- 38 H
HLT
-> Solution:
MVI D, 1B H
MVI B, B5 H
INR B
DCR D
MOV A, B
SUB D
OUT 03 H
HLT
[B] = ? -> B6 H
[D] = ? -> 1A H
[Flag] = ? -> 84 H
Solution:
[B] - [C] = B6 H - 1A H
———————————-
Flag: [S] = 1
[Z] = 0
X = 0
[AC] = 0
X = 0
[P] = 1
X = 0
[CY] = 0
-> 9C84 H
Practice Question:
1. Multiplication of two 8 bit numbers.
[4000] = 05H
[4001] = 10H
Store the lower order result at 4002H and higher order result at 4003H
Program:
-> Solution:
0000H: LXI H, 4000H ## Initialize the first memory location to fetch data
0003H: MOV B, M ## Get the first number in B Register [B] <- 05H
0004H: INX H; ## HL = 4001H
0005H: MOV C, M ## Get the second number in C Register [C] <=10H
0006H: MVI A, 00H ## Initialize Accumulator content to 00H
0008H: MVI D, 00H ## D register will be used if the result is more than 8
LOOP: ADD B
JNC NEXT
INR D
NEXT: DCR C
JNZ LOOP
INX H ## HL = 4002
MOV M, A ## [4002] <- [A]
INX H ## HL = 4003
MOV M, D ## [4003] <- [D]
HLT
2. Division of two 8 bit numbers.
[2000] = FFH
[2001] = 05H
PROGRAM:
MVI C, 00H ## For quotient
LXI H, 2000H
MOV B, M
INX H
MOV A, M
LOOP: CMP B
JC END
INR C
SUB B
JMP LOOP
END: INX H
MOV M, A
INX H
MOV M, C
HLT
4. Load the bit pattern 8AH in register D and 49H in register E.
Mask all bits except D3 from register D and E. If D3 has logic 08
in both the register then output the result at PORT1.
Solution:
1000 0000 = 80 H -> [D] [D] <- 8A H
0000 1000 = 08H [ANDed] [E] <- 49H
—————————————
0000 0000 = [A] -> [D] 0000 1000 = [A] -> [D]
ANA D
[Z] SET or RESET
IF [Z] = SET,
OUT PORT1 {NO}
ELSE
OUT PORT1 <- [A]
0000 0100
0000 0001 -> [ANI with 01H]
————————
0000 0000 Result
Zero Flag is SET when given number is EVEN
Zero Flag is RESET when given number is ODD
Program:
5000: LDA 8000H
5003: ANI 01H
5005: JZ EVEN
5008: MVI A, 00H
500A: JMP ODD
[EVEN]500D: MVI A, 01H
[ODD]500F: STA 8100H
5012: HLT
7. Write a program to count the data byte in memory that equal
to 33H starting at memory location 8000H through 800FH. Place
the count value in B register.
MVI E, 00H
LXI H, 8000H
INR E
JNZ LOOP
HLT
8. Write a program to sort given 10 numbers from memory
location 4100H in ascending order.
-> Program:
MVI E, 09H
LXI H, 4100H
START: MOV A, M
MVI C, 09H
LOOP: INX H
CMP M [A] < [M] -> CF SET
JC SWAP_SKIP [A] = [M] -> ZF SET
JZ SWAP_SKIP [A] > [M] -> CF & ZF RESET
## SWAPPING FUCNTION ##
MOV A, M
JMP LOOP
NEW MOV D, M
MOV M, A
DCX H
MOV M, D
INX H
JMP LOOP
END HLT
9. The block of data is stored in memory locations from 8000H to
8005H. Transfer the entire block of data to the locations 8080H to
8085H in reverse order.
Data: 22H, A5H, B2H, 99H, 7FH, 37H
Program:
LXI H, 8000H # Read data from memory
LXI D, 8085H # To store data in memory
MVI C, 06H # Use to count 6 memory location
LOOP: MOV A, M
STAX D
INX H
DCX D
DCR C
JNZ LOOP
HLT # Halt the program