Click here to Skip to main content
14,265,797 members
Rate this:
Please Sign up or sign in to vote.
According to my logic, I think it is possible to convert one language to another, without a transpiler.

C code:
#include <stdio.h>

int main(){
printf("Hello, world!");
return 0;
}


1. I have converted C code into Assembly code using MinGW (GDB) in CMD. This C program is a simple Hello-World program! The Assembly code is given below:

0x00401410 <+0>:     push   %ebp
0x00401411 <+1>:     mov    %esp,%ebp
0x00401413 <+3>:     and    $0xfffffff0,%esp
0x00401416 <+6>:     sub    $0x10,%esp
0x00401419 <+9>:     call   0x401950 <__main>
0x0040141e <+14>:    movl   $0x405044,(%esp)
0x00401425 <+21>:    call   0x403a90 <printf>
0x0040142a <+26>:    mov    $0x0,%eax
0x0040142f <+31>:    leave
0x00401430 <+32>:    ret


What I have tried:

Now, I have to convert this code to C++.

INSTEAD OF DOING THIS, I wish to use this assembly code in C++ which could be used through the so-called '_asm{}' function.

but i could not seem to find the syntax. i tried but i always get an error.

please help me.
thanks, any help is appreciated. 
Posted
Updated 4 days ago
Comments
Richard MacCutchan 4 days ago
   
That C code would be almost exactly the same in C++. It is not the generated code that will help you do it since even a full C++ program will generate non-OOP assembler.
Stefan_Lang 3 days ago
   
Well, C++ is not an OOP language, it's a general purpose language, that can be used to write procedural, OOP, functional, or other kind of programs. So, whether the code is OOP, doesn't matter.

What does matter, is what purpose could the resulting code serve, since it will effectively be (bad) C code with little structure, probably random symbol names, and no documentation.

The only reason I can fathom is porting a program to another platform by creating portable program code, no matter how bad it looks.
Richard MacCutchan 3 days ago
   
I don't think I said that it is an OOP language.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
but i could not seem to find the syntax. i tried but i always get an error.

Advice: Never do this, not like that!
The code in your comment show that you do not understand what you do, and it is a good thing that you get errors.
This piece of code:
0x00401410 <+0>:     push   %ebp
0x00401411 <+1>:     mov    %esp,%ebp
0x00401413 <+3>:     and    $0xfffffff0,%esp
0x00401416 <+6>:     sub    $0x10,%esp
0x00401419 <+9>:     call   0x401950 <__main>
0x0040141e <+14>:    movl   $0x405044,(%esp)
0x00401425 <+21>:    call   0x403a90 <printf>
0x0040142a <+26>:    mov    $0x0,%eax
0x0040142f <+31>:    leave
0x00401430 <+32>:    ret

is not autonomous, there are 2 calls to the standard library functions and you can see that 'Hello, world!' is not there.
Quote:
I have converted C code into Assembly code using MinGW (GDB) in CMD.

No, you have disassembled executable in memory, it is useful to understand what is doing a program but can't be used to make another program.
Advice: ask the compiler to generate assembly file as it compile your code, you will see that the assembly is not the same.

To do what you want, you need a very good understanding of differences in both languages:
- Registers usage
- Calling convention
- Naming scheme of routines in library

This kind of activity is for seasoned programmer, and there is very few cases where there a gain.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
Comments
Advik Raj 4 days ago
   
Well, here's my code as per the link:

#include<stdio.h>


int main() {

__asm__(
"push %ebp;"
"mov %esp,%ebp;"
"and $0xfffffff0,%esp;"
"sub $0x10,%esp;"
"call 0x401950 <__main>;"
"and $0xfffffff0, %esp;"
"sub $0x10, %esp;"
"call 0x401950 < __main >;"
"movl $0x405044, (%esp);"
"call 0x403a90 < printf >;"
"mov $0x0, %eax;"
"leave;"
"ret;"
);
return 0;
}


And here are the errors given by GCC Compiler.

C:\Users\xxxxxx\AppData\Local\Temp\cc8rm2fi.s: Assembler messages:
C:\Users\xxxxxx\AppData\Local\Temp\cc8rm2fi.s:18: Warning: missing operand; zer
o assumed
C:\Users\xxxxxx\AppData\Local\Temp\cc8rm2fi.s:18: Warning: missing operand; zer
o assumed
C:\Users\xxxxxx\AppData\Local\Temp\cc8rm2fi.s:18: Warning: missing operand; zer
o assumed
C:\Users\xxxxxx\AppData\Local\Temp\cc8rm2fi.s:18: Error: invalid operands (*ABS
* and *UND* sections) for `<'
C:\Users\xxxxxx\AppData\Local\Temp\cc8rm2fi.s:18: Error: invalid operands (*ABS
* and *UND* sections) for `<'
C:\Users\xxxxxx\AppData\Local\Temp\cc8rm2fi.s:18: Error: invalid operands (*ABS
* and *UND* sections) for `<'


Explain?
How do i Resolve this??
Patrice T 4 days ago
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Quote:
I have converted C code into Assembly code using MinGW (GDB) in CMD.
Nope, you didn't convert it. The gcc compiler compiled it to assembly.
In any case, while converting a piece of code from a programming language to another, you should know well both of them, in order to fully understand the original algorithm and provide a clean, proper implementation in the target language.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100