Sunday, January 16, 2011

Some Microprocessor programs

Exchange the contents of memory locations
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.

Add contents of two memory locations

Sample problem:
(4000H) = 7FH
(400lH)  = 89H
         Result    = 7FH + 89H = lO8H
(4002H) = 08H
(4003H) = 0lH
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 the lower byte of result at 4002H
MVIA, 00 :Initialize higher byte result with 00H
ADC A :Add carry in the high byte result
INX H :HL Points 4003H
MOV M, A :Store the higher byte of result at 4003H
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

Finding Two's complement of a number

Sample problem:
(4200H) = 55H
Result = (4300H) = AAH + 1 = ABH
Source program:
LDA 4200H : Get the number
CMA : Complement the number
ADI, 01 H : Add one in the number
STA 4300H : Store the result
HLT : Terminate program execution

Finding one's complement of a number

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

Unpack a BCD number

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

Find the negative numbers in a block of data.

Sample problem 1:
(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

Separate even numbers from given numbers

Source program : 
  • LXI H, 2200H : Initialize memory pointer l
  • LXI D, 2300H : Initialize memory pointer2
  • MVI C, 32H : Initialize counter
  • BACK:MOV A, M : Get the number
  • ANI 0lH : Check for even number
  • JNZ SKIP : If ODD, don't store
  • MOV A, M : Get the number
  • STAX D : Store the number in result list
  • INX D : Increment pointer 2
  • SKIP: INX H : Increment pointer l
  • DCR C : Decrement counter
  • JNZ BACK : If not zero, repeat
  • HLT : Stop

Evaluate the expression C=A2+B2

MVI B, Data#1 ; Data #1 is stored in register B
MOV C, B ; Copy of Data #1 is made in register C
MVI D, Data#2 ; Data #2 is stored in register D
MOV E,D ; Copy of Data #2 is made in register E
XRA A ; Accumulator content is cleared
Again: ADD B    
DCR C : A2 is calculated by repeated Addition
JNZ Again
MOV H,A ; Calculated A2 value is stored in register H
XRA A ; Accumulator content is cleared
Loop: ADD D
DCR E  :B2 is calculated by repeated Addition
JNZ Loop
ADD H ; A2+ B2 is determined, by adding result in A and register content H
 STA 4200H ; Result is stored in memory location 4200H

2-Digit BCD to binary conversion

Sample problem 1:
(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

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 1:
(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

Generate and display BCD down counter

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 Program 2:
  • 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 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 Routine:
  • 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.

Generate and display binary up counter

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 Routine:
  • 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

Find the factorial of a number

Statement: Program to calculate the factorial of a number between 0 to 8
Source program : 
  • LXI SP, 27FFH ; Initialize stack pointer
  • LDA 2200H ; Get the number
  • CPI 02H ; Check if number is greater than 1
  • JC LAST
  • MVI D, 00H ; Load number as a result
  • MOV E, A
  • DCR A
  • MOV C,A ; Load counter one less than number
  • CALL FACTO ; Call subroutine FACTO
  • XCHG ; Get the result in HL
  • SHLD 2201H ; Store result in the memory
  • JMP END
  • LAST: LXI H, 000lH ; Store result = 01
  • END: SHLD 2201H
  • HLT
Subroutine Program:
  • FACTO:LXI H, 0000H
  • MOV B, C ; Load counter
  • BACK: DAD D
  • DCR B
  • JNZ BACK ; Multiply by successive addition
  • XCHG ; Store result in DE
  • DCR C ; Decrement counter
  • CNZ FACTO ; Call subroutine FACTO
  • RET ; Return to main program

No comments: