News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Alloc

Started by jj2007, October 10, 2009, 08:17:40 AM

Previous topic - Next topic

jj2007

I stumbled by accident over this:

include \masm32\include\masm32rt.inc

.code
start: int 3
mov eax, Alloc(4)
exit

end start



I mistook Alloc for my own macro but then found out that I have no such macro. It's also not in macros.asm...
eax contains offset 401010h
::)

BlackVortex

I assembled it (without the int3) and it produces this strangeness :

00401000 >  B8 10104000     MOV EAX,testbed.00401010
00401005    6A 00           PUSH 0
00401007    E8 34000000     CALL <JMP.&kernel32.ExitProcess>
0040100C    55              PUSH EBP
0040100D    8BEC            MOV EBP,ESP
0040100F    833D 00304000 0>CMP DWORD PTR DS:[403000],0
00401016    75 16           JNZ SHORT testbed.0040102E
00401018    68 00304000     PUSH testbed.00403000
0040101D    6A 01           PUSH 1
0040101F    E8 22000000     CALL <JMP.&ole32.CoGetMalloc>
00401024    0BC0            OR EAX,EAX
00401026    74 06           JE SHORT testbed.0040102E
00401028    33C0            XOR EAX,EAX
0040102A    C9              LEAVE
0040102B    C2 0400         RET 4
0040102E    FF75 08         PUSH DWORD PTR SS:[EBP+8]
00401031    8B0D 00304000   MOV ECX,DWORD PTR DS:[403000]
00401037    8B09            MOV ECX,DWORD PTR DS:[ECX]
00401039    FF51 0C         CALL DWORD PTR DS:[ECX+C]
0040103C    C9              LEAVE
0040103D    C2 0400         RET 4
00401040  - FF25 00204000   JMP DWORD PTR DS:[<&kernel32.ExitProcess>; kernel32.ExitProcess
00401046  - FF25 08204000   JMP DWORD PTR DS:[<&ole32.CoGetMalloc>]  ; ole32.CoGetMalloc

Can someone explaing the intended use and funcitonality ?

hutch--

JJ,

Its one of Ernie Murphy's modules in the masm32 library. I use the macro name "alloc" which avoids the problem.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

Quote from: hutch-- on October 10, 2009, 11:54:19 AM
Its one of Ernie Murphy's modules in the masm32 library. I use the macro name "alloc" which avoids the problem.

Hutch,
Just out of curiosity: How does the "problem" creep into the executable? I searched *.inc and macros.asm for a macro but no success; and mov eax, Alloc(n) is clearly a macro call...

dedndave

from masm32\macros\macros.asm

      alloc MACRO bytecount
        invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,bytecount
        EXITM <eax>
      ENDM

Ernies proc, from masm32\m32lib\alloc.asm

Alloc proc public cb:DWORD
   
    ; -------------------------------------------------------------
    ; Alloc will allocate cb bytes
    ;
    ; The Alloc method allocates a memory block in essentially the same
    ; way that the C Library malloc function does.
    ;
    ; The initial contents of the returned memory block are undefined
    ; there is no guarantee that the block has been initialized, so you should
    ; initialize it in your code. The allocated block may be larger than cb bytes
    ; because of the space required for alignment and for maintenance information.
    ;
    ; If cb is zero, Alloc allocates a zero-length item and returns a valid
    ; pointer to that item. If there is insufficient memory available, Alloc
    ; returns NULL.
    ;
    ; Note Applications should always check the return value from this method,
    ; even when requesting small amounts of memory, because there is no guarantee
    ; the memory will be allocated
    ;
    ;
    ; EXAMPLE:
    ; invoke Alloc, 128         ; allocates 128 bytes
    ;                           ; pointer to memory in eax
    ;
    ; Uses: eax, ecx, edx.
    ;
    ; -------------------------------------------------------------
   
    .IF !Alloc_pIMalloc     ; check if we hold a valid pointer
        invoke CoGetMalloc,1 , ADDR Alloc_pIMalloc
        .IF eax
            ; failed getting pIMalloc
            xor eax, eax    ;  NULL return pointer
            ret
        .ENDIF
    .ENDIF
    ; now request the memory
    push cb
    mov ecx, Alloc_pIMalloc
    mov ecx, [ecx]
    call DWORD PTR [ecx] + 12
    ret

Alloc endp

qWord

strange ... the following code compiles without error:

start:
.code

bla proto :DWORD

mov eax,bla(100)


bla proc x:DWORD

xor eax,eax
ret

bla endp

end start


EDIT: I've got it: bla(imm32) returns the offset of the function + imm32
e.g.: bla(0) returns the offset of function 'bla'

EDIT2: you can use it with any function: Offset = FunctionName(FncRelativOffset). I've also test it with ml 6-10 -no problems. Did anyone knows this syntax - it absolute new to meĀ  :dazzled:
FPU in a trice: SmplMath
It's that simple!

jj2007

Quote from: dedndave on October 10, 2009, 03:39:17 PM
from masm32\macros\macros.asm

      alloc MACRO bytecount
        invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,bytecount
        EXITM <eax>
      ENDM

Ernies proc, from masm32\m32lib\alloc.asm

Alloc proc public cb:DWORD

Quote from: qWord on October 10, 2009, 03:58:05 PM
I've got it: bla(imm32) returns the offset of the function + imm32
e.g.: bla(0) returns the offset of function 'bla'

Yep, that's the solution. No macro involved - Masm links Alloc statically, and returns the offset. We keep learning :bg
Thanks, Dave & qWord :U