Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assembly language Greatest Common Divisor (GCD) The greatest common divisor (GCD

ID: 3823762 • Letter: A

Question

Assembly language

Greatest Common Divisor (GCD) The greatest common divisor (GCD) of two integers is the largest integer that will evenly divide both integers. The GCD algorithm involves integer division in a loop, described by the following m pseudocode: int GCD(int x, int y) {//absolute value x = abs (x) y = abs (y) do {int n = x % y x = y y = n} while (y > 0) return x} Implement this function in assembly language and write a test program that calls the function several times, passing it different values. Display all results on the screen.

Explanation / Answer

Solution:-

The below given assembly code will calculate the GCD of two numbers. It divide the larger number with the smaller number. If we get remainder 0 then the smaller number is our GCD for the two given number. If we don’t get a zero remainder we transfer the remainder to AX and then again repeat the process till we get that zero remainder. In short I perform the same procedure we use mathematically to find the Greatest Common Denominator.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

MY_DATA SEGMENT

                NUM1 DW 000AH

                NUM2 DW 0004H

                GCD DW ?

MY_DATA ENDS

CODE SEGMENT

                ASSUME CS:CODE, DS:DATA

                START:

                MOV AX, DATA

                MOV DS, AX

                ; INITIALISATION OF DATA SEGMENT

               

                MOV AX, NUM1

                ; MOVE FIRST NUMBER TO AX

                MOV BX, NUM2

                ; MOVE SECOND NUMBER TO BX

               

                UP:

                CMP AX, BX

                JE EXIT

                ; IF EQUAL THEN NO POINT IN FINDING GCD

                JB EXCG

                ; IF FIRST NUMBER IS SMALLER PUT IT BX

                ; MEANING SMALL NUMBER MUST STAY IN BX

               

                UP1:

                MOV DX, 0000H

                ; INITIALISE DX

                DIV BX

                ; DIVIDE (BIGGER NUMBER)/(SMALLER NUMBER)

                CMP DX, 0

                ; SEE IF REMAINDER IS ZERO OR NOT

                JE EXIT

                ; IF ZERO VOILA YOU HAVE FOUND THE GCD IN THE SMALLER NUMBER

                MOV AX, DX

                ; IF NON ZERO MOVE REMAINDER TO AX

                JMP UP

                EXCG:

                XCHG AX, BX

                ; EXCHANGE CONTENTS OF AX AND BX

                JMP UP1

               

                EXIT:

                MOV GCD, BX

                ; STORE RESULT IN GCD

               

                ; WRITE THESE TWO LINES IF YOU ARE TESTING IN TASM

                MOV AH, 4CH

                INT 21H

                ; THE ABOVE TWO LINES ARE OBVIOUSLY THE EXIT INTERRUPT

                ; USED IN TASM

CODE ENDS

END START

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote