Question

In assembler code must be a gcd.s file So I here is the C code I...

In assembler code must be a gcd.s file

So I here is the C code I have written originally for the program. My question is how do I convert it to assembly code?

// C code below

#include

//this function swaps given integers

void swap(int*a,int*b)

{

    int temp=*a;

    *a=*b;

    *b=temp;

}

//this function returns gcd of a and b

int gcd(int a,int b)

{

    if(a

    swap(&a,&b);

    int r=a%b;

    if(r==0)

    return b;

    return gcd(b,r);

}

int main()

{

    int first,second;

    printf("Enter first positive integer: ");

    scanf("%d",&first);

    printf("Enter second positive integer: ");

    scanf("%d",&second);

    printf("The GCD is %d",gcd(first,second));

}

Original instructions

The Euclidean algorithm is a way to find the greatest common divisor of two positive integers, a and b. First let me show the computations for a=210 and b=45.

Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30.

Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15.

Divide 30 by 15, and get the result 2 with remainder 0, so 30=2·15+0.

The greatest common divisor of 210 and 45 is 15.

Formal description of the Euclidean algorithm

Input Two positive integers, a and b.

Output The greatest common divisor, g, of a and b.

Internal computation

1. If a<b, exchange a and b.

2. Divide a by b and get the remainder, r. If r=0, report b as the GCD of a and b.

3. Replace a by b and replace b by r. Return to the previous step.

Your assignment is to write an assembler code (gcd.s) that asks the user two positive numbers and calculates their greatest common denominator.

For example, you program should produce the following outputs:

Enter first positive integer: 6

Enter second positive integer: 8

The GCD is 2

Homework Answers

Answer #1

""

First make sure we got gcc compiler in your system then save your file has filename.c,

Now go to the command prompt and enter gcc -S filename.c then press enter

then a file named filename.s will be created in the same folder where your c file is

""

   .file   "help.c"
   .text
   .globl   swap
   .def   swap;   .scl   2;   .type   32;   .endef
   .seh_proc   swap
swap:
   pushq   %rbp
   .seh_pushreg   %rbp
   movq   %rsp, %rbp
   .seh_setframe   %rbp, 0
   subq   $16, %rsp
   .seh_stackalloc   16
   .seh_endprologue
   movq   %rcx, 16(%rbp)
   movq   %rdx, 24(%rbp)
   movl   $0, -4(%rbp)
   movq   16(%rbp), %rax
   movl   (%rax), %eax
   movl   %eax, -4(%rbp)
   movq   24(%rbp), %rax
   movl   (%rax), %edx
   movq   16(%rbp), %rax
   movl   %edx, (%rax)
   movq   24(%rbp), %rax
   movl   -4(%rbp), %edx
   movl   %edx, (%rax)
   nop
   addq   $16, %rsp
   popq   %rbp
   ret
   .seh_endproc
   .globl   gcd
   .def   gcd;   .scl   2;   .type   32;   .endef
   .seh_proc   gcd
gcd:
   pushq   %rbp
   .seh_pushreg   %rbp
   movq   %rsp, %rbp
   .seh_setframe   %rbp, 0
   subq   $48, %rsp
   .seh_stackalloc   48
   .seh_endprologue
   movl   %ecx, 16(%rbp)
   movl   %edx, 24(%rbp)
   movl   $0, -4(%rbp)
   movl   16(%rbp), %edx
   movl   24(%rbp), %eax
   cmpl   %eax, %edx
   jle   .L3
   leaq   24(%rbp), %rax
   movq   %rax, %rdx
   leaq   16(%rbp), %rcx
   call   swap
.L3:
   movl   16(%rbp), %eax
   movl   24(%rbp), %ecx
   cltd
   idivl   %ecx
   movl   %edx, -4(%rbp)
   cmpl   $0, -4(%rbp)
   jne   .L4
   movl   24(%rbp), %eax
   jmp   .L5
.L4:
   movl   24(%rbp), %eax
   movl   -4(%rbp), %edx
   movl   %eax, %ecx
   call   gcd
.L5:
   addq   $48, %rsp
   popq   %rbp
   ret
   .seh_endproc
   .def   __main;   .scl   2;   .type   32;   .endef
   .section .rdata,"dr"
   .align 8
.LC0:
   .ascii "Enter first positive integer: \0"
.LC1:
   .ascii "%d\0"
   .align 8
.LC2:
   .ascii "Enter second positive integer: \0"
.LC3:
   .ascii "The GCD is %d\0"
   .text
   .globl   main
   .def   main;   .scl   2;   .type   32;   .endef
   .seh_proc   main
main:
   pushq   %rbp
   .seh_pushreg   %rbp
   movq   %rsp, %rbp
   .seh_setframe   %rbp, 0
   subq   $48, %rsp
   .seh_stackalloc   48
   .seh_endprologue
   call   __main
   movl   $0, -4(%rbp)
   movl   $0, -8(%rbp)
   leaq   .LC0(%rip), %rcx
   call   printf
   leaq   -4(%rbp), %rax
   movq   %rax, %rdx
   leaq   .LC1(%rip), %rcx
   call   scanf
   leaq   .LC2(%rip), %rcx
   call   printf
   leaq   -8(%rbp), %rax
   movq   %rax, %rdx
   leaq   .LC1(%rip), %rcx
   call   scanf
   movl   -8(%rbp), %edx
   movl   -4(%rbp), %eax
   movl   %eax, %ecx
   call   gcd
   movl   %eax, %edx
   leaq   .LC3(%rip), %rcx
   call   printf
   movl   $0, %eax
   addq   $48, %rsp
   popq   %rbp
   ret
   .seh_endproc
   .ident   "GCC: (tdm64-1) 5.1.0"
   .def   printf;   .scl   2;   .type   32;   .endef
   .def   scanf;   .scl   2;   .type   32;   .endef

--->happy coding

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ARM assembly Code The Euclidean algorithm is a way to find the greatest common divisor of...
ARM assembly Code The Euclidean algorithm is a way to find the greatest common divisor of two positive integers, a and b. First let me show the computations for a=210 and b=45. Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30. Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15. Divide 30 by 15, and get the result 2 with remainder 0, so 30=2·15+0. The greatest common divisor of 210...
In assembler code must be a gcd.s file So here is the C code I have...
In assembler code must be a gcd.s file So here is the C code I have written originally for the program. My question is how do I convert it to assembly code? //C code #include <stdio.h> int fibonacci(int n) { if(n <= 2) { return 1; } else { return fibonacci(n-1) + fibonacci(n-2); } } int main(void) { int n; printf("Enter Fibonacci term: "); scanf("%d", &n); printf("The %dth Fibonacci number is %d\n", n, fibonacci(n)); return 0; } Instructions In C...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function...
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...
Assembly Language Programming create an .asm [assembly language program file, using Microsoft Visual Studio or equivalent...
Assembly Language Programming create an .asm [assembly language program file, using Microsoft Visual Studio or equivalent Windows32 integrated development environment (IDE)] program into one that inputs two positive integers, and outputs their greatest common divisor, by implementing the following pseudo-code: input num1 and num2 (assume num1 > 0 and num2 > 0) gcd := num1 remainder := num2 repeat numerator := gcd gcd := remainder remainder := numerator % gcd until (remainder == 0) output gcd The following 3 windows...
How can i make this lunix code print 3 numbers in reverse it must be in...
How can i make this lunix code print 3 numbers in reverse it must be in printStars format and no loops Here is the code i have but can figure out how to reverse the numbers #include<stdio.h> void printStars(int n) { if (n>0){ printf(""); printStars(n-1); } } int main() { int n; int b; int c; printf("Enter 3 numbers to reverse "); scanf("%d",&n,&b,&c); printf("your reversed numbers are %d",n); printStars(n); return 0;
debug code on C(not C+ or C++) I try to fix by myself, node part show...
debug code on C(not C+ or C++) I try to fix by myself, node part show error. This program should work for scan 5 ints from the user Using those 5 ints, it require construct a linked list of 5 elements. After this, it sould prntf of the list using the PrintList. #include <stdio.h> struct Node{    int data;    Node* next; }; int main(void){    struct Node first = {0, 0};    struct Node* second = {0, 0};   ...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
CODE IN C++. The first 11 prime integers are 2, 3, 5, 7, 11, 13, 17,...
CODE IN C++. The first 11 prime integers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31. A positive integer between 1 and 1000 (inclusive), other than the first 11 prime integers, is prime if it is not divisible by 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31. Instructions Write a program that prompts the user to enter a positive integer between 1 and 1000 (inclusive) and that outputs whether the number...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...