EC237 Lab Assignment-1
EC237 Lab Assignment-1
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
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
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
; 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
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
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)
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
; 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…
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
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
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 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
Code :
;<Program title>
jmp start
;data
;code
start: nop
MOV A, M ; Load binary number into accumulator
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
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
;<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
;<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).
;<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
ANI 0F0H ; Mask out the lower nibble (only carry remains)
STA 6002H ; Store the carry in memory location 6002H
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
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
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
jmp START
;data
;code
START: NOP
MVI D,08H ;Initialize a register with the size of array
DCR D
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
;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
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)
;<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
MOV A, D
SUI 08H
SKIP: MOV B, A
MVI A, 01H
NEXT: DCR B
JNZ NEXT
ANA C
JZ ZERO
JMP END
; 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
;<Program title>
jmp start
;data
;code
start: nop
; Load array of 10, 8-bit numbers from external memory locations
starting at 4000H
; 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
; 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:
jmp start
;data
;code
start: nop
MVI C, 00H ; Initialize counter C to 0
;<Program title>
jmp start
;data
;code
start: nop
; Load the number of data bytes to be transferred from register A
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
Code:
jmp start
; 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
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
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
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
; Subroutine to subtract two 16-bit numbers (B-C and D-E) and store the result in H-L
; Subroutine to perform bitwise AND on two 16-bit numbers (B-C and D-E) and store the …
; Subroutine to perform bitwise OR on two 16-bit numbers (B-C and D-E) and store the r…
; 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
; 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