Write an ALP to find the GCD of two 16bit unsigned numbers,alp to find the factorial

At first install emu 8086 for compiling this assembly language program
code:
MOV BX, 1100H    ; Load the address 1100H into the BX register
MOV DI, 1200H    ; Load the address 1200H into the DI register
MOV AX, [BX]     ; Move the value at memory location 1100H into AX register (first number)
MOV CX, [BX+2]   ; Move the value at memory location 1102H into CX register (second number)

RPT:            ; Label for the beginning of the loop
CMP AX, CX      ; Compare AX with CX
JE STORE        ; If AX is equal to CX, jump to STORE (GCD found)
JNC SKIP        ; If AX is greater than or equal to CX, skip the next instruction
XCHG AX, CX     ; Exchange the values of AX and CX (to ensure AX >= CX)

SKIP:           ; Label for skipping the exchange
MOV DX, 0000H   ; Clear DX register to prepare for division
DIV CX          ; Divide AX by CX, quotient in AX, remainder in DX
CMP DX, 0000H   ; Compare the remainder with 0
JE STORE        ; If remainder is 0, jump to STORE (GCD found)
MOV AX, DX      ; Move the remainder into AX (AX = previous remainder)
JMP RPT         ; Jump back to the beginning of the loop

STORE:          ; Label for storing the result
MOV [DI], CX    ; Move the GCD (in CX) into the memory location 1200H
HLT             ; Halt the processor




Output:



tap on the view section and tap on memory


find out  0100:0000
change it to 0100:1100 and update

and first number as 7E that is 126 in decimal
and next  number 08  and update it



next tap on single step until you get the minimum GDP



The GCD stored on DX that is 02

Greatest common divisor is 02 of 7E(126 in decimal) and 08


Initialization:

assembly
MOV BX, 1100H
MOV DI, 1200H
Load 1100H into BX and 1200H into DI. BX points to the memory locations where the two 16-bit numbers are stored, and DI points to the memory location where the result (GCD) will be stored.
Loading Values:

assembly

MOV AX, [BX]
MOV CX, [BX+2]
Move the 16-bit number from memory location 1100H into AX (first number).
Move the 16-bit number from memory location 1102H into CX (second number).
Comparison and Exchange:

assembly

RPT:
CMP AX, CX
JE STORE
JNC SKIP
XCHG AX, CX
Compare AX and CX.
If AX is equal to CX, jump to STORE because the GCD is found.
If AX is greater than or equal to CX, continue to SKIP.
If AX is less than CX, exchange the values in AX and CX to ensure AX is always the larger number.
Division and Loop:

assembly

SKIP:
MOV DX, 0000H
DIV CX
CMP DX, 0000H
JE STORE
MOV AX, DX
JMP RPT
Clear DX to prepare for division (since DIV uses DX:AX as the dividend).
Divide AX by CX. The quotient goes to AX and the remainder goes to DX.
If the remainder (DX) is 0, jump to STORE because the GCD is CX.
If the remainder is not 0, move the remainder (DX) into AX and repeat the loop.
Store the Result:

assembly

STORE:
MOV [DI], CX
HLT
Move the GCD (contained in CX) into the memory location 1200H.
Halt the processor.
Summary
This assembly program implements the Euclidean algorithm for finding the GCD of two numbers. It repeatedly reduces the problem size by replacing the larger number with the remainder of the division until the remainder is zero, at which point the GCD is the other number. The result is stored at memory location 1200H.


In clear:


  1. Initialization:

    • MOV BX, 1100H: Loads the value 0x1100 (4352 in decimal) into register BX. This might be an address where data is stored, but we don't have enough context to know for sure.
    • MOV DI, 1200H: Loads the value 0x1200 (4608 in decimal) into register DI. This might be another address for data storage.
    • MOV AX, [BX]: Loads the value from the memory location pointed to by BX into register AX.
    • MOV CX, [BX+2]: Loads the value from the memory location 2 bytes after the address pointed to by BX into register CX.
  2. Sorting Loop (RPT Block):

    • RPT: Starts a loop that repeats until a specific condition is met (explained later).
    • CMP AX, CX: Compares the values in AX and CX.
    • JE STORE: If the values are equal (Jump if Equal), it jumps to the STORE label.
    • JNC SKIP: If the values are not equal and CX is not greater (Jump if Not Carry), it jumps to the SKIP label. Essentially, this checks if AX is less than CX.
    • XCHG AX, CX: Exchanges the values in AX and CX (swapping them).
    • SKIP: Label for the jump from the JNC instruction.
  3. Remainder Calculation:

    • MOV DX, 0000H: Loads 0 into register DX. This is likely used for storing the remainder during division.
    • DIV CX: Divides the value in AX by the value in CX and stores the quotient in AX and the remainder in DX.
  4. Inner Loop Check:

    • CMP DX, 0000H: Compares the remainder in DX with 0.
    • JE STORE: If the remainder is 0 (Jump if Equal), it jumps to the STORE label, indicating we've found the remainder and can exit the inner loop.
  5. Inner Loop Body:

    • MOV AX, DX: If the remainder isn't 0, this line moves the remainder from DX to AX, essentially setting AX to the new value for the next iteration of the inner loop.
    • JMP RPT: Jumps back to the beginning of the RPT loop to continue the division process.
  6. Storing the Result:

    • STORE: Label for the jump from the JE instructions in the previous sections.
    • MOV [DI], CX: Stores the value in CX (which should now be the GCD) into the memory location pointed to by DI.
    • HLT: Halts the program execution.

Key Points:

  • This code doesn't directly calculate the GCD but seems to implement a custom sorting mechanism. The RPT loop and conditional jumps suggest an iterative approach, but it's not Euclid's algorithm typically used for GCD calculation.
  • Without knowing the initial values in memory (pointed to by BX and BX+2), it's difficult to determine the exact sorting behavior.
  • The inner loop with DIV CX and the JE STORE check might be an attempt to find a remainder within the sorting process.

If you intend to calculate GCD:

You'd need to use Euclid's algorithm, which involves repeated division and modulo operations. The provided code snippet doesn't directly implement this approach.


WRITE AN ALP TO FIND THE FACTORIAL OF A GIVEN NUMBER USING

RECURSIVE PROCEDURE


AIM: TO WRITE AN ALP TO FIND THE FACTORIAL OF A GIVEN NUMBER USING RECURSIVE

PROCEDURE

SOFTWARE REQUIRED: MASM 16 BIT

PROGRAM:

.MODEL SMALL

.DATA

.COD

E

NUM DW 8

RESULT DW (?) ; INITIALIZE MEMORY FOR THE RESULT

MAIN PROC

MOV AX, @DATA ; INITIALIZE DATA SEGMENT

MOV DS, AX

MOV AX, 01 ; INITIALIZE RESULT AS 01 IF THE NUMBER IS 0

MOV CX, NUM ; INITIALIZE NUMBER

CMP CX, 00 ; CHECK WHETHER NUMBER IS 0

JE LOOP1 ; YES, TERMINATE PROGRAM

MOV BX, CX ; SAVE THE NUMBER IN BX

CALL FACT ; CALL FACTORIAL PROCEDURE

LOOP1

: MOV RESULT, AX ; SAVE FACTORIAL RESULT

MOV AH, 4CH

INT 21H

MAIN ENDP ; END MAIN PROCEDURE

FACT PROC

CMP BX, 01

JZ LOOP2

PUSH BX

DEC BX

CALL FACT ; CALL FACTORIAL

PROCEDURE POP BX

MUL BX

RET ; RETURN CALLED PROGRAM

LOOP2

: MOV AX, 01 ; INITIALIZE AX REGISTER TO 01

RET ; RETURN CALLED PROGRAM

FACT ENDP ; END FACTORIAL PROCEDURE

END ; END PROGRAM

================================================================================= OUTPUT:

BEFORE EXECUTION

================

0 1 2 3 4 5 6 7 8 9 A B C D E F

DS:0000 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

DS:0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

DS:0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

DS:0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

DS:0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

AFTER EXECUTION

===============

0 1 2 3 4 5 6 7 8 9 A B C D E F

DS:0000 08 00 80 9D 00 00 00 00 00 00 00 00 00 00 00 00

28

DS:0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

DS:0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

DS:0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

DS:0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

RESULT: PROGRAM IS EXECUTED WITHOUT ERRORS AND THE OUTPUT IS VERIFIED

VERIFICATION AND VALIDATION: OUTPUT IS VERIFIED AND IS FOUND CORRECT

CONCLUSION: THE FACTORIAL OF A GIVEN NUMBER IS FOUND AND OUTPUT IS VERIFIED

POST VIVA QUESTIONS:

 

1. Explain XCHG, LAHF, SAHF, XLAT

2. What are the two types of I/O addressing modes. ( fixed port ,variable port)

3. What do you mean by segment override prefix.

4. Explain the following directives. NEAR ,FAR,BYTE PTR,ORG,OFFSET,ORG

Differentiate END, ENDP, ENDM

I'd be glad to explain the code you provided:

Overall Functionality:

This assembly language program calculates the factorial of a number stored in memory location NUM. Factorial is the product of a non-negative integer and all positive integers less than or equal to itself. For example, the factorial of 5 (written as 5!) is 5 x 4 x 3 x 2 x 1 = 120.

Code Breakdown:

1. Model and Segments:

  • .MODEL SMALL: This line specifies that the program uses the "small" memory model, which is a way to manage memory allocation.
  • .DATA: This section declares the data segment, which stores variables like NUM and RESULT.
  • .CODE: This section declares the code segment, which contains the actual instructions for the program.

2. Data Segment:

  • NUM DW 8: This line defines a double word (16-bit) variable named NUM and initializes it with the value 8 (replace with the desired number).

3. Code Segment:

MAIN Procedure:

  • MOV AX, @DATA: This instruction loads the address of the data segment into the AX register.
  • MOV DS, AX: This instruction sets the data segment register (DS) to the address loaded in AX, making the data segment accessible.
  • MOV AX, 01: This initializes the RESULT variable with the value 1 (assuming 0! is defined as 1).
  • MOV CX, NUM: This copies the value of NUM into the CX register for further processing.
  • CMP CX, 00: This instruction compares the value in CX with 0 to check if NUM is 0.
  • JE LOOP1: If the comparison is equal (JE - Jump if Equal), the program jumps to the LOOP1 label. This is because the factorial of 0 is a special case (often defined as 1).
  • MOV BX, CX: If NUM is not 0, this instruction saves its value in the BX register for use in the factorial calculation.
  • CALL FACT: This instruction calls the FACT procedure, which performs the factorial calculation.

LOOP1 Label:

  • MOV RESULT, AX: This line stores the result of the factorial calculation (stored in AX) into the RESULT variable.

System Termination (Interrupt):

  • MOV AH, 4CH: This instruction sets the AH register to 4Ch, which is the system call code for program termination.
  • INT 21H: This instruction triggers an interrupt (INT 21H) to the operating system, signaling the program's end.

MAIN ENDP:

  • This line marks the end of the MAIN procedure.

FACT Procedure:

  • CMP BX, 01: This compares the value in BX (the number to calculate the factorial for) with 1.
  • JZ LOOP2: If the comparison is zero (JZ - Jump if Zero), the program jumps to the LOOP2 label, signifying the base case (factorial of 1 is 1).
  • PUSH BX: This instruction pushes the current value of BX onto the stack for temporary storage.
  • DEC BX: This instruction decrements the value in BX by 1.
  • CALL FACT: This recursively calls the FACT procedure again, but with the decremented value of BX. The recursion continues until the base case (factorial of 1) is reached.
  • POP BX: This instruction retrieves the previously saved value of BX from the stack.
  • MUL BX: This instruction multiplies the retrieved value of BX with the result returned from the recursive call (stored in AX). This effectively calculates the factorial.
  • RET: This instruction returns control from the FACT procedure.

LOOP2 Label:

  • MOV AX, 01: This initializes the AX register with 1 for the base case of the factorial (factorial of 1 is 1).
  • RET: This instruction returns control from the FACT procedure, passing the calculated factorial result in AX.

FACT ENDP:

  • This line marks the end of the FACT procedure.

END:

  • This line signifies the end of the entire assembly language program.

Key Points:

  • The program uses recursion, a technique where a function calls itself, to calculate the factorial.
  • The stack is used to store temporary values (BX) during the recursive calls.
  • The program handles the special case of NUM being 0.

I hope this explanation is comprehensive and helpful!

 

 
















Post a Comment

0 Comments