0% found this document useful (0 votes)
28 views

EC237 Lab Assignment-1

The document provides instructions and sample code for 4 problems related to 8085 microprocessor programming. Problem 1 involves multiplying two 8-bit numbers stored in memory using shift and add method. Problem 2 adds eight 16-bit unsigned numbers stored in consecutive memory locations. Problem 3 finds the 2's complement of a 16-bit number stored in register pair D-E and stores it in H-L. Problem 4 adds two multi-byte numbers stored in external memory and stores the result in another memory location. Sample assembly language code and flowcharts are provided for solving each problem.

Uploaded by

421243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

EC237 Lab Assignment-1

The document provides instructions and sample code for 4 problems related to 8085 microprocessor programming. Problem 1 involves multiplying two 8-bit numbers stored in memory using shift and add method. Problem 2 adds eight 16-bit unsigned numbers stored in consecutive memory locations. Problem 3 finds the 2's complement of a 16-bit number stored in register pair D-E and stores it in H-L. Problem 4 adds two multi-byte numbers stored in external memory and stores the result in another memory location. Sample assembly language code and flowcharts are provided for solving each problem.

Uploaded by

421243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

EC237 – MICROPROCESSORS LAB-1 (PROBLEM SET-1)

Name : Kaspa Vivek


Roll no: 421166
Branch: Computer science & Engineering
Section: A
----------------------------------------------------------------------------------------------------------------------------- ---------

TASK-1 : Familiarization & Programming of 8085


Note : Draw the flowchart for the solution of each problem and write efficient
assembly language programs for 8085 Microprocessor.
1. Write a program to multiply two 8-bit numbers stored in the memory
location 3001H and 3002H using Shift and Add method. Store the answer
in register pair H-L. (Higher byte of the result in the H register and lower
byte of the result in the L register).

8085 Assembly Language program (Code):


;Program to Multiply 8 bit nos using Shift & Add

jmp start

start: nop
LDA 3001H
MOV B,A
LDA 3002H
MOV C,A
LXI H,0000H

LOOP: RLC
JNC SKIP
ADD B
SKIP: DCR B
JNZ LOOP
MOV L,C
MOV H,A

MOV A, H ; Move the content of H register to Accumulator A


STA 5000H ; Store Accumulator A content in memory address 8000H
MOV A,L
STA 5001H
HLT
Flowchart
Simulation & Results :
Alternative Code:
; Multiply two numbers using ADD and SHIFT

jmp start

;data

P:EQU 3000H
Q:EQU 3001H

;code
start: NOP
LXI H,P ; Load first number's address in P register
MOV A,M ; Get the first number in A register
MOV B,A ; copy that no in B register

LXI H,Q
MOV A,M
MOV L,A ; get second no in L

MVI H,00H ; Result will be in H|L pair

MVI C,08 ; Initialize counter(C reg=8)

RPT : MOV A,L ; Test the LSB of product (if it is 1 or 0)


RRC ; using rotate instr get LSB in C flag
JNC SHIFT ; if LSB is 0,do shift only and bypass ADD

ADD: STC ; clear carry if any from above


CMC
MOV A,B ; Add H=H+A
ADD H
MOV H,A

SHIFT: MOV A,H ; Right shift product one bit through carry
RAR
MOV H,A; shifting 1 16 bit value using 2 8 bit
MOV A,L ; Rotate right through carry instructions
RAR
MOV L,A

DCR C
JNZ RPT
STOP: HLT
2. Eight 16-bit unsigned numbers are stored in the memory 1000H-1000Fh
(lower byte of the 16-bit number stored at lower address). Add all the
numbers and store the sum in the memory locations 2002H (Most
significant byte), 2001H and 2000H.
Code:

LHLD 1000H ; Load the contents of memory location 1000H into HL register pair
XCHG ; Exchange the content of HL register pair with DE register pair
LHLD 1002H ; Load the contents of memory location 1002H into HL register pair
DAD D ; Add the value of DE register pair to HL register pair (DAD stands for
"Double Add")
MVI C,00H ; Move immediate value 00H into the C register
JNC LOOP1 ; Jump to label LOOP1 if no carry flag is set (JNC stands for "Jump if No
Carry")
INR C ; Increment the value of the C register by 1
LOOP1: ; Label indicating the start of LOOP1 block
SHLD 1002H ; Store the contents of HL register pair into memory location 1002H

LHLD 1002H
XCHG
LHLD 1004H
DAD D
MVI C,00H
JNC LOOP2
INR C
LOOP2: SHLD 1004H

LHLD 1004H
XCHG
LHLD 1006H
DAD D
MVI C,00H
JNC LOOP3
INR C
LOOP3: SHLD 1006H

LHLD 1006H
XCHG
LHLD 1008H
DAD D
MVI C,00H
JNC LOOP4
INR C
LOOP4: SHLD 1008H

LHLD 1008H
XCHG
LHLD 100AH
DAD D
MVI C,00H
JNC LOOP5
INR C
LOOP5: SHLD 100AH

LHLD 100AH
XCHG
LHLD 100CH
DAD D
MVI C,00H
JNC LOOP6
INR C
LOOP6: SHLD 100CH

LHLD 100CH
XCHG
LHLD 100EH
DAD D
MVI C,00H
JNC LOOP7
INR C
LOOP7: SHLD 2000H ; Store the contents of HL register pair into memory location 2000H
MOV A,C ; Move the value of the C register into the accumulator A
STA 2002H ; Store the value of the accumulator into memory location 2002H

HLT

Simulation & results:


Alternative code:

; This program adds eight 16-bit unsigned numbers stored in the memory
; in the consecutive memory locations

jmp start

; data

P: EQU 1000H
Q: EQU 2000H

;code
start: NOP
LXI B,P ; pointer to base address of nos
LXI H,Q ; pointer to base address of sum

MVI D,08H ; Initialize counter for 8 numbers

RPT: LDAX B ; Add lower order byte of number to the


ADD M ; lower order byte of sum
MOV M,A ; store the result in sum

INX B ; point to the second byte of no


INX H ; point to second byte of sum

LDAX B ; Add higher order byte of no to the


ADC M ; second byte of sum along with carry
MOV M,A ; store the result in the sum

INX B ; point to the first byte of the next number


JNC SKIP ; if no carry skip this part

; Do the following part only if you get carry upon


; adding 2nd byte of sum along with carry to higher order byte of no

MVI A,00H ; initialize the accumulator to 0


INX H ;point to 3rd byte of sum
ADC M
MOV M,A ; accomodate carry into third byte if any
DCR H ; again point to second byte of sum

SKIP: DCX H ; point to first byte of sum


DCR D ; Decrement counter
JNZ RPT
STOP: HLT
Flowchart:
3. Suppose a 16-bit number is present in register pair D-E. Find the 2’s
complement of the number and store the result in H-L register pair. Use
the first register of the register pair as the most significant byte.

Code :

LXI H,0000H ; Load immediate value 0000H into the H and L registers
MVI B,00H ; Load immediate value 00H into the B register
MOV A,M ; Move the value at the memory location pointed by HL into the A register
CMA ; Complement the value in the A register (bitwise NOT)
ADI 01H ; Add immediate value 01H to the value in the A register
STA 0002H ; Store the result in the A register to memory location 0002H

JNC LOOP ; Jump to LOOP if no carry (carry flag is not set)


INR B ; Increment the value in the B register

LOOP: LXI H,0001H ; Load immediate value 0001H into the H and L registers
MOV A,M ; Move the value at the memory location pointed by HL into the A register
CMA ; Complement the value in the A register (bitwise NOT)

ADD B ; Add the value in the B register to the value in the A register
STA 0003H ; Store the result in the A register to memory location 0003H
JNC LOOP2 ; Jump to LOOP2 if no carry (carry flag is not set)

MVI A,01H ; Load immediate value 01H into the A register

STA 0004H ; Store the value in the A register to memory location 0004H

HLT
LOOP2: MVI A,00H ; Load immediate value 00H into the A register
STA 0004H ; Store the value in the A register to memory location 0004H

HLT
Alternative code :

;Program to Find the 2’s complement of the 16-bit number and store the result in H-L r…

MVI E, 5AH ; Load a sample least significant byte (LSB) value into E
MVI D, 02AH ; Load a sample most significant byte (MSB) value into D

; Compute 2's complement of the LSB

MOV A, E ; Copy the least significant byte (E) to accumulator


CMA ; Take 1's complement of A
ADI 01H ; Add 1 to get 2's complement
MOV L, A ; Store the result in L (least significant byte of HL)

; Compute 2's complement of the MSB with carry

MOV A, D ; Copy the most significant byte (D) to accumulator


CMA ; Take 1's complement of A
; ADC 00H ; Add carry from previous operation (1 or 0)
MOV B, A ; Store 1's complement of MSB in register B
MOV A, L ; Load LSB into A for addition
ADD B ; Add 1's complement of MSB (stored in B)
MOV H, A ; Store the result in H (2's complement of MSB)

; Display the original and 2's complement values

LXI H, 2300H ; Memory location to display values


MOV M, E ; Store original LSB value
INX H
MOV M, D ; Store original MSB value
INX H
MOV M, L ; Store 2's complement LSB value
INX H
MOV M, H ; Store 2's complement MSB value
HLT ; Halt the processor

; Data values
DB 00H ; Placeholder for original LSB
DB 00H ; Placeholder for original MSB
DB 00H ; Placeholder for 2's complement LSB
DB 00H ; Placeholder for 2's complement MS
Flowchart:
Simulation & Results:

Alternate solution :

;2s Complement

jmp start

;data

;code
start: nop
LXI D,2000H ; Load immediate value 2000H into registers D and E as a combined…
LDAX D ; Load the accumulator (A) with the value stored at the memory add…
CMA ; Complement (flip) all bits in the accumulator (A)
INR A ; Increment the value in the accumulator (A) by 1
STAX D ; Store the value in the accumulator (A) back to the memory addres…

hlt ; Halt instruction, which stops the program execution


4. Write a program to add two multi-byte numbers stored in the external
memory. The first number is stored at the external memory location
6000H, 6001H, 6002H and the second number is stored at the external
memory location 7000H, 7001H, 7002H (lower byte of data stored at
lower address). Write the result at the external memory location starting
from 8000H.

Code :

;<Program title>

jmp start

;data

;code
start: nop
MVI B, 10H ; Load higher byte of address for the first number
MVI C, 00H ; Load lower byte of address for the first number

MVI H, 20H ; Load higher byte of address for the second number
MVI L, 00H ; Load lower byte of address for the second number

MVI D, 30H ; Load higher byte of address for the result


MVI E, 00H ; Load lower byte of address for the result

MOV A, M ; Load the first byte of the first number into the accumulator
ADD M ; Add the first byte of the second number to the accumulator
STAX D ; Store the result in the memory location pointed to by DE

INX B ; Increment the higher byte of the address for the first number
INX H ; Increment the higher byte of the address for the second number
INX D ; Increment the higher byte of the address for the result

MOV A, M ; Load the second byte of the first number into the accumulator
ADC M ; Add the second byte of the second number along with the carry
STAX D ; Store the result in the memory location pointed to by DE

INX B ; Increment the higher byte of the address for the first number
INX H ; Increment the higher byte of the address for the second number
INX D ; Increment the higher byte of the address for the result

MOV A, M ; Load the third byte of the first number into the accumulator
ADC M ; Add the third byte of the second number along with the carry
STAX D ; Store the final result in the memory location pointed to by
hlt
Alternate solution :

;<Program title>

jmp start

;data

;code
start: nop
MVI H, 60H ; Initialize HL pair with address 6000H
MVI L, 00H

MOV A, 6000H ; Load the first byte from address 6000H into A
INX H ; Increment HL pair to point to the next byte (6001H)
MOV B, 6001H ; Load the second byte from address 6001H into B

MVI H, 70H ; Initialize HL pair with address 7000H


MVI L, 00H

MOV C, 7000H ; Load the first byte from address 7000H into C
INX H ; Increment HL pair to point to the next byte (7001H)
MOV D, 7001H ; Load the second byte from address 7001H into D

; Add the lower bytes


ADD B ; Add the second byte from address 6001H to A
MOV E, A ; Store the result in E temporarily

; Add the higher bytes along with the carry from the previous addition
ADC D ; Add the second byte from address 7001H and carry to A
MOV L, A ; Store the result in L
MOV A, E ; Restore the lower byte result from E
ADC C ; Add the first byte from address 7000H and carry to A
MOV H, A ; Store the result in H
MOV A, L ; Move the result from L to A

ADI 00H ; Add zero to clear the carry


MOV 8000H, A ; Store the result at address 8000H
INX H ; Increment HL pair to point to the next memory location (8001H)
MOV 8001H, H ; Store the higher byte of the result at address 8001H

HLT ; Halt the processor


Flowchart:
Simulation & Results :
5. Write a program to convert 8-bit binary number to BCD. Binary number
is available at external memory location at 2000H. Store result at the
memory location 2001H (BCD2,BCD1) and 2002H (BCD0).

Code :

;<Program title>

jmp start

;data

;code
start: nop
MOV A, M ; Load binary number into accumulator

ANI 0F0H ; Mask lower 4 bits


RRC ; Rotate accumulator right through carry
RRC ; Rotate again
RRC ; Rotate again
MOV B, A ; Store the BCD value in B

ANI 0FH ; Mask upper 4 bits


RAR ; Rotate accumulator right through carry
RAR ; Rotate again
RAR ; Rotate again
MOV C, A ; Store the least significant BCD digit in C
MOV A, B ; Move the original BCD value from B to A

ANI 0FH ; Mask lower 4 bits


MOV B, A ; Store the remaining BCD value in B
MOV A, C ; Move the least significant BCD digit back to A
STA 2001H ; Store BCD2 and BCD1 at memory location 2001H
MOV A, B ; Move the remaining BCD value from B to A
STA 2002H ; Store BCD0 at memory location 2002H

HLT ; Halt the microprocessor

DB 74H ; Place your 8-bit binary number her


Alternative code :
; This program converts a binary number to its BCD digits
; we will be using a subroutine MYDIV for 8bit division

jmp start

;data

P: EQU 2000H
STACK: EQU 4000H

;code
start: NOP
LXI SP,STACK ; Initialize stack pointer
LXI H,P ; pointer to binary no in memory
MOV A,M ; get the byte in the accumulator
MVI B,,0AH ; Load B with decimal 10
CALL MYDIV ; divide no by 10,quotient in A ,remainder in B

INX H ; increment pointer twice to point to BCD0 location


INX H;
MOV M,B ; Write BCDO in memory at 2002H

MVI B, ОАН ; load B with 10 decimal again


CALL MYDIV

ANI 0FH ; mask first 4 bits of A register


CALL SWAP_A ; swap upper and lower nibble of A
MOV C,A ; Save A into C register
MOV A,B ; Get B into A register
ANI 0FH ; Mask first 4 bits of B register
ADD C ; Pack 2 BCD numbers

DCX H ; Point to BCD2|BCD1 location


MOV M,A ; store the result

MYDIV: PUSH D ; Preserve DĒ pair


MVI D,0FFH ; Initialize with FFH

RPT: INR D ; Increment C


SUB B ; A=A-B
JNC RPT ; If A>B,keep subtracting
ADD B ; now remainder is in A
MOV B,A ; Remainder in B
MOV A,D ;Quotient in A
POP D ; Restore DĒ pair
RET ; Return to main program

SWAP_A: RRC ; rotate right A by 4 times


RRC
RRC
RRC
RET
STOP : HLT
Flowchart:
Simulation & Results:
6. Write a program to convert a 8-bit packed BCD number stored in the
Accumulator to two ASCII numbers corresponding to the two BCD digits
and store the ASCII numbers in the register pair D-E.

Code:

jmp start

; Data

; Code
start: nop ; No operation

LDA 2050H ; Load the accumulator with the content of memory address 2050H
MOV B, A ; Move the content of the accumulator (A) to the B register
ANI 0F0H ; Perform a bitwise AND operation between the accumulator (A) and 0F0H
ADI 30H ; Add 30H (48 in decimal) to the accumulator (A)
STA 3050H ; Store the content of the accumulator (A) to memory address 3050H
MOV A, B ; Move the content of the B register back to the accumulator (A)
ANI 0FH ; Perform a bitwise AND operati
on between the accumulator (A) and 0FH
RLC ; Rotate the bits in the accumulator (A) left through carry
RLC ; Rotate the bits in the accumulator (A) left through carry again
RLC ; Rotate the bits in the accumulator (A) left through carry again
RLC ; Rotate the bits in the accumulator (A) left through carry again
ADI 30H ; Add 30H (48 in decimal) to the accumulator (A)
STA 3051H ; Store the content of the accumulator (A) to memory address 3051H

hlt ; Halt the execution of the program


Alternate code: (SIMPLE)

;<Program title>

jmp start

;data

;code

start: nop
MOV A, M ; Load the packed BCD number from memory to
Accumulator
ANI 0FH ; Mask the lower 4 bits to get the first BCD digit
DAA ; Convert BCD digit to ASCII
MOV E, A ; Store the ASCII value of the first BCD digit in register E

MOV A, M ; Load the packed BCD number again


RAR ; Rotate the Accumulator right to get the second BCD digit
in the l…
ANI 0FH ; Mask the lower 4 bits to get the second BCD digit
DAA ; Convert BCD digit to ASCII
MOV D, A ; Store the ASCII value of the second BCD digit in
register D
HLT ; Halt the processor
Flowchart:
Simulation & Results:
7. Write a program to add two packed BCD numbers stored at external
memory location 6000H and 6001H. Store the answer in memory
location 6002H (for carry) and 6003H.

;<Program title>

jmp start

;data

;code
start: nop
LXI H,9000H ; Load the HL register pair with the memory address 9000H.
MVI C,00H ; Load register C with the value 00H.
MOV A,M ; Move the value in memory location pointed by HL (9000H) to
register A.
INX H ; Increment the HL register pair to point to the next memory location.
ADD M ; Add the value in memory location pointed by HL (9001H) to the
value in register A.
DAA ; Decimal Adjust Accumulator after addition.
JNC next ; Jump to 'next' if no carry occurred after the addition.
INR C ; Increment the value in register C.

next: INX H ; Increment the HL register pair to point to the next mem loc
MOV M,A ; Move the value in register A to the memory location pointed
by HL (9001H).
INX H ; Increment the HL register pair to point to the next memory
location.
MOV M,C ; Move the value in register C to the memory location pointed
by HL (9002H).

hlt ; Halt the CPU.


Alternate Code:

;<Program title>

jmp start

;data

;code
start: nop
LXI H, 6000H ; Load memory location 6000H in HL pair
MOV A, M ; Load the BCD number from 6000H into accumulator

INR H ; Increment HL to point to the next memory location (6001H)

ADD M ; Add the BCD number from 6001H to accumulator


DAA ; Adjust the result in accumulator

MOV M, A ; Store the result in memory location 6003H


MOV A, L ; Load the carry value from accumulator's low nibble

ANI 0F0H ; Mask out the lower nibble (only carry remains)
STA 6002H ; Store the carry in memory location 6002H

HLT ; Halt the processor


Flowchart:
Simulation & Results:
8. Perform operations on 16 bit packed BCD data stored in register pair B-C
and D-E according to contents of Accumulator as shown below. Use
separate subroutine for each operation. Store the answer in external
memory locations 2001H, 2002H and 2003H(for carry/borrow).

Code:

;<Program title>

jmp START

;data

;code
START: nop
; Load the accumulator with the operation code
LXI H, 2000H
MOV A, M
; Load data for the operation (e.g., B and C)
LXI H, 2050H
MOV B, M
INX H
MOV C, M

; Call the appropriate subroutine based on the operation code


CALL OPERATION_SUBROUTINE

; Store the result in memory locations 2001H-2003H


LXI H, 2001H
MOV M, E
INX H
MOV M, D
INX H
MOV M, C

; Store the carry/borrow in memory location 2004H


MOV A, E
RRC
MOV M, A
HLT

OPERATION_SUBROUTINE: CALL BCD_DECREMENT ; Call the BCD Decrement


subroutine if A…
CALL NINE_TEN_COMPLEMENT ; Call the 9's and 10's Complement
subroutine if A = 1 or…
CALL BCD_SUBTRACTION ; Call the BCD Subtraction subroutine if A
=3
RET

BCD_DECREMENT: MOV A, E
SUB B
MOV E, A
MOV A, D
SBB C
MOV D, A
RET

NINE_TEN_COMPLEMENT: CMA
DAA
MOV E, A
MOV A, D
CMA
DAA
MOV D, A
RET

BCD_SUBTRACTION: MOV A, E
SUB B
MOV E, A
MOV A, D
SBB C
MOV D, A
RAR
RAR
MOV A, E
SUB B
MOV E, A
MOV A, D
SBB C
MOV D, A
RET
hlt

Flowchart:
Simulation & Results:
9. Write a program to convert an 8-bit hexadecimal number stored in
register A to decimal number (0-255) and store each decimal digit in
B,C,D registers. The units place digit should be in D register, tens place
digit should be in C register and hundreds place digit should be in the B
register.
Code:
;<Program title>

jmp start

;data

;code
start: nop
MVI B, 00H ; Initialize B register with 0
MVI C, 00H ; Initialize C register with 0
MVI D, 00H ; Initialize D register with 0

; Load the hexadecimal number from A register


MOV A, A ; Copy A to A to work with

; Convert hexadecimal to decimal using repeated subtraction

LOOP: CPI 10H ; Compare A with 10H


JC DONE ; Jump to DONE if A < 10H (i.e., units digit)
INR C ; Increment tens place (C register)
;SUBI 10H ; Subtract 10H from A
DCR A ; Decrement A (subtract 1)
JMP LOOP ; Jump back to LOOP

DONE: MOV B, C ; Move tens place (C) to B register


MOV C, B ; Initialize C register with 0

; Convert the remaining value in A to units and store in D


MOV D, A ; Move the remaining value to D

HLT ; Halt the processor


Flowchart:
Simulation & Results:
10. Treat the register pair D-E (MSB in D) as 16-bit register and rotate them 2
places to the left without involving the carry bit.

Code:

;<Program title>

jmp START

;data

;code
START: nop
LXI D, DATA ; Load the data (initial value) into the D-E register pair
MOV A, D ; Move the contents of D into the accumulator

; Rotate left 2 places


RAL
RAL
MOV D, A ; Move the rotated value from accumulator to D

; Repeat the process for E


INX D ; Increment the D-E register pair to point to E
MOV A, E ; Move the contents of E into the accumulator
RAL
RAL
MOV E, A ; Move the rotated value from accumulator to E

HLT ; Halt the processor


DATA: DB 12H ; Initial data value (you can change this value
Flowchart:
Simulation & Results:
11. Write a program to sort an array of ten 8 bit numbers available in the
external memory location starting at 2000h onwards in ascending order.
(After ordering, lowest value must appear at 2000h).
Code:
;<Program title>

jmp START

;data

ARRAY: EQU 2000H

;code

START: NOP
MVI D,08H ;Initialize a register with the size of array

DCR D

OUTER_LOOP: MOV E, D ;Initialize another counter with one less than D


LXI H, ARRAY ; Load the base address of the array in HIL pair

INNER_LOOP: MOV B, M ;Get first number in B register


INX H ;Point to next number
MOV A, M ;Get next number in A register
STC
CMC ; clear carry flag

SUB B ;A=A-B
JNC NEXT ; if A>B ,move to next element,else swap

SWAP: MOV A, M ; Now the pointer HIL points to second number, get that
number in A
MOV M, B ; Store the number in B into Memory referenced by H-L pair
DCX H ; Decrement pointer (point to first number)
MOV M, A ;store the number in A into memory referenced by H-L pair
INX H ; Again increment H

NEXT: DCR E
JNZ INNER_LOOP
DCR D
JNZ OUTER_LOOP

STOP: HLT
Flowchart:
Simulation & Results:
12. Write a program to sort an array starting from external memory location
4000H in descending order. Number of data elements is stored on
external memory location 1FFFH.

Code:

;<Program title>

jmp START

;data

ARRAY: EQU 4000H


SIZE: EQU 1FFFH ; External memory location to store the number of data elements

;code

START: NOP
LXI H, SIZE ; Load the base address of the size of the array in H-L pair
MOV D, M ; Load the number of data elements in register D

OUTER_LOOP: MOV E, D
LXI H, ARRAY ; Load the base address of the array in H-L pair

INNER_LOOP: MOV A, M ; Get first number in A register


INX H ; Point to next number
MOV B, M ; Get next number in B register
CMP B ; Compare A and B
JNC NEXT ; If A >= B, move to next element, else swap

SWAP: XCHG ; Exchange the contents of H-L pair with D-E pair
MOV M, A ; Store the number in A into memory referenced by H-L pair
XCHG ; Restore the original values of H-L and D-E pairs
DCX H ; Decrement pointer (point to first number)
MOV A, M ; Load the swapped value back into the A register
MOV M, B ; Store the number in B into memory referenced by H-L pair
INX H ; Again increment H
MOV M, A ; Store the swapped value back into memory

NEXT: DCR E
JNZ INNER_LOOP
DCR D
JNZ OUTER_LOOP

STOP: HLT
Flowchart:
Simulation & Results:
13. Write a program to check whether the nth bit of a 16-bit number stored
in external memory location 4000H and 4001H (lower byte at lower
address) is 1, Write FFH in A register if it is 1, else store 00H in A register.
The value of n is stored in the B register.

Code:
;<Program title>

jmp start

;data

;code
start: nop
LXI H, 4000H ; Load the memory address of the 16-bit number into HL pair
MOV B, M ; Load the value of n from memory into the B register
INX H ; Increment HL to point to the second byte (4001H)
MOV A, M ; Load the 16-bit number's higher byte into the accumulator
ANI 01H ; Perform bitwise AND of accumulator (higher byte) with B (n)
JZ ZERO_BIT ; Jump to ZERO_BIT if result is zero (nth bit is not set)
MVI A, 0FFH ; Load FFH into the accumulator (bit is set)
HLT ; Halt the processor

ZERO_BIT: MVI A, 00H ; Load 00H into the accumulator (bit is not set)

HLT ; Halt the processor


Alternate code:

;<Program title>

jmp start

;data

;code
start: nop
; Load 16-bit number from external memory locations 4000H and
4001H (lower byte at lower address)
LDA 4000H
MOV C, A

LDA 4001H
MOV D, A

; Store the value of n in the B register


MVI B, 7 ; Replace n with the desired value

; Load the value of n from the B register


MOV A, B

; Check whether the nth bit of the 16-bit number is 1


CPI 08H
JC SKIP

MOV A, D
SUI 08H

SKIP: MOV B, A
MVI A, 01H

NEXT: DCR B
JNZ NEXT
ANA C
JZ ZERO

; If the nth bit is 1, store FFH in A register, else store 00H in A


register
MVI A, 255

JMP END

ZERO: MVI A, 00H

; End
END: HLT

Flowchart:
Simulation & Results:
14. Write a program to search a number in a given array of 10, 8-bit numbers.
The array is stored in the external memory at address starting at 4000H. The
number to be searched is given in B register. Write FFH in A register if it is
found, else write 00H in A register. Write the memory address of the number in
H-L pair if it is found. Use the binary search algorithm.
Code:
;<Program title>

jmp start

;data

;code
start: nop
MVI C, 10H ; Counter for number of elements in the array
LXI H, 4000H ; Load starting address of the array in H-L pair
MOV D, B ; Copy the search number from B to D
INX H ; Move to the next memory location

SEARCH_LOOP: MOV A, M ; Load the current array element into the


accumulator
CMP D ; Compare the accumulator with the search number
JZ FOUND ; If they are equal, jump to the found label
JC NOT_FOUND ; If accumulator < search number, jump to the
not found label
INR H ; Increment the memory address
DCR C ; Decrement the counter
JNZ SEARCH_LOOP ; If counter is not zero, repeat the loop

NOT_FOUND: MVI A, 000H ; Set A register to 00H (not found)


JMP END

FOUND: MVI A, 0FFH ; Set A register to FFH (found)


LHLD 00H ; Load the memory address into H-L pair

END: HLT ; Halt the processor


Alternate Code:

;<Program title>

jmp start

;data

;code
start: nop
; Load array of 10, 8-bit numbers from external memory locations
starting at 4000H

; Load the number to be searched from the B register


MVI B,02DH
MOV D, B

; Search for the number in the array using the linear search
algorithm
LXI H, 4000H ; Set HL pair to point to the start of the array
MVI C, 03H ; Set C register to the length of the array

NEXT: MOV A, M ; Load the value at HL into A


CMP D ; Compare A with D (the number to be searched)
JZ FOUND ; If A is equal to D, jump to FOUND

INX H ; Increment HL pair


DCR C ; Decrement C register
JNZ NEXT ; If C is not zero, jump to NEXT

; If the number is not found, store 00H in A register


NOTFOUND: MVI A, 00H
JMP END

; If the number is found, store FFH in A register and the memory address
of the number in H-L pair
FOUND: MVI A, 0FFH

END: HLT
Flowchart:
Simulation & Results:
15. Write a program to transfer a block of data from external memory location
4000H to 6000H. The number of data bytes to be transferred is given in the
register A.

Code:

; program to transfer a block of data from external memory location


4000H to 6000H.
; The number of data bytes to be transferred is given in the register A.

jmp start

;data

;code
start: nop
MVI C, 00H ; Initialize counter C to 0

LOOP: MOV A, M ; Load data from source (4000H) into accumulator


STA 6000H ; Store data to destination (6000H)
INX H ; Increment HL pair (point to next memory location)
INX D ; Increment DE pair (point to next memory location)
DCX B ; Decrement BC pair (decrease counter)
JNZ LOOP ; Jump to LOOP if counter is not zero

HLT ; Halt the microprocessor


Alternate Code:

;<Program title>

jmp start

;data

;code
start: nop
; Load the number of data bytes to be transferred from register A

; Transfer the block of data from external memory location 4000H to


6000H
LXI H, 4000H
LXI D, 6000H

NEXT: MOV A, M
STAX D

INX H
INX D

DCR B
JNZ NEXT

; End
HLT
Flowchart:
Simulation & Results:
16. Write a program to find the LCM and HCF of two 8-bit numbers stored in
the external memory location 3001h and 3002h. Write two subroutines to
compute HCF and LCM. Store the result in the register pair D-E for LCM
and B for HCF.

Code:
;<Program title>
jmp start

;data

;code
start: nop
MVI C, 00H ; Initialize counter for LCM
LDA 3001H ; Load first number from memory
MOV E, A ; Store it in register E
LDA 3002H ; Load second number from memory
MOV D, A ; Store it in register D
CALL LCM ; Call LCM subroutine
LDA 3001H ; Load first number from memory
MOV B, A ; Store it in register B
LDA 3002H ; Load second number from memory
CALL HCF ; Call HCF subroutine
HLT

LCM: LXI H,00H; Initialize HL pair for multiplication


MOV A, E ; Load value from E into A

LCM_LOOP: ADD E ; Add value in E to A


DAA ; Decimal adjust after addition
JNC LCM_NEXT ; Jump if no carry occurred
INX H ; Increment HL pair

LCM_NEXT: DCR C ; Decrement counter


JNZ LCM_LOOP ; Jump if counter is not zero
MOV E, A ; Store result in E register
RET

HCF: CMP B ; Compare B and A


JZ HCF_DONE ; Jump to done if B equals A
JC HCF_SWAP ; Jump to swap if B < A
SUB B ; Subtract B from A
JMP HCF ; Repeat the process

HCF_SWAP: MOV A, B ; Copy B to A


MOV B, C ; Copy counter to B
MOV C, A ; Copy A to counter
JMP HCF ; Repeat the process

HCF_DONE: MOV B, A ; Store HCF result in B register


RET
HLT
00
Flowchart:
Simulation & Results:
17. Write a program to generate first 10 numbers of Fibonacci series and store
in the external memory locations starting 3000h.

Code:
jmp start

; data (no data section is provided in your code)

; code
start: nop
LXI H, 3000h ; Load the memory address H with 3000h
MVI C, 0Ah ; Initialize the loop counter C with 10 (to generate 10
numbers)
MVI B, 00h ; Initialize B register with the first Fibonacci number (0)
MVI D, 01h ; Initialize D register with the second Fibonacci number (1)

RPT: MOV M, B ; Store the current Fibonacci number (B) at the memory
location
INX H ; Increment memory address
MOV A, B ; Move the contents of B (current Fibonacci) to A
ADD D ; Add the contents of D (previous Fibonacci) to A
MOV B, D ; Move the contents of D to B (previous Fibonacci)
MOV D, A ; Move the sum (A) to D (current Fibonacci)
DCR C ; Decrement the loop counter
JNZ RPT ; Jump back to RPT if the loop counter is not zero

HLT ; Halt the program


Flowchart:
Simulation & Results:
18. Write a program to divide the 8-bit number in the B register by the data
stored in C register. Restore the number in B register using multiplication of
the result and the answer of division operation.

Code:
;<Program title>

jmp start

;data

;code
start: nop
; Initialize data
MVI B, 42H ; Load the dividend (8-bit number) into B register
MVI C, 05H ; Load the divisor (data stored in C register)

; Division algorithm
MOV A, B ; Copy dividend to A register
CPI 00H ; Compare with 0
JC END_DIV ; If dividend is 0 or less, skip division

DIV_LOOP: SUB C ; Subtract divisor from A


JNC DIV_DONE ; If no carry, division is done
INR B ; Increment quotient (stored in B)
JMP DIV_LOOP ; Repeat division

DIV_DONE: DCX B ; Decrement quotient (compensate for last


increment)
MOV A, B ; Move quotient to A register

END_DIV: MOV B, A ; Copy quotient to B register


MOV A, C ; Copy divisor to A register
MVI C, 00H ; Clear C register to store multiplication result

MUL_LOOP: ADD B ; Add B to A (multiply)


DCR C ; Decrement counter (stored in C)
JNZ MUL_LOOP ; Repeat multiplication until counter is not zero
; Result is in A register (restored number after division and multiplication)

HLT ; Halt the microprocessor


Flowchart:
Simulation & Results:
19. Write a program to count the number of data bytes stored in external
memory locations 0100H to 0200H that are greater than the random
unsigned number in B register and less than the random unsigned number
stored in C register. Write the result in D-E register pair.

Code:
;A program to count the number of data bytes stored in external
memory locations 0100H
; to 0200H that are greater than the random unsigned number in B
register and less than…
; random unsigned number stored in C register. Write the result in D-E
register pair

jmp start

;data

;code
start: nop
MVI B, 000H ; Initialize B register with lower limit
MVI C, 0FFH ; Initialize C register with upper limit

LXI H, 0100H ; Initialize HL pair with starting memory address


LXI D, 0000H ; Initialize DE pair to store the count

MOV A, M ; Load the data from memory to accumulator


CMP B ; Compare with lower limit
JC SKIP ; Jump to SKIP if accumulator < B
CMP C ; Compare with upper limit
JC INCREMENT ; Jump to INCREMENT if accumulator < C

SKIP: INX H ; Increment memory address


DCR B ; Decrement lower limit
DCR C ; Decrement upper limit
CMP H ; Compare memory address with 0200H
JNZ LOOP ; Jump to LOOP if not yet reached the end

INCREMENT: INR D ; Increment the count


JMP SKIP ; Jump to SKIP

LOOP: MOV A, M ; Load the data from memory to accumulator


CMP C ; Compare with upper limit
JC SKIP ; Jump to SKIP if accumulator < C
INX H ; Increment memory address
DCR C ; Decrement upper limit
CMP H ; Compare memory address with 0200H
JNZ LOOP ; Jump to LOOP if not yet reached the end

HLT ; Halt the execution

Flowchart:
Simulation & Results:
20. Suppose you need to create a wrapper (software module) to extend the
arithmetic and logic unit of 8085 microprocessor for 16 bit operations. Write
the subroutines for each of the following pseudo-instruction (corresponding to
8-bit instruction in 8085). Assume that the operands are in the B-C and D-E
register pair. In case only one operand is required, assume it is in the B-C
register pair. Result should be read from the H-L register pair. Carry flag should
be set/reset according to the result of addition and subtraction instruction. You
need not consider any other flag.
ˆ MYADD16
ˆ MYSUB16
ˆ MYAND16
ˆ MYOR16
ˆ MYXOR16
ˆ MYRLC16
ˆ MYRRC16
ˆ MYRAL16
ˆ MYRLR16
ˆ MYINC16
ˆ MYDEC16
You should preserve any register that you are going to use in your subroutines.
Write a main program to test some of these pseudo-instructions
Flowchart:
Code:
;<Program title>
jmp START

;data

; Subroutine to add two 16-bit numbers (B-C and D-E) and store the result in H-L

MYADD16: PUSH PSW ; Preserve flags


PUSH B ; Preserve B register
PUSH D ; Preserve D register

; Add lower bytes


MOV A, C
ADD E
MOV L, A

; Add upper bytes with carry


MOV A, B
ADC D
MOV H, A

POP D ; Restore D register


POP B ; Restore B register
POP PSW ; Restore flags
RET

; Subroutine to subtract two 16-bit numbers (B-C and D-E) and store the result in H-L

MYSUB16: PUSH PSW ; Preserve flags


PUSH B ; Preserve B register
PUSH D ; Preserve D register

; Subtract lower bytes


MOV A, C
SUB E
MOV L, A

; Subtract upper bytes with borrow


MOV A, B
SBB D
MOV H, A
POP D ; Restore D register
POP B ; Restore B register
POP PSW ; Restore flags
RET

; Subroutine to perform bitwise AND on two 16-bit numbers (B-C and D-E) and store the …

MYAND16: PUSH PSW ; Preserve flags


PUSH B ; Preserve B register
PUSH D ; Preserve D register

; Perform AND on lower bytes


MOV A, C
ANA E
MOV L, A

; Perform AND on upper bytes


MOV A, B
ANA D
MOV H, A
POP D ; Restore D register
POP B ; Restore B register
POP PSW ; Restore flags
RET

; Subroutine to perform bitwise OR on two 16-bit numbers (B-C and D-E) and store the r…

MYOR16: PUSH PSW ; Preserve flags


PUSH B ; Preserve B register
PUSH D ; Preserve D register

; Perform OR on lower bytes


MOV A, C
ORA E
MOV L, A

; Perform OR on upper bytes


MOV A, B
ORA D
MOV H, A
POP D ; Restore D register
POP B ; Restore B register
POP PSW ; Restore flags
RET

; Subroutine to perform bitwise XOR on two 16-bit numbers (B-C and D-E) and store the …
MYXOR16: PUSH PSW ; Preserve flags
PUSH B ; Preserve B register
PUSH D ; Preserve D register

; Perform XOR on lower bytes


MOV A, C
XRA E
MOV L, A

; Perform XOR on upper bytes


MOV A, B
XRA D
MOV H, A
POP D ; Restore D register
POP B ; Restore B register
POP PSW ; Restore flags
RET

; Subroutine to rotate the 16-bit number in H-L left through carry


MYRLC16: PUSH PSW ; Preserve flags

; Rotate lower bytes left through carry


MOV A, L
RLC
MOV L, A

; Rotate upper bytes left through carry


MOV A, H
RLC
MOV H, A
POP PSW ; Restore flags
RET

; Subroutine to rotate the 16-bit number in H-L right through carry


MYRRC16: PUSH PSW ; Preserve flags

; Rotate lower bytes right through carry


MOV A, L
RRC
MOV L, A

; Rotate upper bytes right through carry


MOV A, H
RRC
MOV H, A
POP PSW ; Restore flags
RET

; Subroutine to rotate the 16-bit number in H-L left without carry


MYRAL16: PUSH PSW ; Preserve flags
; Rotate lower bytes left without carry
MOV A, L
RAL
MOV L, A

; Rotate upper bytes left without carry


MOV A, H
RAL
MOV H, A
POP PSW ; Restore flags
RET

; Subroutine to rotate the 16-bit number in H-L right without carry


MYRLR16: PUSH PSW ; Preserve flags
; Rotate lower bytes right without carry
MOV A, L
RAR
MOV L, A

; Rotate upper bytes right without carry


MOV A, H
RAR
MOV H, A
POP PSW ; Restore flags
RET

; Subroutine to increment the 16-bit number in H-L


MYINC16: PUSH PSW ; Preserve flags
; Increment lower bytes
INR L
; Check if lower byte overflowed
JNZ INC_END
INR H ; Increment upper bytes if lower byte overflowed

INC_END: POP PSW ; Restore flags


RET

; Subroutine to decrement the 16-bit number in H-L


MYDEC16: PUSH PSW ; Preserve flags
; Decrement lower bytes
DCR L

; Check if lower byte underflowed


JNZ DEC_END
DCR H ; Decrement upper bytes if lower byte underflowed
DEC_END: POP PSW ; Restore flags
RET

; Main program
START: LXI B, 1234H ; Load 16-bit value in B-C
LXI D, 5678H ; Load 16-bit value in D-E

; Test MYADD16
CALL MYADD16
; Result in H-L after addition
; Test MYSUB16
CALL MYSUB16
; Result in H-L after subtraction
; Test MYAND16
CALL MYAND16
; Result in H-L after bitwise AND
; Test MYOR16
CALL MYOR16
; Result in H-L after bitwise OR
; Test MYXOR16
CALL MYXOR16
; Result in H-L after bitwise XOR
; Additional tests can be added here
HLT ; Halt the program

Simulation & Results:

You might also like