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

  • Thread starter Thread starter ladesidude
  • Start date Start date
  • Tags Tags
    Assembly C++
AI Thread Summary
The discussion revolves around a C++ function and its corresponding assembly code, focusing on the accuracy of comments related to the assembly instructions. Key points include the clarification that the assembly code is missing initial setup instructions like "push ebp" and "mov esp, ebp," which are essential for establishing the stack frame. The participants note that the syntax used is reversed from the Intel standard, where destination and source operands are ordered differently, and that the "l" in instructions like "movl" is not part of the Intel standard. Additionally, there is mention of optimization levels affecting how function parameters are passed, with Microsoft’s _fastcall convention allowing for parameters to be passed in registers rather than on the stack. The conversation also touches on the purpose of translating simple functions to assembly, questioning whether it serves a practical purpose or is merely an academic exercise. Finally, it is suggested that users can generate assembly code from C++ compilers to better understand the translation process.
ladesidude
Messages
4
Reaction score
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
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.
 
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?
 
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.
 
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
19
Views
5K
Replies
6
Views
6K
Replies
7
Views
3K
Replies
19
Views
4K
Replies
4
Views
4K
Replies
2
Views
2K
Back
Top