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

i need help implementing this function into a naked funstion in ASSEMBLY languag

ID: 3861683 • Letter: I

Question

i need help implementing this function into a naked funstion in ASSEMBLY language

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Dijkstra's Algorithm to calculate GCD.

Implement a recursive function in assembly that calculates the greatest common divisor of
two unsigned integers.

Given two unsigned integers n and m, we can define GCD(m , n) as:

GCD(m , n) = n                       ,    if (m % n) == 0
GCD(m , n) = GCD(n , m % n)          ,    if (m % n) > 0

************************************************************************************************/

__declspec(naked)
unsigned int gcd(unsigned int m, unsigned int n) {

// C code to be converted to x86 assembly
/*
if ((m % n) == 0)
return n;
else
return gcd(n, m % n);
*/
__asm {
  mov eax, 0
  // BEGIN YOUR CODE HERE


  // END YOUR CODE HERE
  ret
}
}

Explanation / Answer

for the code:

#include <stdio.h>
int main()
{
int m,n;
int gcd(int,int);
if ((m % n) == 0)
return n;
else
return gcd(n, m % n);
}

mips x86 is as follows:

main: # @main
push RBP
mov RBP, RSP
sub RSP, 16
mov DWORD PTR [RBP - 4], 0
mov EAX, DWORD PTR [RBP - 8]
mov ECX, DWORD PTR [RBP - 12]
cdq
idiv ECX
cmp EDX, 0
jne .LBB0_2
mov EAX, DWORD PTR [RBP - 12]
mov DWORD PTR [RBP - 4], EAX
jmp .LBB0_3
.LBB0_2:
mov EAX, DWORD PTR [RBP - 12]
mov ECX, DWORD PTR [RBP - 8]
mov DWORD PTR [RBP - 16], EAX # 4-byte Spill
mov EAX, ECX
cdq
mov ECX, DWORD PTR [RBP - 16] # 4-byte Reload
idiv ECX
mov EDI, ECX
mov ESI, EDX
call gcd(int, int)
mov DWORD PTR [RBP - 4], EAX
.LBB0_3:
mov EAX, DWORD PTR [RBP - 4]
add RSP, 16
pop RBP
ret