Assembly Language for Beginners

(Jeff_L) #1

3.18. C++


This function in itsinternal representationhas only one argument, used as pointer to the object (this).


This function could be rewritten in C like this:


void ZN1c4dumpEv (int obj)
{
printf ("%d; %d\n",
obj, *(obj+1));
};


Thus, if we base our judgment on these simple examples, the difference between MSVC and GCC is the
style of the encoding of function names (name mangling) and the method for passing a pointer to the
object (via theECXregister or via the first argument).


GCC: x86-64


The first 6 arguments, as we already know, are passed in theRDI,RSI,RDX,RCX,R8andR9([Michael Matz,
Jan Hubicka, Andreas Jaeger, Mark Mitchell,System V Application Binary Interface. AMD64 Architecture
Processor Supplement, (2013)]^26 ) registers, and the pointer tothisvia the first one (RDI) and that is what
we see here. Theintdata type is also 32-bit here.


TheJMPinstead ofREThackis also used here.


Listing 3.87: GCC 4.4.6 x64

; default ctor


_ZN1cC2Ev:
mov DWORD PTR [rdi], 667
mov DWORD PTR [rdi+4], 999
ret


; c(int a, int b)


_ZN1cC2Eii:
mov DWORD PTR [rdi], esi
mov DWORD PTR [rdi+4], edx
ret


; dump()


_ZN1c4dumpEv:
mov edx, DWORD PTR [rdi+4]
mov esi, DWORD PTR [rdi]
xor eax, eax
mov edi, OFFSET FLAT:.LC0 ; "%d; %d\n"
jmp printf


Class inheritance


Inherited classes are similar to the simple structures we already discussed, but extended in inheritable
classes.


Let’s take this simple example:


#include <stdio.h>


class object
{
public:
int color;
object() { };
object (int color) { this->color=color; };
void print_color() { printf ("color=%d\n", color); };
};


(^26) Also available ashttps://software.intel.com/sites/default/files/article/402129/mpx-linux64-abi.pdf

Free download pdf