0% found this document useful (0 votes)
39 views9 pages

Imp 5 Answer

The document describes an assembly language program that copies data from one memory location to another by: 1) initializing a counter to 5, 2) loading the source and destination addresses into registers, and 3) copying data using a loop that decrements the counter until it reaches 0.

Uploaded by

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

Imp 5 Answer

The document describes an assembly language program that copies data from one memory location to another by: 1) initializing a counter to 5, 2) loading the source and destination addresses into registers, and 3) copying data using a loop that decrements the counter until it reaches 0.

Uploaded by

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

1….

ORG 0000H ; Starting address of program

START: MVI C, 05H ; Initialize counter to 5

LXI H, 2001H ; Load source address into HL

LXI D, 3001H ; Load destination address into DE

COPY_LOOP:

MOV A, M ; Move data from source address to accumulator

MOV M, A ; Move data from accumulator to destination address

INX H ; Increment source address

INX D ; Increment destination address

DCR C ; Decrement counter

JNZ COPY_LOOP ; Jump if counter is not zero

HLT ; Halt the processor

END ;
2…….

ORG 0000H ; Starting address of program

START: MVI C, 08H ; Initialize counter to 8

LXI H, FACTORIAL_TABLE ; Load the address of factorial table into HL

CALL INPUT_NUMBER ; Call subroutine to input the number

MOV B, A ; Move the number to register B

MOV A, M ; Load the factorial of the input number from the table

CPI 00H ; Compare if the input number is 0

JZ RESULT ; Jump to result if the input number is 0

CALL CALC_FACTORIAL ; Call subroutine to calculate factorial

JMP DISPLAY_RESULT ; Jump to display result

MVI AH, 01H ; Function to read a character from the console

INT 21H ; DOS interrupt

SUB '0' ; Convert ASCII character to binary

RET ; Return from subroutine

DCR B ; Decrement the counter

JZ RETURN ; If the counter is 0, return

PUSH B ; Save the counter value

CALL FACTORIAL ; Calculate factorial recursively

POP B ; Restore the counter value

MUL M ; Multiply accumulator with the factorial value

JMP RETURN ; Return from subroutine

FACTORIAL_TABLE:

DB 01H, 01H, 02H, 06H, 0AH, 0FH, 015H, 01CH, 024H


RESULT:

MOV A, B ; Move the input number to accumulator

ADD L ; Add the address offset of the factorial table

MOV A, M ; Load the factorial value of the input number

JMP RETURN ; Jump to return

DISPLAY_RESULT:

MVI AH, 02H ; Function to display a character on the console

INT 21H ; DOS interrupt

RET ; Return from subroutine

RETURN:

HLT ; Halt the processor

END ; End of program


3…..

ORG 0000H ; Starting address of program

START: MVI C, 10H ; Initialize counter to 10 (array size)

LXI H, 2100H ; Load starting address of array into HL

MOV E, M ; Move first element of array to register E

INX H ; Increment address

DCR C ; Decrement counter

COMPARE_LOOP:

MOV A, M ; Move current element of array to accumulator

CMP E ; Compare with the largest number found so far

JNC UPDATE_LARGEST ; Jump if current number is larger or equal

MOV E, A ; Update largest number if current number is larger

UPDATE_LARGEST:

INX H ; Move to the next element

DCR C ; Decrement counter

JNZ COMPARE_LOOP ; Jump if counter is not zero

STORE_RESULT:

LXI H, 2200H ; Load destination address

MOV M, E ; Store the largest number at memory location 2200h

HLT ; Halt the processor

END ; End of program


4…..

ORG 0000H ; Starting address of program

START: MVI C, 19H ; Initialize counter to 19 (array size - 1)

LXI H, 2000H ; Load starting address of array into HL

OUTER_LOOP:

LXI D, 2000H ; Load starting address of array into DE for inner loop

MVI E, 19H ; Initialize inner counter to 19 (array size - 1)

INNER_LOOP:

MOV A, M ; Move current element of array to accumulator

INX D ; Move to the next element

CMP M ; Compare with the next element

JNC SKIP_SWAP ; Jump if no swap needed (current element >= next element)

MOV B, A ; Move current element to register B

MOV A, M ; Move next element to accumulator

MOV M, B ; Move current element to next element's position

DCX D ; Decrement D to point to the current element again

MOV M, A ; Move next element (stored in accumulator) to current element's position

SKIP_SWAP:

DCR E ; Decrement inner counter

JNZ INNER_LOOP ; Jump if inner counter is not zero

DCR C ; Decrement outer counter

JNZ OUTER_LOOP ; Jump if outer counter is not zero

HLT ; Halt the processor


5….

ORG 0000H ; Starting address of program

START: MVI C, 10H ; Initialize counter to 10

LXI H, RESULT ; Load the address to store factorial results

LXI D, 0000H ; Load the destination address for storing the result

CALCULATE_FACT:

CALL INPUT_NUMBER ; Call subroutine to input the number

MOV B, A ; Move the number to register B

CALL CALC_FACTORIAL ; Call subroutine to calculate factorial

MOV A, B ; Move the number back to accumulator

STAX D ; Store the factorial result at the destination address

INX D ; Move to the next destination address

DCR C ; Decrement counter

JNZ CALCULATE_FACT ; Jump if counter is not zero

HLT ; Halt the processor

INPUT_NUMBER:

MVI AH, 01H ; Function to read a character from the console

INT 21H ; DOS interrupt

SUB '0' ; Convert ASCII character to binary

RET ; Return from subroutine

CALC_FACTORIAL:

MOV A, B ; Move the number to accumulator

CPI 00H ; Compare if the number is 0


JZ FACT_ZERO ; Jump to FACT_ZERO if the number is 0

MVI B, 01H ; Initialize factorial value to 1

CALL FACTORIAL_LOOP ; Call loop to calculate factorial

JMP RETURN ; Jump to RETURN

FACT_ZERO:

MVI B, 01H ; Set factorial value to 1

JMP RETURN ; Jump to RETURN

FACTORIAL_LOOP:

DCR A ; Decrement the number

JZ RETURN ; Return if the number is 0

PUSH B ; Save the factorial value

CALL FACTORIAL_LOOP ; Recursive call to factorial loop

POP B ; Restore the factorial value

MUL B ; Multiply accumulator with the factorial value

JMP RETURN ; Jump to RETURN

RETURN:

RET ; Return from subroutine

RESULT: ; Storage space for factorial results

END ; End of program


6…..

ORG 0000H ; Starting address of program

START: LXI H, 9000H ; Load starting address of numbers into HL

MVI C, 04H ; Initialize outer loop counter to 4

OUTER_LOOP:

LXI D, 9000H ; Load starting address of numbers into DE for inner loop

MOV A, L ; Load the least significant byte of HL into A

INX H ; Increment HL to point to the next number

MOV B, L ; Copy least significant byte of HL into B for comparison

MVI E, 04H ; Initialize inner loop counter to 4

INNER_LOOP:

MOV L, B ; Move number in B to least significant byte of HL

MOV A, M ; Move current element of array to accumulator

INX D ; Move to the next element

CMP M ; Compare with the next element

JNC SKIP_SWAP ; Jump if no swap needed (current element <= next element)

MOV C, A ; Move the current element to register C

MOV A, M ; Move next element to accumulator

MOV M, C ; Move current element to next element's position

DCX D ; Decrement DE to point to the current element again

MOV M, A ; Move next element (stored in accumulator) to current element's position

SKIP_SWAP:

DCR E ; Decrement inner counter

JNZ INNER_LOOP ; Jump if inner counter is not zero


DCR C ; Decrement outer counter

JNZ OUTER_LOOP ; Jump if outer counter is not zero

HLT ; Halt the processor

END ; End of program

You might also like