Store 8-Bit Data in Memory
Store 8-Bit Data in Memory
Statement: Store the data byte 32H into memory location 4000H.
Program 1:
MVI A, 52H : Store 32H in the accumulator
STA 4000H : Copy accumulator contents at address 4000H
HLT : Terminate program execution
Program 2:
LXI H : Load HL with 4000H
MVI M : Store 32H in memory location pointed by HL register pair (4000H)
HLT : Terminate program execution
The result of both programs will be the same. In program 1 direct addressing instruction is used, whereas in program 2
indirect addressing instruction is used.
Exchange the contents of memory locations
Statement: Exchange the contents of memory locations 2000H and 4000H
Program 1:
LDA 2000H : Get the contents of memory location 2000H into accumulator
MOV B, A : Save the contents into B register
LDA 4000H : Get the contents of memory location 4000Hinto accumulator
STA 2000H : Store the contents of accumulator at address 2000H
MOV A, B : Get the saved contents back into A register
STA 4000H : Store the contents of accumulator at address 4000H
Program 2:
LXI H 2000H : Initialize HL register pair as a pointer to memory location 2000H.
LXI D 4000H : Initialize DE register pair as a pointer to memory location 4000H.
MOV B, M : Get the contents of memory location 2000H into B register.
LDAX D : Get the contents of memory location 4000H into A register.
MOV M, A : Store the contents of A register into memory location 2000H.
MOV A, B : Copy the contents of B register into accumulator.
STAX D : Store the contents of A register into memory location 4000H.
HLT : Terminate program execution.
In Program 1, direct addressing instructions are used, whereas in Program 2, indirect addressing
instructions are used.
Add two 8-bit numbers
Statement: Add the contents of memory locations 4000H and 4001H and place the result in memory location 4002H.
Sample problem
(4000H) = 14H
(4001H) = 89H
Result = 14H + 89H = 9DH
Source program
LXI H 4000H : HL points 4000H
MOV A, M : Get first operand
INX H : HL points 4001H
ADD M : Add second operand
INX H : HL points 4002H
MOV M, A : Store result at 4002H
HLT : Terminate program execution
Subtract two 8-bit numbers
Statement: Subtract the contents of memory location 4001H from the memory location 2000H and place the result in
memory location 4002H.
Program - 4: Subtract two 8-bit numbers
Sample problem:
(4000H) = 51H
(4001H) = 19H
Result = 51H - 19H = 38H
Source program:
LXI H, 4000H : HL points 4000H
MOV A, M : Get first operand
INX H : HL points 4001H
SUB M : Subtract second operand
INX H : HL points 4002H
MOV M, A : Store result at 4002H.
HLT : Terminate program execution
Finding one's complement of a number
Statement: Find the l's complement of the number stored at memory location 4400H and store the complemented number
at memory location 4300H.
Sample problem:
(4400H) = 55H
Result = (4300B) = AAB
Source program:
LDA 4400B : Get the number
CMA : Complement number
STA 4300H : Store the result
HLT : Terminate program execution
Finding Two's complement of a number
Statement: Find the 2's complement of the number stored at memory location 4200H and store the complemented
number at memory location 4300H.
Sample problem:
(4200H) = 55H
Result = (4300H) = AAH + 1 = ABH
Source program:
LDA 4200H : Get the number
CMA : Complement the number
ADI, 01H : Add one in the number
STA 4300H : Store the result
HLT : Terminate program execution
Pack the unpacked BCD numbers
Statement: Pack the two unpacked BCD numbers stored in memory locations 4200H and 4201H and store result in memory
location 4300H. Assume the least significant digit is stored at 4200H.
Sample problem:
(4200H) = 04
(4201H) = 09
Result = (4300H) = 94
Source program
LDA 4201H : Get the Most significant BCD digit
RLC
RLC
RLC
RLC : Adjust the position of the second digit (09 is changed to 90)
ANI FOH : Make least significant BCD digit zero
MOV C, A : store the partial result
LDA 4200H : Get the lower BCD digit
ADD C : Add lower BCD digit
STA 4300H : Store the result
HLT : Terminate program execution
NOTE:
BCD NO.: The numbers "0 to 9" are called BCD (Binary Coded Decimal) numbers. A decimal
number 29 can be converted into BCD number by splitting it into two. ie. 02 and 09.
Unpack a BCD number
Statement: Two digit BCD number is stored in memory location 4200H. Unpack the BCD number and store the two digits in
memory locations 4300H and 4301H such that memory location 4300H will have lower BCD digit.
Sample problem
(4200H) = 58
Result = (4300H) = 08 and
(4301H) = 05
Source program
LDA 4200H : Get the packed BCD number
ANI FOH : Mask lower nibble
RRC
RRC
RRC
RRC : Adjust higher BCD digit as a lower digit
STA 4301H : Store the partial result
LDA 4200H : .Get the original BCD number
ANI OFH : Mask higher nibble
STA 4201H : Store the result
HLT : Terminate program execution
Execution format of instructions
Statement: Read the program given below and state the contents of all registers after the execution of each instruction in
sequence.
Main program:
4000H LXI SP, 27FFH
4003H LXI H, 2000H
4006H LXI B, 1020H
4009H CALL SUB
400CH HLT
Subroutine program:
4100H SUB: PUSH B
4101H PUSH H
4102H LXI B, 4080H
4105H LXI H, 4090H
4108H SHLD 2200H
4109H DAD B
410CH POP H
410DH POP B
410EH RET
Note:
The table given gives the instruction sequence and the contents of all registers and stack
after execution of each instruction.
Right shift bit of data
Statement: Write a program to shift an eight bit data four bits right. Assume that data is in register C.
Source program:
MOV A, C
RAR
RAR
RAR
RAR
MOV C, A
HLT
Statement: Write a program to shift a 16 bit data, 1 bit right. Assume that data is in BC register pair.
Source program:
MOV A, B
RAR
MOV B, A
MOV A, C
RAR
MOV C, A
HLT
Left Shifting of a 16-bit data
Statement: Program to shift a 16-bit data 1 bit left. Assume data is in the HL register pair.
Source program:
DAD H : Adds HL data with HL data
Alter the contents of flag register in 8085
Statement: Write a set of instructions to alter the contents of flag register in 8085.
PUSH PSW : Save flags on stack
POP H : Retrieve flags in 'L'
MOV A, L : Flags in accumulator
CMA : Complement accumulator
MOV L, A : Accumulator in 'L'
PUSH H : Save on stack
POP PSW : Back to flag register
HLT :Terminate program execution
Calculate the sum of series of numbers
Statement: Calculate the sum of series of numbers. The length of the series is in memory location 4200H and the series
begins from memory location 4201H.
a. Consider the sum to be 8 bit number. So, ignore carries. Store the sum at memory location 4300H.
b. Consider the sum to be 16 bit number. Store the sum at memory locations 4300H and 4301H.
a. Sample problem
4200H = 04H
4201H = 10H
4202H = 45H
4203H = 33H
4204H = 22H
Result = 10 +41 + 30 + 12 = H
4300H = H
Source program:
LDA 4200H
MOV C, A : Initialize counter
SUB A : sum = 0
LXI H, 420lH : Initialize pointer
BACK: ADD M : SUM = SUM + data
INX H : increment pointer
DCR C : Decrement counter
JNZ BACK : if counter 0 repeat
STA 4300H : Store sum
HLT : Terminate program execution
b. Sample problem
4200H = 04H
420lH = 9AH
4202H = 52H
4203H = 89H
4204H = 3EH
Result = 9AH + 52H + 89H + 3EH = H
4300H = B3H Lower byte
4301H = 0lH Higher byte
Source program:
LDA 4200H
MOV C, A : Initialize counter
LXI H, 4201H : Initialize pointer
SUB A :Sum low = 0
MOV B, A : Sum high = 0
BACK: ADD M : Sum = sum + data
JNC SKIP
INR B : Add carry to MSB of SUM
SKIP: INX H : Increment pointer
DCR C : Decrement counter
JNZ BACK : Check if counter 0 repeat
STA 4300H : Store lower byte
MOV A, B
STA 4301H : Store higher byte
HLT :Terminate program execution
Divide a 16 bit number by a 8-bit number
Statement:Divide 16 bit number stored in memory locations 2200H and 2201H by the 8 bit number stored at memory
location 2202H. Store the quotient in memory locations 2300H and 2301H and remainder in memory locations 2302H and
2303H.
Sample problem
(2200H) = 60H
(2201H) = A0H
(2202H) = l2H
Result = A060H/12H = 8E8H Quotient and 10H remainder
(2300H) = E8H
(2301H) = 08H
(2302H= 10H
(2303H) 00H
Source program
LHLD 2200H : Get the dividend
LDA 2202H : Get the divisor
MOV C, A
LXI D, 0000H : Quotient = 0
BACK: MOV A, L
SUB C : Subtract divisor
MOV L, A : Save partial result
JNC SKIP : if CY 1 jump
DCR H : Subtract borrow of previous subtraction
SKIP: INX D : Increment quotient
MOV A, H
CPI, 00 : Check if dividend < divisor
JNZ BACK : if no repeat
MOV A, L
CMP C
JNC BACK
SHLD 2302H : Store the remainder
XCHG
SHLD 2300H : Store the quotient
HLT : Terminate program execution
Find the negative numbers in a block of data.
Statement:Find the number of negative elements (most significant bit 1) in a block of data. The length of the block is in
memory location 2200H and the block itself begins in memory location 2201H. Store the number of negative elements in
memory location 2300H
Sample problem
(2200H) = 04H
(2201H) = 56H
(2202H) = A9H
(2203H) = 73H
(2204H) = 82H
Result = 02 since 2202H and 2204H contain numbers with a MSB of 1.
Source program
LDA 2200H
MOV C, A : Initialize count
MVI B, 00 : Negative number = 0
LXI H, 2201H : Initialize pointer
BACK: MOV A, M : Get the number
ANI 80H : Check for MSB
JZ SKIP : If MSB = 1
INR B : Increment negative number count
SKIP: INX H : Increment pointer
DCR C : Decrement count
JNZ BACK : If count 0 repeat
MOV A, B
STA 2300H : Store the result
HLT : Terminate program execution
Find the largest of given numbers
Statement:Find the number of negative elements (most significant bit 1) in a block of data. The length of the block is in
memory location 2200H and the block itself begins in memory location 2201H. Store the number of negative elements in
memory location 2300H
Sample problem
(2200H) = 04H
(2201H) = 56H
(2202H) = A9H
(2203H) = 73H
(2204H) = 82H
Result = 02 since 2202H and 2204H contain numbers with a MSB of 1.
Source program
LDA 2200H
MOV C, A : Initialize count
MVI B, 00 : Negative number = 0
LXI H, 2201H : Initialize pointer
BACK: MOV A, M : Get the number
ANI 80H : Check for MSB
JZ SKIP : If MSB = 1
INR B : Increment negative number count
SKIP: INX H : Increment pointer
DCR C : Decrement count
JNZ BACK : If count 0 repeat
MOV A, B
STA 2300H : Store the result
HLT : Terminate program execution
Calculate the sum of series of even numbers
Statement:Calculate the sum of series of even numbers from the list of numbers. The length of the list is in memory
location 2200H and the series itself begins from memory location 2201H. Assume the sum to be 8 bit number so you can
ignore carries and store the sum at memory location 2210H.
Sample problem:
2200H= 4H
2201H= 20H
2202H= l5H
2203H= l3H
2204H= 22H
Result 22l0H= 20 + 22 = 42H
= 42H
Source program:
LDA 2200H
MOV C, A : Initialize counter
MVI B, 00H : sum = 0
LXI H, 2201H : Initialize pointer
BACK: MOV A, M : Get the number
ANI 0lH : Mask Bit1 to Bit7
JNZ SKIP : Don't add if number is ODD
MOV A, B : Get the sum
ADD M : SUM = SUM + data
MOV B, A : Store result in B register
SKIP: INX H : increment pointer
DCR C : Decrement counter
JNZ BACK : if counter 0 repeat
STA 2210H : store sum
HLT : Terminate program execution
Inserting string in a given array of characters
Statement:Write an 8085 assembly language program to insert a string of four characters from the tenth location in the
given array of 50 characters.
Solution:
Step 1: Move bytes from location 10 till the end of array by four bytes downwards.
Step 2: Insert four bytes at locations 10, 11, 12 and 13.
Source Program:
LXI H, 2l31H : Initialize pointer at the last location of array.
LXI D, 2l35H : Initialize another pointer to point the last location of array after
insertion.
AGAIN: MOV A, M : Get the character
STAX D : Store at the new location
DCX D : Decrement destination pointer
DCX H : Decrement source pointer
MOV A, L : [check whether desired
CPI 05H bytes are shifted or not]
JNZ AGAIN : if not repeat the process
INX H : adjust the memory pointer
LXI D, 2200H : Initialize the memory pointer to point the string to be inserted
REPE: LDAX D : Get the character
MOV M, A : Store it in the array
INX D : Increment source pointer
INX H : Increment destination pointer
MOV A, E : [Check whether the 4 bytes
CPI 04 are inserted]
JNZ REPE : if not repeat the process
HLT : stop
Deleting string in a given array of characters
Statement:Write an 8085 assembly language program to delete a string of 4 characters from the tenth location in the given
array of 50 characters.
Solution: Shift bytes from location 14 till the end of array upwards by 4 characters i.e. from
location 10 onwards.
Source Program:
LXI H, 2l0DH :Initialize source memory pointer at the 14thlocation of the array.
LXI D, 2l09H : Initialize destn memory pointer at the 10th location of the array.
MOV A, M : Get the character
STAX D : Store character at new location
INX D : Increment destination pointer
INX H : Increment source pointer
MOV A, L : [check whether desired
CPI 32H bytes are shifted or not]
JNZ REPE : if not repeat the process
HLT : stop
Multiply two eight bit numbers with shift and add method
Statement:Multiply the 8-bit unsigned number in memory location 2200H by the 8-bit unsigned number in memory
location 2201H. Store the 8 least significant bits of the result in memory location 2300H and the 8 most significant bits in
memory location 2301H.
Sample problem:
(2200) = 1100 (0CH)
(2201) = 0101 (05H)
Multiplicand = 1100 (1210)
Multiplier = 0101 (510)
Result = 12 x 5 = (6010)
Source program
LXI H, 2200 : Initialize the memory pointer
MOV E, M : Get multiplicand
MVI D, 00H : Extend to 16-bits
INX H : Increment memory pointer
MOV A, M : Get multiplier
LXI H, 0000 : Product = 0
MVI B, 08H : Initialize counter with count 8
MULT: DAD H : Product = product x 2
RAL
JNC SKIP : Is carry from multiplier 1 ?
DAD D : Yes, Product =Product + Multiplicand
SKIP: DCR B : Is counter = zero
JNZ MULT : no, repeat
SHLD 2300H : Store the result
HLT : End of program
Divide 16-bit number with 8-bit number using shifting technique
Statement:Divide the 16-bit unsigned number in memory locations 2200H and 2201H (most significant bits in 2201H) by
the B-bit unsigned number in memory location 2300H store the quotient in memory location 2400H and remainder in
2401H.
Assumption: The most significant bits of both the divisor and dividend are zero.
Source program
MVI E, 00 : Quotient = 0
LHLD 2200H : Get dividend
LDA 2300 : Get divisor
MOV B, A : Store divisor
MVI C, 08 : Count = 8
NEXT: DAD H : Dividend = Dividend x 2
MOV A, E
RLC
MOV E, A : Quotient = Quotient x 2
MOV A, H
SUB B : Is most significant byte of Dividend > divisor
JC SKIP : No, go to Next step
MOV H, A : Yes, subtract divisor
INR E : and Quotient = Quotient + 1
SKIP:DCR C : Count = Count - 1
JNZ NEXT : Is count =0 repeat
MOV A, E
STA 2401H : Store Quotient
Mov A, H
STA 2410H : Store remainder
HLT : End of program.
Flowchart for subroutine:
Split a HEX data into two nibbles and store it
Statement:Write a simple program to Split a HEX data into two nibbles and store it in memory
Source Program:
LXI H, 4200H : Set pointer data for array
MOV B,M : Get the data in B-reg
MOV A,B : Copy the data to A-reg
ANI OFH : Mask the upper nibble
INX H : Increment address as 4201
MOV M,A : Store the lower nibble in memory
MOV A,B : Get the data in A-reg
ANI FOH : Bring the upper nibble to lower nibble position
RRC
RRC
RRC
RRC
INX H
MOV M,A : Store the upper nibble in memory
HLT : Terminate program execution
Add two 4-digit BCD numbers
Statement: Add two 4 digit BCD numbers in HL and DE register pairs and store result in memory locations, 2300H and
2301H. Ignore carry after 16 bit.
Sample Problem:
(HL) =3629
(DE) =4738
Step 1 : 29 + 38 = 61 and auxiliary carry flag = 1
:.add 06
61 + 06 = 67
Step 2 : 36 + 47 + 0 (carry of LSB) = 7D
Lower nibble of addition is greater than 9, so add 6.
7D + 06 = 83
Result = 8367
Source program
MOV A, L : Get lower 2 digits of no. 1
ADD E : Add two lower digits
DAA : Adjust result to valid BCD
STA 2300H : Store partial result
MOV A, H : Get most significant 2 digits of number
ADC D : Add two most significant digits
DAA : Adjust result to valid BCD
STA 2301H : Store partial result
HLT : Terminate program execution.
Subtraction of two BCD numbers
Statement: Subtract the BCD number stored in E register from the number stored in the D register.
Source Program:
MVI A,99H
SUB E : Find the 99's complement of subtrahend
INR A : Find 100's complement of subtrahend
ADD D : Add minuend to 100's complement of subtrahend
DAA : Adjust for BCD
HLT : Terminate program execution
Multiply two 2-digit BCD numbers
Statement: Write an assembly language program to multiply 2 BCD numbers
Source Program:
MVI C, Multiplier : Load BCD multiplier
MVI B, 00 : Initialize counter
LXI H, 0000H : Result = 0000
MVI E, multiplicand : Load multiplicand
MVI D, 00H : Extend to 16-bits
BACK: DAD D : Result Result + Multiplicand
MOV A, L : Get the lower byte of the result
ADI, 00H
DAA : Adjust the lower byte of result to BCD.
MOV L, A : Store the lower byte of result
MOV A, H : Get the higher byte of the result
ACI, 00H
DAA : Adjust the higher byte of the result to BCD
MOV H, A : Store the higher byte of result.
MOV A, B : [Increment
ADI 01H : counter
DAA : adjust it to BCD and
MOV B,A : store it]
CMP C : Compare if count = multiplier
JNZ BACK : if not equal repeat
HLT : Stop
Generate and display binary up counter
Statement:Write a program for displaying binary up counter. Counter should count numbers from 00 to FFH and it should
increment after every 0.5 sec.
Assume operating frequency of 8085 equal to 2MHz. Display routine is available.
Source Program:
LXI SP, 27FFH : Initialize stack pointer
MVI C, OOH : Initialize counter
BACK: CALL Display : Call display subroutine
CALL Delay : Call delay subroutine
INR C : Increment counter
MOV A, C
CPI OOH : Check counter is > FFH
JNZ BACK : If not, repeat
HLT : Stop
Delay Subroutine:
Delay: LXI B, count : Initialize count
BACK: DCX D : Decrement count
MOV A, E
ORA D : Logically OR D and E
JNZ BACK : If result is not 0 repeat
RET : Return to main program
Flowchart for Source Program:
Generate and display BCD up counter with frequency 1Hz
Statement:Write a program for displaying BCD up counter. Counter should count numbers from 00 to 99H and it should
increment after every 1 sec. Assume operating frequency of 8085 equal to 3MHz. Display routine is available.
Source Program:
LXI SP, 27FFH : Initialize stack pointer
MVI C, OOH : Initialize counter
BACK: CALL Display : Call display subroutine
CALL Delay : Call delay subroutine
MOV A, C
ADI A, 0 1 : Increment counter
DAA : Adjust it for decimal
MOV C,A : Store count
CPI ,00 : Check count is > 99
JNZ BACK : If not, repeat
HLT : Stop
Delay Subroutine:
Delay:MVI B, Multiplier-count : Initialize multiplier count
BACK 1:LXI D, Initialize Count
BACK: DCX D : Decrement count
MOV A, E
ORA D : Logically OR D and E
JNZ BACK : If result is not a, repeat
DCR B : Decrement multiplier count
JNZ BACK 1 : If not zero, repeat
RET : Return to main program.
Operating Frequency : 3MHz
Flowchart for Source Program:
Flowchart for Delay routine:
Generate and display BCD down counter
Statement:Write a program for displaying BCD down counter. Counter should count numbers from 99 to 00 and it should
increment after every 1 sec. Assume operating frequency of 8085 equal to 3MHz. Display routine is available.
Source Program 1:
LXI SP, 27FFH : Initialize stack pointer
MVI C, 99H : Initialize counter = 99
BACK:CALL Display : Call display subroutine
CALL Delay : Call delay subroutine
ADI 99H : See Addition below
DAA : Adjust for decimal
CPI 99H : Compare with last count
JNZ BACK :If no, repeat
HLT
Source Program2:
LXI SP, 27FFH : Initialize stack pointer
MVI C, 99H : Initialize counter = 99
BACK: CALL Display : Call display subroutine
CALL Delay : Call delay subroutine
MOV A, C : Get count
ANI 0FH : Check for lower nibble
JNZ SKIP : If it is not 0FH go to skip
MOV A,C : Else get the count
SBI ,06 : Subtract 06
MOV C,A : Store the count
DCR C : Decrement count
CPI 99H : Check it for last count
JNZ BACK : If not, repeat
HLT : Stop
Generate and display the contents of decimal counter
Statement:Write assembly language program to with proper comments for the following: To display decimal decrementing
counter (99 to 00) at port 05 H with delay of half seconds between .each count. Write as well the delay routine giving delay
of half seconds. Operating frequency of microprocessor is 3.072 MHz. Neglect delay for the main program.
Source Program:
MVI C, 99H : Initialize counter
BACK: MOV A, C
ANI OF : Mask higher nibble
CPI OF
JNZ SKIP
MOV A, C
SUI 06 : Subtract 6 to adjust decimal count
MOV D, A
SKIP: MOV A, C
OUT 05 : send count on output port
CALL Delay : Wait for 0.5 seconds
DCR C : decrement count
MOV A, C
CPI FF
JNZ BACK : If not zero, repeat
HLT : Stop execution
Delay subroutine:
Delay: LXI D, Count
Back: DCX D : 6 T-states
MOV A, D : 4 T-states
ORA E : 4 T-states
JNZ Back : 10 T-states
RET
Debug the delay routine
Statement:The delay routine given below is in infinite loop, identify the error and correct the program.
Delay routine with error:
DELAY : LXI H, N
L1 : DCX H
JNZ L1
Sol.: 1) The fault in the above program is at instruction JNZ L1. This condition always evaluates to
be true hence loops keep on executing and hence infinite loop.
2) Reason for infinite looping: - The instruction DCX H decrease the HL pair count one by one but
it does not affect the zero flag. So when count reaches to OOOOH in HL pair zero flag is not
affected and JNZ L1 evaluates to be true and loop continues. Now HL again decrements below
OOOOH and HL becomes FFFFH and thus execution continues.
3) The modification in the program is as follows:
DELAY : LXI H, N :Load 16 bit count
L1 : DCX H : Decrement count
MOV A, L
ORA H : logically OR Hand L
JNZ L1 : If result is not 0 repeat
PROGRAMS IN CODE CONVERSION
2-Digit BCD to binary conversion.
Statement: Convert a 2-digit BCD number stored at memory address 2200H into its binary equivalent number and store the
result in a memory location 2300H.
Sample Problem
(2200H) = 67H
(2300H) = 6 x OAH + 7 = 3CH + 7 = 43H
Source Program:
LDA 2200H : Get the BCD number
MOV B, A : Save it
ANI OFH : Mask most significant four bits
MOV C, A : Save unpacked BCDI in C register
MOV A, B : Get BCD again
ANI FOH : Mask least significant four bits
RRC : Convert most significant four bits into unpacked BCD2
RRC
RRC
RRC
MOV B, A : Save unpacked BCD2 in B register
XRA A : Clear accumulator (sum = 0)
MVI D, 0AH : Set D as a multiplier of 10
Sum: ADD D : Add 10 until (B) = 0
DCR B : Decrement BCD2 by one
JNZ SUM : Is multiplication complete? i if not, go back and add again
ADD C : Add BCD1
STA 2300H : Store the result
HLT : Terminate program execution
Binary to BCD conversion
Statement: Write a main program and a conversion subroutine to convert the binary number stored at 6000H into its
equivalent BCD number. Store the result from memory location 6100H.
Sample Problem: (6000) H = 8AH
1.8AH ? 64H (Decimal 100) :. Divide by 64H (Decimal 100)
8AH/64H ? Quotient = 1, Remainder = 26H
26H < 64H (Decimal 100) :. Go to step 2 and Digit 2 = 1
2.26H ? OAH (Decimal 10) :. Divide by OAH (Decimal 10)
26H/OAH ? Quotient = 3, Remainder = O8H
OSH < OAH (Decimal 10) :. Go to step 3 and Digit 1 = 3
3. Digit 0 = O8H
Source Program:
LXI SP, 27FFH : Initialize stack pointer
LDA 6000H : Get the binary number in accumulator
CALL SUBROUTINE : Call subroutine
HLT : Terminate program execution
Subroutine to convert binary number into its equivalent BCD number:
PUSH B : Save BC register pair contents
PUSH D : Save DE register pair contents
MVI B, 64H : Load divisor decimal 100 in B register
MVI C, 0AH : Load divisor decimal 10 in C register
MVI D, 00H : Initialize Digit 1
MVI E, 00H : Initialize Digit 2
STEP1: CMP B : Check if number < Decimal 100
JC STEP 2 : if yes go to step 2
SUB B : Subtract decimal 100
INR E : update quotient
JMP STEP 1 : go to step 1
STEP2: CMP C : Check if number < Decimal 10
JC STEP 3 : if yes go to step 3
SUB C : Subtract decimal 10
INR D : Update quotient
JMP STEP 2 : Continue division by 10
STEP3: STA 6100H : Store Digit 0
MOV A, D : Get Digit 1
STA 6101H : Store Digit 1
MOV A, E : Get Digit 2
STA 6102H : Store Digit 2
POP D : Restore DE register pair
POP B : Restore BC register pair
RET : Return to main program
Find the 7-segment codes for given numbers
Statement: Find the 7-segment codes for given 5 numbers from memory location 6000H and store the result from memory
location 7000H.
Sample Problem: (6000) H = 8AH
Source Program
LXI H, 6200H : Initialize lookup table pointer
LXI D, 6000H : Initialize source memory pointer
LXI B, 7000H : Initialize destination memory pointer
BACK: LDAX D : Get the number
MOV L, A : A point to the 7-segment code
MOV A, M : Get the 7-segment code
STAX B : Store the result at destination memory location
INX D : Increment source memory pointer
INX B : Increment destination memory pointer
MOV A, C
CPI O5H : Check for last number
JNZ BACK : If not repeat
HLT : End of program
57. Find the ASCII character
Statement: Write an assembly language program to convert the contents of the five memory locations starting from 2000H
into an ASCII character. Place the result in another five memory locations starting from 2200H.
Sample Problem
(2000H) = 1
(2001H) = 2
(2002H) = 9
(2003H) = A
(2004H) = B
Result:(2200H) = 31
(2201H) = 32
(2202H) = 39
(2203H) = 41
(2204H) = 42
Source program:
LXI SP, 27FFH : Initialize stack pointer
LXI H, 2000H : Source memory pointer
LXI D, 2200H : Destination memory pointer
MVI C, O5H : Initialize the counter
BACK: MOV A, M : Get the number
CALL ASCII : Call subroutine ASCII
STAX D : Store result
INX H : Increment source memory pointer
INX D : Increment destination memory pointer
DCR C : Decrement count by 1
CJNZ : if not zero, repeat
HLT : Stop program execution subroutine ASCII
ASCII: CPI, OAH : Check if number is OAR
JNC NEXT : If yes go to next otherwise continue
ADI 30H
JMP LAST
NEXT: ADI 37H
LAST: RET : Return to main program
Subroutine:
Subroutine 'ASCII' converts a hexadecimal digit to ASCII.The digit is passed using accumulator
and the result is stored in accumulator.Stack starts From 27FEH to 27FDH.
ASCII to Decimal Conversion
Statement: convert the ASCII number in memory to its equivalent decimal number
Source Program:
LXI H, 4150 : Point to data
MOV A, M : Get operand
SUI 30 : convert to decimal
CPI 0A : Check whether it is valid decimal number
JC LOOP : yes, store result
MVI A, FF : No, make result=FF
LOOP: INX H
MOV M, A
HLT : (A) = (4151)
Note: The ASCII Code (American Standard Code for Information Interchange) is commonly used
for communication. It is a seven bit code. In this code number 0 through 9 are represented as 30
through 39 respectively and letters A through Z are represented as 41H through 5AH. Therefore,
by subtracting 30H we can convert an ASCII number into its decimal equivalent.
HEX to Decimal conversion
Statement: Convert the HEX number in memory to its equivalent decimal number
Source Program:
LXI H, 4150 ; Point to data
LXI B, 0000 ; Initialize hundreds= 0, Tens=0
MOV A, M ; Get hex data to A
LOOP: SUI 64
JC LOOP 1
INR B ; hundreds= hundreds+1
JMP LOOP
LOOP 1: ADI 64 ; if subtracted extra, add it clear carry flag
LOOP 2: SUI 0A
JC LOOP 3
INR C ; Tens=tens+1
JMP LOOP 2
LOOP 3: ADI 0A ; If subtracted extra, add it again
INX H ; A = Units
MOV M, B ; store hundreds
MOV B, A ; Combine Tens in C &
MOV A, C ; Units in A to form a
RLC ; Single 8-bit number
RLC
RLC
RLC
ADD B
INX H
MOV M, A ; Store tens & Units
HLT
Note: In this experiment the number is converted to its equivalent decimal number using the
following logic. First count the number of hundreds, the number of tens & units present in that
hex number. Then add up to get the equivalent decimal number.
Converting A9 we get:
A9 /64=45 Hundreds = 01
Since 64(100 decimal) cannot be subtracted from 45 no. of hundreds = 01. Now count tens
45/0A=3B Tens = 01
Now from 09, 0A cannot be subtracted. Hence tens = 06 the decimal equivalent of A9 is 169.
HEX to binary conversion
Statement: Convert an 8 bit hex no to its binary form & store in memory.
Source Program:
LXI H, 4150 : Initialize memory pointer
MVI B, 08 : count for 8-bit
MVI A, 54
LOOP : RRC
JC LOOP1
MVI M, 00 : store zero it no carry
JMP COMMON
LOOP2: MVI M, 01 : store one if there is a carry
COMMON: INX H
DCR B : check for carry
JNZ LOOP
HLT : Terminate the program