News:

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

How to create a string buffer?

Started by RHL, January 16, 2012, 03:14:15 AM

Previous topic - Next topic

RHL

Hello all! :)
I would know how to you create a string buffer? for store any value :)
I have said about malloc, but I see complicated :/

in runtime :P
thanks :B

dedndave

for reasonably small buffers
        .DATA?

Buffer  db 128 dup(?)

that method is good if you are going to continually re-use the buffer or if you need global scope

there are also numerous ways to allocate a temporary buffer on the stack
for example, inside a PROC...
SomeFunction PROC

        LOCAL   Buffer[128]:BYTE
;
;


for larger buffers, there are LocalAlloc, GlobalAlloc, HeapAlloc, and VirtualAlloc functions
each function has a little different characteristics - pick the one that best suits your needs

for most buffers less than a few hundred Mbytes, i generally use HeapAlloc - it is simple and fast
        INVOKE  GetProcessHeap
        mov     hHeap,eax
        INVOKE  HeapAlloc,eax,HEAP_ZERO_MEMORY,NumberOfBytes
        mov     hBlock,eax
;
;hBlock = address of heap block
;
;when done using the buffer, you must free it
;
        INVOKE  HeapFree,hHeap,NULL,hBlock


if you do not require that it be zero'ed, replace the "HEAP_ZERO_MEMORY" with "NULL"

RHL

thanks dedndave, the truth that I will use it in inline asm, and for that reason did not want to make a call to the API, for that be fast :B



dedndave

how large of a buffer do you need ?
is it temporary ?

RHL

I have the situation that, I cannot use the stack,API ( if posible)
The size I need to is 80-160 bytes and return the pointer of buffer , but this I can do, only I need a method :)

dedndave

well - if you cannot use the stack, then just put it in the .DATA? (or _BSS) section
        .DATA?

Buffer  db 160 dup(?)

        .CODE

        mov     eax,offset Buffer

RHL

Sorry, but edit my first post x3
I need in runtime : P

dedndave

ok
you can't use a global allocation
can't use the stack
use HeapAlloc

you can initialize the hHeap value at the beginning of the program
        INVOKE  GetProcessHeap
        mov     hHeap,eax


then use HeapAlloc and HeapFree inline
        INVOKE  HeapAlloc,hHeap,HEAP_ZERO_MEMORY,NumberOfBytes
        mov     hBlock,eax
;
;
;
        INVOKE  HeapFree,hHeap,NULL,hBlock

it's quite fast, especially for a small buffer
even faster if you do not need it to be zero'ed

RHL

Ok very nice, dedndave thanks!  :U