• Status: Solved
• Priority: Medium
• Security: Public
• Views: 898

# Converting C to Assembly Language

Is it possible to convert a program written in c to assembly language that will solve quadratic equation? Below is a sample program.

// Program to calculate roots of a quadratic equation ie ax2+bx+c=0 //

# include <stdio.h>

# include <conio.h>

#include <math.h>

void main()

{

float a, b, c, d, x1,x2;

clrscr();

printf("Enter coefficients of quadratic equation ie a, b and c: ");

scanf("%f%f%f", &a, &b, &c);

d=b*b-4*a*c;

if (d>0)

{

x1= (-b+(sqrt(d)))/(2*a);

x2= (-b-(sqrt(d)))/(2*a);

printf("Roots x1= %f and x2= %f are real and unequal", x1, x2);

}

else if (d==0)

{

x1= (-b/2*a);

x2= x1;

printf("Roots x1= %f and x2= %f are  real and equal", x1, x2);

}

else

{

x1= (-b/2*a);

x2= sqrt(abs(d))/(2*a);

printf("Real part= %f and imaginary part= %f are complex conjugates", x1,x2);

}

printf(\n\n\t\t Press any key to continue&);

getch();

}

0
dnice143
• 8
• 6
• 2
• +2
2 Solutions

Commented:
Almost all compilers have a flag to generate assembler output. gcc for example uses the -S flag :

gcc -S -c source.c

and the assembler will be in source.s
0

Commented:
cc -S
0

Author Commented:
so using a gcc compiler will convert the text for me?
0

Commented:
No, gcc is just a common compiler. You might be using a different compiler ... Are you ?
0

Commented:
>> No, gcc is just a common compiler.

What I meant by that is : you CAN use gcc to do it, but you don't HAVE to. Most compilers have an option to generate assembler output.
0

Author Commented:
So you are saying that it is possible to utilize assembly language to find roots to the quadratic equation. I downloaded Miricale Workbench on yesterday and It gave me an error when trying to link and compile.
0

Commented:
>> So you are saying that it is possible to utilize assembly language to find roots to the quadratic equation.

That was not what I was saying, but yes it's possible, although not very easy. Why would you want to do that anyway ? Just write the code in C, and let the compiler do its work and generate an executable. Do you have a specific reason ?

>> I downloaded Miricale Workbench on yesterday and It gave me an error when trying to link and compile.

That's most likely completely unrelated to the subject of this question ... Can you give a bit more background about what exactly you are trying to accomplish ?
0

Senior Software Engineer (Avast)Commented:
>> Is it possible to convert a program written in c to assembly language that will solve quadratic equation?

Do you you want to see disassembly? If do you can see this in Visual Studio if you put a break-point in the code you are interested in and then open the disassembly window from the debug menu.

The attached is just a snippet for your code, the full output is HUGE because it also includes all the standard libraries.

I hope this helps.

-Rx.
``````
--- c:\temp\testr\testr\testr.cpp ----------------------------------------------
// testr.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

// Program to calculate roots of a quadratic equation ie ax2+bx+c=0 //

# include <stdio.h>

# include <conio.h>

#include <math.h>

void main()

{
00401000  push        ebp
00401001  mov         ebp,esp
00401003  and         esp,0FFFFFFC0h
00401006  sub         esp,3Ch
00401009  push        esi

float a, b, c, d, x1,x2;

//            clrscr();

printf("Enter coefficients of quadratic equation ie a, b and c: ");
0040100A  mov         esi,dword ptr [__imp__printf (4020A4h)]
00401010  push        offset string "Enter coefficients of quadratic "... (4020F4h)
00401015  call        esi

scanf("%f%f%f", &a, &b, &c);
0040101A  lea         eax,[esp+38h]
0040101E  push        eax
0040101F  lea         ecx,[esp+38h]
00401023  push        ecx
00401024  lea         edx,[esp+38h]
00401028  push        edx
00401029  push        offset string "%f%f%f" (402130h)
0040102E  call        dword ptr [__imp__scanf (40209Ch)]

d=b*b-4*a*c;
00401034  fld         dword ptr [esp+44h]
00401038  fld         st(0)
0040103D  fmul        st(0),st
0040103F  fld         dword ptr [esp+30h]
00401043  fld         qword ptr [__real@4010000000000000 (402200h)]
00401049  fmul        st,st(1)
0040104B  fmul        dword ptr [esp+38h]
0040104F  fsubp       st(2),st
00401051  fxch        st(1)
00401053  fstp        dword ptr [esp+3Ch]

if (d>0)
00401057  fldz
00401059  fld         dword ptr [esp+3Ch]
0040105D  fcom        st(1)
0040105F  fnstsw      ax
00401061  test        ah,41h
00401064  jne         main+0B7h (4010B7h)
00401066  fstp        st(3)
00401068  fstp        st(1)
0040106A  fstp        st(0)

{

x1= (-b+(sqrt(d)))/(2*a);

x2= (-b-(sqrt(d)))/(2*a);
0040106C  call        _CIsqrt (401930h)
00401071  fstp        dword ptr [esp+3Ch]
00401075  fld         dword ptr [esp+30h]

printf("Roots x1= %f and x2= %f are real and unequal", x1, x2);
00401079  sub         esp,10h
0040107E  fld         dword ptr [esp+44h]
00401082  fld         st(0)
00401084  fchs
00401086  fld         dword ptr [esp+4Ch]
0040108A  fld         st(0)
0040108C  fsubp       st(2),st
0040108E  fxch        st(1)
00401090  fdiv        st,st(3)
00401092  fstp        dword ptr [esp+4Ch]
00401096  fld         dword ptr [esp+4Ch]
0040109A  fstp        qword ptr [esp+8]
0040109E  fsubrp      st(1),st
004010A0  fdivrp      st(1),st
004010A2  fstp        dword ptr [esp+4Ch]
004010A6  fld         dword ptr [esp+4Ch]
004010AA  fstp        qword ptr [esp]
004010AD  push        offset string "Roots x1= %f and x2= %f are real"... (402138h)
004010B2  jmp         main+13Dh (40113Dh)

}

else if (d==0)
004010B7  fucom       st(1)
004010B9  fnstsw      ax
004010BB  fstp        st(1)
004010BD  test        ah,44h
004010C0  jp          main+0E9h (4010E9h)
004010C2  fstp        st(0)
x2= (-b-(sqrt(d)))/(2*a);

printf("Roots x1= %f and x2= %f are real and unequal", x1, x2);

}

else if (d==0)

{

x1= (-b/2*a);

x2= x1;

printf("Roots x1= %f and x2= %f are  real and equal", x1, x2);
004010C4  sub         esp,10h
004010C7  fxch        st(1)
004010C9  fchs
004010CB  fmul        qword ptr [__real@3fe0000000000000 (4021F8h)]
004010D1  fmulp       st(1),st
004010D3  fstp        dword ptr [esp+4Ch]
004010D7  fld         dword ptr [esp+4Ch]
004010DB  fst         qword ptr [esp+8]
004010DF  fstp        qword ptr [esp]
004010E2  push        offset string "Roots x1= %f and x2= %f are  rea"... (402168h)

}

else
004010E7  jmp         main+13Dh (40113Dh)

}

else if (d==0)
004010E9  fstp        st(2)
004010EB  fstp        st(0)

{

x1= (-b/2*a);

x2= sqrt(abs(d))/(2*a);
004010ED  fabs

printf("Real part= %f and imaginary part= %f are complex conjugates", x1,x2);
004010EF  fstp        dword ptr [esp+3Ch]
004010F3  fld         dword ptr [esp+3Ch]
004010F7  call        _CIsqrt (401930h)
004010FC  fstp        dword ptr [esp+3Ch]
00401100  fld         dword ptr [esp+3Ch]
00401104  sub         esp,10h
00401107  fld         dword ptr [esp+40h]
0040110B  fld         st(0)
0040110F  fdivp       st(2),st
00401111  fxch        st(1)
00401113  fstp        dword ptr [esp+4Ch]
00401117  fld         dword ptr [esp+4Ch]
0040111B  fstp        qword ptr [esp+8]
0040111F  fld         dword ptr [esp+44h]
00401123  fchs
00401125  fmul        qword ptr [__real@3fe0000000000000 (4021F8h)]
0040112B  fmulp       st(1),st
0040112D  fstp        dword ptr [esp+4Ch]
00401131  fld         dword ptr [esp+4Ch]
00401135  fstp        qword ptr [esp]
00401138  push        offset string "Real part= %f and imaginary part"... (402194h)
0040113D  call        esi

}

printf("\n\n\t\t Press any key to continue&");
00401142  push        offset string "\x1c\n\n\t\t Press any key to continue&"... (4021D0h)
00401147  call        esi

getch();
0040114C  call        dword ptr [__imp___getch (4020A8h)]

}
00401152  xor         eax,eax
00401154  pop         esi
00401155  mov         esp,ebp
00401157  pop         ebp
00401158  ret
``````
0

Senior Software Engineer (Avast)Commented:
BTW: Is this code snippet thing new? I've not seen it until just now!!! :)
0

Commented:
>> BTW: Is this code snippet thing new? I've not seen it until just now!!! :)

Seems so ... First time I see it too - too bad you can only attach it at the end of the post.

evilrix, check this other question by the same asker : it clarifies a bit more what he's trying to do :

http://www.experts-exchange.com/Programming/Languages/C/Q_22961507.html
0

Senior Software Engineer (Avast)Commented:
Thanks "I8" -- you're name's too long to type so I have adopted this pseudo name -- I hope that's ok :)
0

Senior Software Engineer (Avast)Commented:
Erg -- assembly - yuk! :)

Actually, I8, I'm keen to get more into this assembly lark (I think a good C++ programmer should have some grounding) are you aware of any good books for complete idiots? I've done some scouring but found nothing useful. Of course, I'm more interested in theory than platform specific stuff.

BTW: No points for answering this -- but if you want I could open it as a proper Q... if you like :-p
0

Commented:
>> Thanks "I8" -- you're name's too long to type so I have adopted this pseudo name -- I hope that's ok :)

Call me 8 lol

For assembly : to be honest, I'm mostly self-taught. Since there are so many different platforms that each have their own version of assembler, it's pretty much impossible to have one book that covers all. Most of the time, the official architecture manuals are the most interesting. Here's a nice collection for Intel for example :

http://www.x86.org/intel.doc/inteldocs.htm
0

Senior Software Engineer (Avast)Commented:
>> Call me 8 lol
Tsk.. that would just be rude -- ok, if you insist, 8 :)

>> For assembly : to be honest, I'm mostly self-taught.
I like to think I am a damn good C++ programmer (there's nothing like a self inflated ego eh?) but frankly I wouldn't know a jmp from a cmp. I guess I shall just plug on with MASM and the few tutorials I have found.

I suppose I could also follow some  of the threads in the Assembly zone -- like the bomb one you diffused :)

Many thanks 8!

-Rx.
0

Commented:
>> like the bomb one you diffused

Those type of exercises are indeed fun challenges that teach you a lot about assembler. Just dive into the deep ... It's the best way to learn how to swim ... maybe lol
0

Graphics ExpertCommented:
Hi,

Not exactly what you are looking for, but I'd like to share an interesting research I did when working with IBM.
AIX, the IBM's Unix implementation for the RISC/6000 servers and workstations, had 3 compilers, xlc, xlp and xlf, respectively C, Pascal and Fortran. I have compiled a very single 'for' loop with a single instruction to increment an integer. I have used a command line option, like the proposed here, to generate Assembly code from the 3 languages . Surprisely, the result was absolutely the same for the 3 codes. This was around 1991. I think nowadays the compilers, mainly C or C++, are still better, and capable of very optimized assembly. So, it could be very instructive to watch ASM code from these compilers.

Anyway you'll notice that the math part could be human readable code. But the code for printf, for instance, would be really hard to understand...

Now, related to your question (I hope...)
To create an ASM code for    printf     you must prepare the pointers to your text before call an Operating System API function to send it to the display. So, besides your pure ASM code to the math, you must call Windows (or Linux if you work in that environment) to do interfaces and I/O.

Also, you'll spend a huge time to develop your own formatting routine to transform the float number at memory to characters in the screen (the '%f' printf formatting directive, for instance).

Then, if you are looking for SPEED, it could make sense to code the math in Assembler, but not the printf. You'll have no gain in the printf and will have a huge work for nothing.

Jose
0

Graphics ExpertCommented:
0

Commented:
Same as his other question ... Some good advice was given, and then dnice143 disappeared ...
0

Senior Software Engineer (Avast)Commented:
The following both answer the OPs Q: -

{ http:#20283774 }
{ http:#20287566 }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.