On Mon, Aug 29, 2005 at 07:28:07PM +0100, Jonathon McKitrick wrote:
> On Sun, Aug 28, 2005 at 09:45:26PM -0700, Matthew Dillon wrote:
> : 
> : :
> : :Is there a trick to calling malloc from NASM code?  It links fine and steps
> : :into malloc() fine, but returns a NULL pointer.
> : 
> :     Nope, should work just fine.  The assembly should look something like
> :     this (assuming we are talking about a userland program here):
> : 
> :     pushl   $bytes_to_malloc
> :     call    malloc
> :     addl    $4,%esp
> :     [ returns pointer in %eax ]
> 
> That's exactly what I thought.  And it works fine with gas.  But under NASM, I
> get a NULL pointer returned.  It steps into malloc, but doesn't return a mem
> pointer.

err() should tell you what was wrong(and terminates the program):

section .text
extern err
extern malloc

                :

        push    dword 0x80000000
        call    malloc
        add     esp, 4
        test    eax, eax
        jnz     .ok
.oops:
        ;; err(1, "malloc")
        push    .oops_msg
        push    dword 1
        call    err
        ; UNREACHED
.oops_msg:
        db      "malloc", 0
.ok:

Reply via email to