C++ to Asm 
Author Message
 C++ to Asm


>From an Asm point of view, what do the C++ keywords "new" and "delete" do??


Fri, 23 Mar 2001 03:00:00 GMT  
 C++ to Asm
it's been long since i've disassembled c++ code, and different compilers
do it differently, but it usually looks something like this(16 bit code
and stack parameter passing):

        CMyClass *myClassPtr = new CMyClass(5);  // pass a 5 to the constructor
of the class

asm would look something like this:

        mov     ax,20h  ; lets say the size of our class is 20h bytes
        push    ax      ; push it on stack
        call    malloc  ; return pointer to allocated memory in AX
        mov     [myClassPtr],ax
        or      ax,ax   ; check to see if mem alloc successfull
        jz      bad_alloc

        mov     ax,5
        push    ax      ; ax = 5, which is the value to send to the constructor
        mov     ax,[myClassPtr] ; ax = pointer to new class object
        push    ax      ; give address of object to constructor
        call    CMyClass        ; call the contructor of the object


        delete myClassPtr;

        mov     ax,[myClassPtr]         ; ax = pointer to class object
        or      ax,ax                   ; test if it's valid
        jz      not_valid

        push    ax              ; send address of object to destructor
        call    ~CMyClass       ; call descturcor

        push    ax              ; send address of object to free()
        call    free            ; free the memory


hope this helps.  Oh, some compilers may not provide all of the error
checking here, but mine does, so that's why I wrote it.


Fri, 23 Mar 2001 03:00:00 GMT  
 C++ to Asm


>>From an Asm point of view, what do the C++ keywords "new" and "delete" do??

>                                Thanks,

Ok...  If you're working in DOS, they call Int 21h, functions 48h and 49h.  These functions get DOS's memory manager to
allocate some memory for you (BX is the # of paragraphs, but I'm NOT SURE) and then frees it.  Unfortuately, I can't
remember the register values.  Either ES or BX returns the segment of the allocated memory...  Well, I hope that now you
at least know where to look for info.

Fri, 23 Mar 2001 03:00:00 GMT  
 C++ to Asm
I don't think that the C runtime memory allocation functions use dos.
but, I could be wrong.  Usually with C programs, they have to
allocate(sometimes) a lot of little memory chunks(like class objects),
that may be 30-40 bytes or less.  A 16 byte overhead on everyone would
waste alot of memory.  I'm pretty sure the C runtime functions have
their own methods of organizing the data segments.


Sat, 24 Mar 2001 03:00:00 GMT  
 [ 4 post ] 

 Relevant Pages 

1. Passing arguments from C++ to ASM with Borland C++

2. C++ to asm

3. converting C++ to ASM

4. C++ to Asm

5. Please I would appreciate some input, Far pointer from C++ to asm

6. C++ And ASM

7. C/C++ to ASM

8. getting global variable from c++ in asm (MASM)

9. C++ to asm

10. Hmm C++ or Asm??

11. C++ and ASM

12. linking C++ and asm objects


Powered by phpBB® Forum Software