push %rbp
mov %rsp,%rbp
sub $0x10,%rsp
movl $0x1,-0x8(%rbp)
jmp 0x4005c5 <main+72>
movl $0x1,-0x4(%rbp)
jmp 0x4005bb <main+62>
mov -0x8(%rbp),%eax
imul -0x4(%rbp),%eax
mov %eax,%ecx
mov -0x4(%rbp),%edx
mov -0x8(%rbp),%eax
mov %eax,%esi
mov $0x400664,%edi
mov $0x0,%eax
callq 0x400450 <printf@plt>
addl $0x1,-0x4(%rbp)
cmpl $0x9,-0x4(%rbp)
jle 0x400597 <main+26>
addl $0x1,-0x8(%rbp)
cmpl $0x9,-0x8(%rbp)
jle 0x40058e <main+17>
mov $0x0,%edi
callq 0x400480 <exit@plt>
Asked
Active
Viewed 259 times
0

Peter Cordes
- 328,167
- 45
- 605
- 847

gyanendra
- 83
- 7
-
3c++ to asm, a compiler. asm to c++, a decompiler; or your brain and time. But unless this is homework, is this a XY problem? – Jose Oct 30 '20 at 20:56
-
C++ to assembly is a many-to-one function. Given a compiler, an OS and a CPU, it’s well-defined. Going the other way you need a decompiler, but because it’s a one-to-many situation there a many, many possible c++ programs that could be produced from assembly; so in practice decompilers produce abominable code. If you really want to do this then I’d suggest googling “decompiler c++””. – Elliott Oct 30 '20 at 21:06
-
@Elliott can you elaborate a bit more. I am new to this language. – gyanendra Oct 30 '20 at 21:24
-
Trying to convert assembly to C++ is a very unusual goal to have, and so if you’re a beginner then I’d forget about it. I’ve been coding in C++ for 3 years and I’ve never tried to do this — probably few people have. – Elliott Oct 30 '20 at 21:28
-
Any solution will work, is there no any solution for this – gyanendra Oct 30 '20 at 21:44
-
1@Elliott: reverse-engineering asm into C or C++ manually is a decent way to understand the logic better, especially if it's too complex to follow the logic in your head just looking at the asm. Other than that, IDK what the real-world use cases are for mechanical decompilers that do it for you, producing C++ with meaningless variable names. Maybe if you had a binary-only library or program you wanted to use some functions from in something else? IDK, that problem doesn't happen in the open-source world; you'd just start with the original source. – Peter Cordes Oct 30 '20 at 21:45
-
@gyanendra: The procedure for asm->C is to either use a decompiler or do it manually. This function looks pretty small and simple, so by hand should be straightforward. You look at an instruction or group of insns and write a C statement like an `if()`, maybe fixing later if you realize a mistake. (You stripped code addresses / labels from your disassembly, so this starting point makes it impossible to see where branches go. Some disassemblers, like `objconv`, will actually insert branch labels, making asm that's ready to be assembled again if you wanted to do that.) – Peter Cordes Oct 30 '20 at 21:51
-
@PeterCordes, I can see why it’d be useful converting to C, but to convert asm to genuine C++ I’d imagine that you’d also lose track pretty quickly, no? – Elliott Oct 30 '20 at 21:54
-
@Elliott: you mean looking at C++ compiler output? Yeah, it's often harder to see what's going on if you don't have the source, especially in code using standard containers. Having checks to maybe grow a `std::vector` every iteration in a loop using `.push_back()` increases complexity. The OP's asm could well be from compiling some simple source as C++, but I assume you mean "genuine" = using a bunch of C++-only features and C++ standard library containers like std::string and `cout`, not `printf`. – Peter Cordes Oct 30 '20 at 21:58
-
Debug-mode asm is often easier to decompile (manually or otherwise). Optimized asm is often simple enough to just follow directly in its current form. (Again, unless it's implementing a bunch of complex logic, like C++ routinely does with some template libraries.) – Peter Cordes Oct 30 '20 at 22:00
-
@PeterCordes, yeah, I mean the exercise of learning or trying to understand the assembly code (assumedly we do this when we have no access to the source code). I’m a beginner in asm myself, but I could imagine trying to go from asm to C would be useful for this task, but I would have thought that going from asm to C++ (including C++-only features) would be too abstracted away from the asm to really be helpful. – Elliott Oct 30 '20 at 22:04
-
@Elliott: Well clearly this asm doesn't call any C++-only functions, just `printf` and `exit`, so turning it into C++ would just be pretty simple C++ using `int` and string literals. (To evaluate something based on a couple compile-time constants, so optimization would vastly simplify...) You could still write it as `int x{1}` or whatever if you're in a C++ mood. Or if you can see that some asm is looping over an array, writing `for( int &x : array )` is reasonable, if you want to jump to even higher level logic than the `do {} while()` that optimized asm typically uses. – Peter Cordes Oct 30 '20 at 22:11
-
@PeterCordes, okay. I see what you mean. Thanks for your responses, I appreciate it. – Elliott Oct 30 '20 at 22:17
-
can anybody tell me its exact conversion? – gyanendra Oct 30 '20 at 22:19
-
@gyanendra: Not from that source, because as I said you've stripped out the code addresses and labels so we don't know the target of each branch instruction, even if we did want to do your homework for you without you having made an attempt. [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – Peter Cordes Oct 30 '20 at 22:24
-
That’s not possible. It’s like me saying “a+b=16”, what were “a” and “b”? We can’t know. There’s lots of values they could’ve been. This code is small, but it still allows for lots of different corresponding C++ code. – Elliott Oct 30 '20 at 22:28
-
@PeterCordes why you think its a home work? cant you make me understand? Like I am curious to know more about assembly codes. – gyanendra Oct 30 '20 at 22:40
-
I assumed it's homework because *most* beginner asm questions on SO are homework. Another hint in that direction was that it seems "artificial": reverse-engineering some simple asm whose behaviour doesn't depend on any inputs to the function, just constants not `argc` (EDI) or `argv` (RSI). But if you just created this yourself by compiling something, that's fine. – Peter Cordes Oct 30 '20 at 22:46
-
One way to get started understanding how asm implements C++ logic is to look at how blocks of instructions map to source lines, e.g. on https://godbolt.org/, and see also [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) (especially Matt Godbolt's CppCon talk.) Once you've seen how compilation works "forwards", you should be able to reverse it manually for simple cases. – Peter Cordes Oct 30 '20 at 22:48
-
But you've made that extremely hard for this asm because you stripped out the first column that shows the address of each instruction. Which instruction does `jmp 0x4005c5
` jump to? We could figure that out by counting up the machine-code length of each instruction until we get to 72 from the top of the code (which I *assume* is `main+0`, but you removed that, too, from your `objdump -d` output). – Peter Cordes Oct 30 '20 at 22:51 -
@Elliott: many-to-one is fine: you just need to find *a* way of expressing it. In terms of your analogy, finding any `a` and `b` pair that satisfy `a+b = 16` is still useful, and still rules out a lot of numbers. Of course you can't recover the original source's variable names and comments, or even the exact way it chose to express its logic, like `x < 10` vs. `x <= 11`, or how it laid out its if / else logic or whatever. But that's fine, the goal is to find *a* sensible way to express the high-level operations going on. – Peter Cordes Oct 30 '20 at 22:54
-
@PeterCordes, hmm. I guess I was interpreting, but I took “ can anybody tell me its exact conversion?” as “what is the unique C++ code associated with this asm code?” I quite possibly was wrong there. – Elliott Oct 31 '20 at 03:40
-
How can i convert it directly to c? – gyanendra Oct 31 '20 at 19:53