The MASM Forum

General => The Campus => Topic started by: deeR44 on June 07, 2020, 03:19:53 PM

Title: Allocate memory in MASM
Post by: deeR44 on June 07, 2020, 03:19:53 PM
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?
Title: Re: Allocate memory in MASM
Post by: hutch-- on June 07, 2020, 03:29:03 PM
Its an oldie that I did not write, use "malloc" or direct GlobalAlloc() or HeapAlloc().
Title: Re: Allocate memory in MASM
Post by: deeR44 on June 07, 2020, 04:06:53 PM
Thank you for the heads up.
Where would I find info on how to use "malloc", GlobalAlloc(), or HeapAlloc()?
Title: Re: Allocate memory in MASM
Post by: Vortex on June 07, 2020, 06:32:11 PM
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
Title: Re: Allocate memory in MASM
Post by: jj2007 on June 07, 2020, 08:52:11 PM
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 (http://masm32.com/board/index.php?topic=531.0) is a solution that works fine.
Title: Re: Allocate memory in MASM
Post by: hutch-- on June 07, 2020, 11:15:10 PM
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.

Title: Re: Allocate memory in MASM
Post by: 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.
Title: Re: Allocate memory in MASM
Post by: deeR44 on June 08, 2020, 01:43:56 PM
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.
Title: Re: Allocate memory in MASM
Post by: deeR44 on June 08, 2020, 01:49:04 PM
JJ2007
Thank you for the info. I'll take a look at your suggestions.
Title: Re: Allocate memory in MASM
Post by: deeR44 on June 08, 2020, 01:51:02 PM
hutch--
Apology accepted! I don't do 64 bit stuff. I'm having enough trouble with half that.
Title: Re: Allocate memory in MASM
Post by: jj2007 on June 08, 2020, 06:04:02 PM
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.
Title: Re: Allocate memory in MASM
Post by: Vortex on June 08, 2020, 06:39:47 PM
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. )
Title: Re: Allocate memory in MASM
Post by: deeR44 on June 09, 2020, 03:13:35 PM
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.
Title: Re: Allocate memory in MASM
Post by: deeR44 on June 09, 2020, 03:21:08 PM
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.
Title: Re: Allocate memory in MASM
Post by: hutch-- on June 09, 2020, 04:49:19 PM
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
Title: Re: Allocate memory in MASM
Post by: Vortex on June 10, 2020, 05:02:40 AM
Hi deeR44,

Windows programming is based on the API functions. There are a lot of them but don't worry : Step by step, one can study them methodically. A lot of Masm32 modules are using APIs, that's true. That's necessary to communicate with the operating system.
Title: Re: Allocate memory in MASM
Post by: deeR44 on June 10, 2020, 02:40:07 PM
Quote from: Vortex on June 10, 2020, 05:02:40 AM
Hi deeR44,

Windows programming is based on the API functions. There are a lot of them but don't worry : Step by step, one can study them methodically. A lot of Masm32 modules are using APIs, that's true. That's necessary to communicate with the operating system.
Where does one go to learn how to use them, what they do, and what arguments they need?
Title: Re: Allocate memory in MASM
Post by: TimoVJL on June 10, 2020, 04:47:42 PM
For example, Win32.chm
https://sourceforge.net/projects/win32-help-chm/

Windows API Index (https://docs.microsoft.com/en-us/windows/win32/apiindex/windows-api-list)
Memory Management Functions (https://docs.microsoft.com/en-us/windows/win32/memory/memory-management-functions)
Title: Re: Allocate memory in MASM
Post by: hutch-- on June 10, 2020, 10:58:39 PM
deeR44,

API calls are just functions, there are a very large number of them but you only need to remember a couple of dozen, much the same with messages. This is why I recommended getting the old WIN32.HLP help file, it had all of the old core windows API functions and to make life easier, almost every argument is DWORD in size, you just need to keep an eye on the difference between a DWORD value and a DWORD address (pointer) and the rest of it generally makes sense.

It is a messaging architecture, once you get the swing of the basic executable layout, a main, a message loop and a WndProc the rest of it is just detail.
Title: Re: Allocate memory in MASM
Post by: deeR44 on July 02, 2020, 04:08:28 PM
How does one use the old "WIN32.HLP" file, assuming that I can find it? I have nothing that uses ".hlp" files. Thank you for any advice.
Title: Re: Allocate memory in MASM
Post by: TouEnMasm on July 02, 2020, 05:20:51 PM

Help system are numerous but there is one you couldn't avoid it is Internet.
Type "msdn malloc" in a search engine and you will have an answer in a few seconds.
malloc is the favorite function to allocate memory in the CRT,it use heap and there is further functions using it.
Type type "msdn heap" and you will Know all on Heap.
Title: Re: Allocate memory in MASM
Post by: hutch-- on July 02, 2020, 05:32:41 PM
The reason to have the old WIN32.HLP is that you can have it locally on your own machine. If you are running Win10, you will need to get the file by JimG in the forum to run it. It is between 12 and 24 meg depending on the version you can find and you need to get used to using it but it has all of the messages and functions of the core Windows API.

For later stuff you use MSDN.
Title: Re: Allocate memory in MASM
Post by: iZ! on July 04, 2020, 04:05:42 AM
Quote from: deeR44 on June 07, 2020, 03:19:53 PM
I cannot allocate memory in a project I'm working on.

Try this:

invoke GlobalAlloc,0,dataLength
mov  hMem,eax
invoke GlobalLock,hMem
mov  ptrMem,eax
...


And when you're done, free the memory up by:
     invoke GlobalFree, hMem