Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

1.

​ ​INCREMENT A NUMBER 5 TIMES USING INR

CODE:
MVI A,02 ;Loading the accumulator with the number to be incremented
MVI C,05 ;Initializing the counter to 5
LOOP:
INR A
DCR C
JNZ LOOP
STA 2000 ;Storing the final answer HLT

OUTPUT:
The memory location 2000H when examined has the number 07.
2. DECREMENT A NUMBER 5 TIMES USING DCR

CODE:
MVI A,07 ;Loading the accumulator with the number to be decremented
MVI C,05 ;Initializing the counter to 5
LOOP:
DCR A
DCR C
JNZ LOOP
STA 2000 ;Storing the final answer HLT

OUTPUT:
The memory location 2000H when examined has the number 02.
3. ​ADD TWO NUMBERS WITH THE SUM WHOSE RESULT WILL
NOT EXCEED THE PROCESSOR RANGE (IGNORE THE CARRY)

CODE:
LDA 2150 ;Loading the accumulator with the first number
MOV B, A ;Storing the number in register B
LDA 2151 ;Loading the accumulator with the second number
ADD B ;Adding the first number to the second number
STA 2152 ;Storing the final answer
HLT

OUTPUT:
Input: 02 (2150) 06 (2251)
Examining the memory locations where the program stores the result, the
following is observed:
08 (2152)
4.​ ​SUBTRACT TWO NUMBERS WHOSE RESULT WILL BE
POSITIVE AND WITHIN THE RANGE OF THE PROCESSOR

CODE:
LDA 2150 ;Loading the accumulator with the first number
MOV B, A ;Storing the number in register B
LDA 2151 ;Loading the accumulator with the second number
SUB B ;Subtracting the first number from the second number
STA 2152 ;Storing the final answer
HLT

OUTPUT:
Input: 02 (2150) 06 (2251)
Examining the memory locations where the program stores the result, the
following is observed:
04 (2152)
5. ​ADD TWO NUMBERS AFFECTING OR NOT AFFECTING THE
CARRY FLAG

CODE:
MVI C, 00 ;Clearing the register C to store carry
LDA 2150 ;Loading the accumulator with the first number
MOV B, A ;Storing the number in register B
LDA 2151 ;Loading the accumulator with the second number
ADD B ;Adding the first number to the second number
JNC LOOP ;Jump on no carry.
INR C ;Incrementing the value of register C i.e. carry LOOP:
STA 2152 ;Storing the final answer
MOV A, C ;Moving content of register C to Accumulator
STA 2153 ;Storing the carry
HLT

OUTPUT:
Input: 80 (2150) 80 (2151)
Examining the memory locations where the program stores the result, the
following is observed:
00 (2152) 01 (2153)
6. ​SUBTRACT TWO NUMBERS AFFECTING CARRY FLAG

CODE:
MVI C, 00 ;Clearing the register C to store borrow
LDA 2150 ;Loading the accumulator with the first number
MOV B, A ;Storing the number in register B
LDA 2151 ;Loading the accumulator with the second number
SUB B ;Subtracting the first number from the second number
JNC NEXT;Jump on no carry
CMA ;Complementing the accumulator contents
INR A ;Incrementing value in the accumulator
INR C ;Incrementing the value in register C
NEXT:
STA 2152 ;Storing the final answer
MOV A, C ;Moving content of register C to Accumulator
STA 2153 ;Storing the carry
HLT

OUTPUT:
Input: 06 (2150) 02 (2151)
Examining the memory locations where the program stores the result, the
following is observed:
04 (2152) 01 (2153)
7. ​CHECK IF A NUMBER IS EQUAL TO 10 OR NOT

CODE:
MVI C,00 ;Resetting register C to indicate result
MVI B,0A ;Loading register B with the value 10
LDA 2150 ;Loading the accumulator with the number to be compared
CMP B ;Comparing the number with 10
JZ EQL ;Register C holds 0 if the number is equal to 10
INR C ;Register C holds 1 if the number is not equal to 10
EQL:
MOV A,C ;Moving the indicator value to accumulator
STA 2151 ;Storing the final answer
HLT

OUTPUT:
1. Input:06 (2150)
Examining the memory locations where the program stores the result, the
following is observed:
01 (2151)
2. Input:0A (2150)
Examining the memory locations where the program stores the result, the
following is observed:
00 (2151)
8. ​CHECK IF A NUMBER IS LESS THAN, EQUAL TO OR
GREATER THAN 10

CODE:
MVI C,00 ;Resetting register C to indicate result
MVI B,0A ;Loading register B with the value 10
LDA 2150 ;Loading the accumulator with the number to be compared
CMP B ;Comparing the number with 10
JZ DSPLY ;Register C holds 0 if the number is equal to 10
INR C ;Incrementing register C
JC DSPLY ;Register C holds 1 if the number is smaller than 10
INR C ;Register C holds 2 if the number is greater than 10
DSPLY:
MOV A,C ;Moving the indicator value to accumulator
STA 2151 ;Storing the final answer
HLT

OUTPUT:
1. Input:06 (2150)
Examining the memory locations where the program stores the result, the
following is observed:
01 (2151)
2. Input:0A (2150)
Examining the memory locations where the program stores the result, the
following is observed:
00 (2151)
3. Input:0C (2150)
Examining the memory locations where the program stores the result, the
following is observed:
02 (2151)
9. ​CHECK IF 2 NUMBERS ARE EQUAL OR NOT

CODE:
MVI C,00 ;Resetting register C to indicate result
LDA 2150 ;Loading the accumulator with the first number
MOV B,A ;Moving the first number to register B
LDA 2151 ;Loading the accumulator with the second number
CMP B ;Comparing the numbers
JZ EQL ;Register C holds 0 if the numbers are equal
INR C ;Register C holds 1 if the numbers are not equal
EQL:
MOV A,C ;Moving the indicator value to accumulator
STA 2152 ;Storing the final answer
HLT

OUTPUT:

1. Input:06 (2150) 06 (2151)


Examining the memory locations where the program stores the result, the
following is observed:
00 (2152)
2. Input:06 (2150) 02 (2151)
Examining the memory locations where the program stores the result, the
following is observed:
01 (2152)
10. ​FIND THE GREATEST OF THREE GIVEN NUMBERS

CODE:
LDA 2050 ;Loading the accumulator with the first number
MOV B,A ;Copying the first number to register B
LDA 2051 ;Loading the accumulator with the second number
MOV C,A ;Copying the second number to register C
LDA 2052 ;Loading the accumulator with the third number
CMP B ;Comparing Register B with A
JNC NXT1 ;Jump if A>=B
MOV A,B ;Else copy B to A
NXT1:
CMP C ;Comparing Register C with A
JNC NXT2 ;Jump if A>=C
MOV A,C ;Else copy C to A
NXT2:
STA 2053 ;Storing the final answer
HLT

OUTPUT:

Input: 06 (2050)
0A (2051) 02 (2052)
Examining the memory locations where the program stores the result, the
following is observed:
0A (2053)
11. ​SEARCH AN ELEMENT IN AN ARRAY OF 10 ELEMENTS

CODE:
MVI C,0A ;Loading Register C with the count 10(size of array)
MVI D,00 ;Loading Register D with 0 to indicate search successful/failed
MVI B,02 ;Loading Register B with the number to be searched( i.e. 2 here)
LXI H,2400 ;Loading the HL Register pair with the starting position of array
LOOP:
MOV A,M ;Loading the accumulator with the first number of the array
INX H ;Incrementing to the next array location
CMP B ;Comparing the value to be found(Register B) with current array element
JZ FND ;Breaking from the loop if element is found
DCR C ;Decrementing count of array elements left
JNZ LOOP ;Looping till count is 0
INR D ;Register D has 1 to indicate search failure and 0 to indicate success
FND:
MOV A,D ;Moving the final answer to the accumulator
STA 2000 ;Storing the final answer
HLT

OUTPUT:
Input: 06 (2400)
0A (2401) 02 (2402) 04 (2403) 01 (2404) 03 (2405) 0B (2406) 00 (2407) 07
(2408) 05 (2409)
Examining the memory locations where the program stores the result, the
following is observed:
00 (2000) (Indicating that the value 02 is present in the array)
12. ​FIND THE SUM OF 10 ELEMENTS OF AN ARRAY

CODE:
MVI C,00 ;
Loading Register C with initial carry ( i.e. 0)
MVI D,0A ;Loading Register D with the size of the array
MVI A,00 ;Loading the accumulator with the initial sum ( i.e. 0)
LXI H,2400 ;Loading the HL Register pair with the starting position of array
LOOP:
MOV B,M ;Loading Register B with the first number of the array
INX H ;Incrementing to the next array location
ADD B ;Adding the array element to current sum
JNC NXT ;If no carry is generated, leave Register C unchanged
INR C ;Increment Register C to indicate Carry
NXT:
DCR D ;Decrement count of array elements left
JNZ LOOP ;Check for end of array
MOV A,B ;Moving the final answer to the accumulator
STA 2000 ;Storing the sum
MOV A,C ;Moving the carry value to the accumulator
STA 2001 ;Storing the carry
HLT

OUTPUT:
Input: 06 (2400)
0A (2401) 02 (2402) 04 (2403) 01 (2404) 03 (2405) 0B (2406) 00 (2407) 07
(2408) 05 (2409)
Examining the memory locations where the program stores the result, the
following is observed:
31 (2000) (SUM) 00 (2001) (CARRY)
13. ​YOU ARE GIVEN AN ARRAY OF 25/( ODD NUMBER OF
)ELEMENTS WITH EVERY NUMBER REPEATED EVEN
NUMBER OF TIMES EXCEPT A SINGLE NUMBER. FIND THAT
NUMBER.

CODE:

MVI C,09 ;Loading Register C with the number of array elements(any odd
number)
MVI A,00 ;Clearing the accumulator to store the result
LXI H,2400 ;Loading the HL Register pair with the starting position of array
LOOP:
MOV B,M ;Loading the Register B with the first number of the array
XOR B ;Xoring the array element to current value
INX H ;Incrementing to the next array location
DCR C ;Decrement count of array elements left
JNZ LOOP ;Check for end of array
STA 2000 ;Storing the final answer
HLT
OUTPUT:
Input: 06 (2400) 06 (2401) 02 (2402) 02 (2403) 06 (2404) 06 (2405) 04 (2406) 01
(2407) 04 (2408)
Examining the memory locations where the program stores the result, the
following is observed:
01 (2000)

You might also like