News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Allocate memory in MASM

Started by deeR44, June 07, 2020, 03:19:53 PM

Previous topic - Next topic

deeR44

I cannot allocate memory in a project I'm working on.

No matter how I try to use MASM32 library proc Malloc,
I only get this-- : error A2006:undefined symbol : Malloc

This usage was:

    INVOKE  Malloc,ln_input     ; length of input file

There is no error before the above statement nor one after
the statement. Period.

"ln_input" is a DWORD label containing the size of a file
which happens to be 862 bytes. I'm trying to allocate space
of an output file the same size as the input file even tho
it's going to be a little smaller.

It doesn't matter if I use upper case, lower case, precede
"ln_input" with 'ADDR' or not--any way I try code "Malloc",
I get the above error. Is there some off-the-wall trick to
using this Malloc proc?

hutch--

Its an oldie that I did not write, use "malloc" or direct GlobalAlloc() or HeapAlloc().

deeR44

Thank you for the heads up.
Where would I find info on how to use "malloc", GlobalAlloc(), or HeapAlloc()?

Vortex

Hi deeR44,

The traditional C function to allocate memory is malloc. To avoid conflicts with some Masm keywords, all the C run-time functions are renamed with the leading prefix crt_

The declaration of crt_malloc can be found in \masm32\include\msvcrt.inc

Here is a quick example :

include     \masm32\include\masm32rt.inc

.data

nSize       dd MAX_COMPUTERNAME_LENGTH+1

.data?

pMemory     dd ?

.code

start:

    invoke  crt_malloc,\
            MAX_COMPUTERNAME_LENGTH+1

    mov     pMemory,eax

    invoke  GetComputerName,eax,ADDR nSize

    invoke  StdOut,pMemory

    invoke  crt_free,pMemory

    invoke  ExitProcess,0

END start

jj2007

Quote from: deeR44 on June 07, 2020, 04:06:53 PM
Where would I find info on how to use "malloc", GlobalAlloc(), or HeapAlloc()?

As Vortex suggests, \masm32\include\*.inc is one place to investigate. Unfortunately, it's not the only one - there are many help files to look for. xHelp for qEditor.exe is a solution that works fine.

hutch--

Use the "High Level Macro Help" file that comes with MASM32. Look for Macro Categories/Memory Allocation and the two macros that wrap GlobalAlloc().

Fixed Memory
    alloc  Allocate a fixed block of memory
    free  Free a fixed block of memory

Apologies but the "malloc" I referred to was the 64 bit version that would be no use to you.


deeR44

A while back, I tied one of the allocation macros and it returned the exact same address that disk_read_file did when I read a file into memory a few moments prior. I triple checked the two addresses. Couldn't believe it.

deeR44

VORTEX

Thank you for the info but I am unfamiliar with MAX_COMUTERNAME_LENGTH AND GetComputerName.
Don't know what they are or where they come from nor how they are used.

deeR44

JJ2007
Thank you for the info. I'll take a look at your suggestions.

deeR44

hutch--
Apology accepted! I don't do 64 bit stuff. I'm having enough trouble with half that.

jj2007

Quote from: deeR44 on June 08, 2020, 01:33:46 PM
A while back, I tied one of the allocation macros and it returned the exact same address that disk_read_file did when I read a file into memory a few moments prior. I triple checked the two addresses. Couldn't believe it.

Most probably you released the disk_read_file memory after you were done with it (and that is the correct procedure). So the system can re-use it. This is speculation, though - if you run into such problems, zip the source and post it here.

Vortex

Hi deeR44,

The documentation of the API GetComputerName :

https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcomputernamea

The purpose of my small example is to retrieve the computer name. The API GetComputerName needs a buffer to receive the computer name. This buffer is created with the help of the function mallocMAX_COMPUTERNAME_LENGTH is a constant defined in \masm32\include\windows.inc  This constant value speficies the size of the buffer to hold the computer name. In other words, the computer name can have a maximum length of MAX_COMPUTERNAME_LENGTH ( not including the last NULL terminator. )

deeR44

jj,
Thank you sir! "Most probably you released the disk_read_file memory after you were done with it (and that is the correct procedure). So the system can re-use it."

That is exactly what happened. I never thought of it. It will not happen again.

deeR44

Vortex,
Thank you for the info. Looks like I can look up other Windows API stuff at your link. I know next to nothing about the zillions off API functions nor where to find stuff about them. I rely heavily on the MASM32 libs. I guess a lot of them are wrappers to API functions. I seem to remember seeing something about that.

hutch--

Get yourself a copy of the old Win32.hlp file. If you are using Win10, there is a zip file from JimG so you can use the old HLP format.

http://masm32.com/board/index.php?topic=5201.0

It takes a while but once you get used to the API naming conventions, they start to make sense.

This link works.

https://github.com/trietptm/OllyDbg-Archive/blob/master/HLP-Files/win32.hlp