Calling realloc from assembly block

General FreeBASIC programming questions.
Zamaster
Posts: 1024
Joined: Jun 20, 2005 21:40
Contact:

Calling realloc from assembly block

Postby Zamaster » Feb 11, 2016 23:16

I assumed it would be something like:

Code: Select all

asm
   push      ebx          'num bytes
   push      ecx          'address
   call      reallocate
   add        esp,                   8
   mov      ecx,         eax   'get result of function call
end asm


But I get a big 'ol crash. What should I have done differently? Thanks!
Zamaster
Posts: 1024
Joined: Jun 20, 2005 21:40
Contact:

Re: Calling realloc from assembly block

Postby Zamaster » Feb 12, 2016 0:21

Even weirder, if I debug it in x32dbg it runs without a problem (can even print stuff after the block), so whats wrong here? Here's the source listing from the member function making the call:

Code: Select all

    sub DArray.reserve naked cdecl(byref _this as DArray, _elements as size_t)
        asm
        #ifdef __FB_64BIT__

        #else
                mov     edx,                dword ptr [esp+4]
                mov     ecx,                dword ptr [edx+8]
                mov     eax,                dword ptr [edx+4]
                add     eax,                dword ptr [esp+8]
                cmp     eax,                ecx
                jb      dsm_darray_reserve_return_32
            dsm_darray_reserve_doublememoryloop_32:
                shl     ecx,                1
                cmp     ecx,                eax
                jb      dsm_darray_reserve_doublememoryloop_32
                push    ecx
                push    dword ptr [edx]
                call    reallocate
                add     esp,                8
                mov     dword ptr [edx],    eax
            dsm_darray_reserve_return_32:
                ret
        #endif
        end asm
    end sub
Zamaster
Posts: 1024
Joined: Jun 20, 2005 21:40
Contact:

Re: Calling realloc from assembly block

Postby Zamaster » Feb 12, 2016 0:58

Okay okay I'm dumb yeah I see it HA, forgive me I was having a special moment
D.J.Peters
Posts: 8023
Joined: May 28, 2005 3:28
Contact:

Re: Calling realloc from assembly block

Postby D.J.Peters » Feb 12, 2016 1:28

Normaly there isn't a "reallocate"
all exported FB stuff has an "fb_" in front so far I remember.
try "fb_reallocate" instead

Joshy
Zamaster
Posts: 1024
Joined: Jun 20, 2005 21:40
Contact:

Re: Calling realloc from assembly block

Postby Zamaster » Feb 12, 2016 3:55

actually, "call reallocate" works just fine!
D.J.Peters
Posts: 8023
Joined: May 28, 2005 3:28
Contact:

Re: Calling realloc from assembly block

Postby D.J.Peters » Feb 12, 2016 5:19

@Zamaster Yes but reallocate calls than fb_reallocate so if you do low level assembler coding why not using fb_allocate directly ?
If you use the FB runtime directly (it's C calling) you don't need to restore the stack from assembler :-)
How ever have fun with assembler.

Joshy
Zamaster
Posts: 1024
Joined: Jun 20, 2005 21:40
Contact:

Re: Calling realloc from assembly block

Postby Zamaster » Feb 12, 2016 7:03

I didn't even know about FB exported functions until you said so! I'll certainly keep that in mind going forward. I ended up calling a different procedure inside the routine anyway, mostly wanted to keep the common case fast since reallocates should be rare in this code.
dkl
Site Admin
Posts: 3210
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Calling realloc from assembly block

Postby dkl » Feb 12, 2016 7:10

FB's reallocate() is just an alias for the CRT realloc(), and fbc even does this replacement for you in the inline ASM (you can check by compiling with -R and looking at the generated .asm file).

Return to “General”

Who is online

Users browsing this forum: Imortis and 3 guests