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

Assembly Language 86x 7 eddetion IT MUST WRITEN ASSEMBLY LANGUAGE The greatest c

ID: 3699442 • Letter: A

Question

Assembly Language 86x 7 eddetion

IT MUST WRITEN ASSEMBLY LANGUAGE

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
pseudocode:
int GCD(int x, int y)
{
x = abs(x) // absolute value
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

Code:

#Define text section

.text

        #Define main

     .globl main

    

#Define main method

main:

        #Push to stack

     sub $sp,$sp,12   

    

     #Store return address

     sw   $ra,4($sp)   

       #String output

     li   $v0,4      

    

     #Load address

     la   $a0,pow   

    

     #Display string

     syscall      

    

     #Integer input

     li $v0,5       

    

     #system call

     syscall        

    

     #Move data into register

     move $t2,$v0   

         

     #Load value  

     li   $v0,4     

    

     #load address

     la   $a0,bas    

    

     #Display string

     syscall        

    

     #Load

     li $v0,5       

    

     #System call

     syscall        

    

     #Move value to register

     move $a0,$t2   

    

     #Move value to register

     move $a1,$v0  

    

     #Jump

     jal euc

    

     #Store

     sw   $v0,8($sp)

     #Load

     li   $v0,4

    

     #Load value

     la   $a0,str

    

     #System call

     syscall

       #Load value

     li   $v0,1

    

     #Load value

     lw   $a0,8($sp)

    

     #System call

     syscall

    

     #Restore address

     lw   $ra,4($sp)   

    

     #Pop stack

     add $sp,$sp,12

    

     #Jump

     jr   $ra

        #Define data

     .data

    

#Define label

str:        

        #Display message          

     .asciiz "value = "

    

#Define label     

bas:         

           #Display message         

     .asciiz "second integer = "

    

#Define label

pow:        

        #Display message          

     .asciiz "first integer = "

    

     #Define text

     .text

#Define label

euc:

       #Push stack

     sub $sp,$sp,8

    

     #Store address

     sw   $ra,4($sp)   

       #Exit if b is not 0

     bne $a1, $zero, L1

    

     #Return a0

     add $v0,$zero,$a0

    

     #Pop stack

     add $sp,$sp,8

    

     #Return

     jr   $ra

    

#Define L1   

L1:

       #Set c to b

     move $t4,$a1        

    

     #Compute remainder

     rem $a1,$a0,$a1     

    

     #Set a

     move $a0,$t4

    

     #Call recursively        

     jal euc           

        #Load

     lw   $ra,4($sp)   

    

     #Move value to $v0

     move $v0,$a0   

        #Pop stack

     add $sp,$sp,8

    

     #Return

     jr   $ra

SAMPLE OUTPUT:-

first integer = 2

second integer = 8

value = 3

first integer = 3

second integer = 9

value = 3

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