EXAMPLE PROGRAMS OF 8085
Here We use Jubin Mitra 8085-simulator.
1). Write assembly language program to subtract two 8-bit numbers and Display Borrow.
Solution:
MVI A,A9H // Loads the A9h to Accumulator
MVI B,ABH // Loads the ABh to B-register
MVI C,00H // Loads the 00h to C-register
SUB B // Subtract the content of B-register from Accumulator and result stored in Accumulator.
JNC SKIP // Jump on no Carry to SKIP
INR C // Increment C-register
CMA // Complements Accumulator content
INR A // Increment Accumulator
SKIP: STA 2100H // Stores the answer to memory location 2100 H
MOV A,C // moves the content of C-register to accumulator (A < - C)
HLT // Stop processing
2). Write assembly language program to find number of 1's and 0's in 8-bit number stored at 2010 memory address.
Solution:
LDA 2010H // Loads the content of 2010H into accumulator
MVI C,08H // Loads the 08h to C-register
MVI D,00H // Loads the 00h to D-register
MVI E,00H // Loads the 00h to E-register
START: RRC // All the bits of accumulator are shifted or rotated right. The CY flag is modified as LSB.
JNC ZERO // Jump on no Carry(CY=0) to ZERO
JC ONE // Jump on Carry(CY=1) to ONE
ONE: INR D // Increment D-register to count number of 1's
JMP END // Jump to END
ZERO: INR E // Increment E-register to count number of 0's
JMP END // Jump to END
END: DCR C // Decrement C-register
JNZ START // Jump on no Zero(Z=0) to START
HLT // Stop processing
# ORG 2010H // Set origin to dataset
# DB ADH // initializes memory with one or more byte values
3). Write assembly language program to find a factorial of a given number.
Solution:
LXI H,2501H // Loads the address of the number in H-L register pair
MOV B,M // Moves the content of memory to B-register
MVI A,00H // Loads the 00h to accumulator
MOV D,B // Moves the content of B-register to D-register
DCR B // Decrement B-register.
START: JZ CNT // Jump on ZERO(Z=1) to CNT
MOV E,B // Moves the content of B-register to E-register
MUL: ADD D // Adds the contents of D-register with the contents of accumulator and the answer is stored in Accumulator
DCR E // Decrement E-register.
JNZ MUL // Jump on no ZERO(Z=0) to MUL
MOV D,A // Moves the content of Accumulator to D-register
MVI A,00H // Loads the 00h to Accumulator
DCR B // Decrement B-register
JMP START // Jump to START
CNT: MOV A,D // Moves the content of D-register to Accumulator
HLT // Stop processing
# ORG 2010H // Set origin to dataset
# DB 05H // initializes memory with one or more byte values
4). Write assembly language program to Load the contents of memory locations 2100 H and 2101 H in B-register and C-register respectively. The content of memory locations 2100 H and 2101H are 16 H and 19 H respectively.
Solution:
LDA 2100H // Loads the content of 2100H into accumulator
MOV B,A // moves the content of accumulator to B-register (B < - A)
LDA 2101H // Loads the content of 2101H into accumulator
MOV C,A // moves the content of accumulator to C-register (C < - A)
HLT // Stop processing
# ORG 2100H // Set origin to dataset
# DB 16H,19H // initializes memory with one or more byte values
5). Write an assembly language program to find the 2's complement of a hexadecimal number. The hexadecimal number 6A H is stored in memory location 2100H and the answer is to be stored in 2101 H.
Solution:
LDA 2100H // Loads the content of 2100H into accumulator
CMA // Complements the accumulator content (1’s complement)
INR A // 1 is added to the accumulator content to get the 2’s complement
STA 2101H // Loads the accumulator contents into memory location 2101 H
HLT // Stop processing
# ORG 2100H // Set origin to dataset
# DB 6AH // initializes memory with one or more byte values
6). Write an assembly language program to add two numbers (decimal) 38 and 64, then subtract decimal number 3 from the sum. The final answer is to be stored in memory location 2100 H.
use Converter to convert decimal to hexadecimal numberSolution:
MVI A,26H // Loads the first number to accumulator
MVI B,40H // Loads the second number to B-register
MVI C,03H // Loads the third number to C-register
ADD B // Adds the contents of B-register with the contents of accumulator and the answer is stored in A
SUB C // Content of C gets subtracted from accumulator and difference is stored in A
STA 2100H // Answer is stored in 2100 H location
HLT // Stop processing
7). Write an assembly language program to interchange (swap) the contents of two memory locations 2100 H and 2101 H.
Solution:
LDA 2100H // Loads the content of 2100H into accumulator
MOV B,A // Moves the content of accumulator to B-register
LDA 2101H // Loads the content of 2101H into accumulator
STA 2100H // Loads the accumulator content to 2100 H location
MOV A,B // Moves the content of B-register to accumulator
STA 2101H // Loads the accumulator content to 2101 H location
HLT // Stop processing
# ORG 2100H // Set origin to dataset
# DB 05H,07H // initializes memory with one or more byte values
8). Write an assembly language program to multiply two decimal numbers 23 and 9 and store the answer in some memory location.
use Converter to convert decimal to hexadecimal numberSolution:
MVI A,00H // Loads the accumulator with 00H
MVI B,17H // Loads B-register with 17H
MVI C,09H // Loads C-register with 09H
AGAIN: ADD B // Adds the content of B-register to accumulator
DCR C // Decrements C-register
JZ END // Checks for zero; if zero jump to END
JMP AGAIN // Repeats the addition
END: STA 2100H // Stores the answer to memory location 2100 H
HLT // Stop processing
9). Write an assembly language program to find the smaller of two numbers stored in memory locations 2501 H and 2502 H. Store the result in 2503 H memory location.
Solution:
LXI H,2501H // Loads the address of the first number in H-L register pair
MOV A,M // Saves the first number in accumulator
INX H // Increments the H-L register pair
CMP M // compares the two numbers
JC NEXT // if carry smaller number is in accumulator go to NEXT
MOV A,M // If no carry then move the smaller number to accumulator
NEXT: STA 2503H // Stores the smaller number in 2503 memory location
HLT // Stop processing
# ORG 2501H // Set origin to dataset
# DB 12H,34H // initializes memory with one or more byte values
10). Write an assembly language program to find the sum of a series 1+2+3+....+10 (or sum of first 10 natural numbers).
Solution:
MVI B,0AH // Loads B-register with 0AH
MVI A,00H // Loads the accumulator with 00H
LOOP: ADD B // Adds the content of B-register to accumulator
DCR B // Decrements B-register
JNZ LOOP // Checks for No zero; if No zero jump to LOOP
STA 2500H // Stores the answer to memory location 2500H
HLT // Stop processing
11). Write an assembly language program to check the even parity or the odd parity of the number stored in memory location 2010 H. Send 00 H or EE H at the output port 02 H if the parity is odd or even respectively.
Solution:
LXI H,2010H // Initializes the H-L register pair with the address of the location
MOV A,M // Moves the number to accumulator
ORA A // ORing of A with A will load the same number to accumulator. The parity flag will be affected with this operation
JPO ODD // Jump to ODD if parity is odd
MVI A,EEH // Load EE H to accumulator for even parity
OUT 02H // EE is sent to output port 02H
JMP END // Jump to END
ODD: MVI A,00H // Load 00 H to accumulator for odd parity
OUT 02H // 00 is sent to output port 02H
END: HLT // Stop processing