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
""
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
Get Answers For Free
Most questions answered within 1 hours.