C++ to Assembly: Convert, Interpret & Understand

  • C/C++
  • Thread starter ladesidude
  • Start date
  • Tags
    Assembly C++
In summary: Alternatively, online compilers such as Code::Blocks and Visual Studio allow you to create asm code directly.
  • #1
ladesidude
4
0
Hi all,

I have this small function in C++

void myfunc(int a, int b) {
int c = 1;
int d = 2;
c += b;
d += a;
return;

}

the assembly code and my comments follow:

subl $8, %esp ;; subtract 8 from %esp, what we are doing here is decrementing the stack pointer by 8 and then writing the value at the new top of stack address, used for allocating space for local variables.

movl $1, -4(%ebp) ;; copy 1 (which is the values stored in y) at the location using %ebp as the base address with an offset -4, the register is a pointer, the displacement specified how far from the pointer

movl $2, -8(%ebp) ;; copy 2 (which is the value stored in z) at the location using %ebp as the base address with an offset -8, the register is a pointer, the displacement specified how far from the pointer

movl 12(%ebp), %edx ;; copy what is at %ebp + offset 12 into %edx, what this means that the last instruction pushed to the stack is copied to %edx

leal -4(%ebp), %eax ;; this is a variant of movl and instead of copying the data at %ebp + offset -4, its storing the effective address into the destination

addl %edx, (%eax)

movl 8(%ebp), %edx

leal -8(%ebp), %eax

addl %edx, (%eax)


Am I correct in the comments of the lines, if not can someone please help me, also I am confused on leal. Thanks a bunch
 
Technology news on Phys.org
  • #2
Your comment on the first leal is correct, it's a load effective address instruction.

Some of the assembly code is missing. There shoud be a push ebp, then a mov esp,ebp, before the first instruction you have. After this, [epb+0] = original ebp, [epb+4] = return address, [ebp+8] = first function parameter, [ebp+12] = second function parameter. [ebp-4] = first local variable, [ebp-8] = second local variable.

Note that this syntax is reversed from the Intel standard, where the operands are ordered as destination, source. Also the "l" such as movl, aren't used in the Intel standard, since operand size is determined by register name or specific declartion (dx or word ptr for 16 bit, edx or dword ptr for 32 bit, rdx or qword ptr for 64 bit).

Depending on the level of optimization, some of the function parameters are in registers instead on the stack. Microsoft has _fastcall convention as an option for 32 bit code. For 64 bit code in Microsoft environment the variations were done away with and the convention is similar to the _fastcall convention of 32 bit code, were the first 4 parameters are located in registers. Even though the parameters are located in registers, rsp is subtracted as if the paramters were passed on the stack, as a default place to store the parameters if the called function wishes to use the space.
 
  • #3
This function doesn't in fact do anything, what is the sense of translating it to assembly? Or is it just an "art for art's sake" exercise?
 
  • #4
Jeff Reid said:
Your comment on the first leal is correct, it's a load effective address instruction.

Some of the assembly code is missing. There shoud be a push ebp, then a mov esp,ebp, before the first instruction you have. After this, [epb+0] = original ebp, [epb+4] = return address, [ebp+8] = first function parameter, [ebp+12] = second function parameter. [ebp-4] = first local variable, [ebp-8] = second local variable.

Note that this syntax is reversed from the Intel standard, where the operands are ordered as destination, source. Also the "l" such as movl, aren't used in the Intel standard, since operand size is determined by register name or specific declartion (dx or word ptr for 16 bit, edx or dword ptr for 32 bit, rdx or qword ptr for 64 bit).

Depending on the level of optimization, some of the function parameters are in registers instead on the stack. Microsoft has _fastcall convention as an option for 32 bit code. For 64 bit code in Microsoft environment the variations were done away with and the convention is similar to the _fastcall convention of 32 bit code, were the first 4 parameters are located in registers. Even though the parameters are located in registers, rsp is subtracted as if the paramters were passed on the stack, as a default place to store the parameters if the called function wishes to use the space.
great answer, through I'm only familar with the intel standard.
 
  • #5
Most C++ compilers can generate assembly code as output. To see what Jeff means by "missing" look into your compiler's manual to find the options to create asm as the endpoint of compilation.
 

Related to C++ to Assembly: Convert, Interpret & Understand

1. What is the purpose of converting C++ to Assembly code?

Converting C++ to Assembly code allows for a lower level understanding of how a program works and can help with optimizing and debugging. It can also be useful for embedded systems where memory and processing power are limited.

2. How do you convert C++ code to Assembly?

C++ code can be converted to Assembly using a compiler, which translates the high-level C++ code into low-level Assembly instructions. The resulting Assembly code can then be interpreted and executed by the computer's processor.

3. Can Assembly code be directly interpreted by a computer?

No, Assembly code needs to be converted into machine code, which is a series of binary instructions that the computer's processor can understand and execute.

4. Is it difficult to understand Assembly code?

Assembly code is considered a low-level programming language and can be more challenging to understand compared to high-level languages like C++. It requires knowledge of computer architecture and the specific Assembly language being used.

5. Is it necessary to understand Assembly code in order to program in C++?

No, it is not necessary to understand Assembly code in order to program in C++. However, having a basic understanding of Assembly can be beneficial for optimizing and debugging C++ code.

Similar threads

  • Programming and Computer Science
Replies
19
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
4
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
4K
Back
Top