flat assembler
Message board for the users of flat assembler.

Index > Windows > Is it possible to alloc memory without apis?

Author
Thread Post new topic Reply to topic
snify



Joined: 02 Dec 2004
Posts: 39
snify 06 Jan 2005, 12:42
Can someone give me an example if it is possible cause I've seen something like this only in 16bit mode or on linux Question
Post 06 Jan 2005, 12:42
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 06 Jan 2005, 13:02
OK, it theory you can do it this way:

1. Allocate one very big uninitialized array in data section - it will not be included in the .exe file, but it will be accessible when you run your program.
2. Write your own procedures to allocate/free blocks from this array.

But the main question is why you need some tricky solutions??? If it is because of portability, there are other ways.

Regards
Post 06 Jan 2005, 13:02
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
snify



Joined: 02 Dec 2004
Posts: 39
snify 07 Jan 2005, 21:50
JohnFound, and if I want to do it with one section?
Post 07 Jan 2005, 21:50
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 08 Jan 2005, 00:49
snify wrote:
JohnFound, and if I want to do it with one section?


The same approach. Only keep your uninitialized data at the end of the section.
Post 08 Jan 2005, 00:49
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
snify



Joined: 02 Dec 2004
Posts: 39
snify 08 Jan 2005, 18:12
but I want also to realloc if needed... heh?
Post 08 Jan 2005, 18:12
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 08 Jan 2005, 18:19
That would be pretty hard to do, snify. On UNIX you have "brk" and "sbrk", but... you really should use whatever API the OS offers, or libc if the API sucks Smile
Post 08 Jan 2005, 18:19
View user's profile Send private message Visit poster's website Reply with quote
Nikolay Petrov



Joined: 22 Apr 2004
Posts: 101
Location: Bulgaria
Nikolay Petrov 09 Jan 2005, 13:49
John is right!
Code:
format PE GUI 4.0
include "%fasminc%\win32a.inc"
include "%fasminc%\macro\if.inc"
entry start

MAX_MEM = 0xFFFFFF

struc GLOBAL_MEM
{
 .count = 0
 .mem rb MAX_MEM
}


macro $import_libs [libs]
;dlls import section with dll's libs and import functions
{
  forward
   local _label
   if ~ libs#.redundant
      dd RVA libs#.lookup,0,0,RVA _label,RVA libs#.address
   end if
  common
   dd 0,0,0,0,0
  forward
   if ~ libs#.redundant
      _label db `libs#'.dll',0
   end if
  forward
   include '%fasminc%\apia\'#`libs#'.inc'
}

macro allocMem var,page,nbytes; if nbytes is constant
{
  page#.count = page#.count + nbytes
  if page#.count <= MAX_MEM
     virtual at page#.mem + page#.count
        var rb nbytes
     end virtual
  else
     page#.count = page#.count - nbytes
     display "Error: ",`page," memory is too low for ",`var,",",13,10,\
             "         and variable not defined!"
  end if
}
;page1.count=0 ;for one section
page1  GLOBAL_MEM ;added section .flat for uninitialized data
page2  GLOBAL_MEM

section '.alldata' import data readable writeable executable

$import_libs kernel32,user32


start:
        allocMem buffer1,page1,256
        ccall multicat,9,buffer1,ss1,ss2,ss3,ss1,ss2,ss3,ss1,ss2,ss3
        invoke  MessageBox,HWND_DESKTOP,buffer1,_title,MB_OK

        allocMem buffer2,page2,256
        ccall multicat,3,buffer2,ss2,ss1,ss3
        invoke  MessageBox,HWND_DESKTOP,buffer2,_title,MB_OK

        invoke  MessageBox,HWND_DESKTOP,buffer1,_title,MB_OK
        invoke  ExitProcess,0

cproc multicat,numsting,strBuffer
push esi edi
    xor ecx,ecx
    xor edx,edx
    mov edi,[strBuffer]
   .repeat
       mov esi,dword[ebp+4*(edx+4)]
    @@:
       lodsb
       test al,al
       jz @f
       stosb
       inc ecx
       jmp @r
    @@:
       inc edx
   .until edx,e,[numsting]
   mov byte[edi],0
   pop edi esi
   return
endp

_title db "Win32 Assembly",0
ss1 db "sting1 ",0
ss2 db "string2 ",0
ss3 db "string3 ",0
;page1.mem  rb MAX_MEM ;for one section    

May be you will try to think a little, how realloc or free memory - it's not unpossible. Wink
Post 09 Jan 2005, 13:49
View user's profile Send private message Reply with quote
snify



Joined: 02 Dec 2004
Posts: 39
snify 11 Jan 2005, 17:33
Yes, this code is good I like the multicat and the $import thing but.. Is there something like malloc written in fasm/masm? I know that it doesn't need any imports..
Post 11 Jan 2005, 17:33
View user's profile Send private message Visit poster's website Reply with quote
beppe85



Joined: 23 Oct 2004
Posts: 181
beppe85 11 Jan 2005, 19:46
snify wrote:
Yes, this code is good I like the multicat and the $import thing but.. Is there something like malloc written in fasm/masm? I know that it doesn't need any imports..

You can write a sub-allocator, you still need to request memory from OS, via VirtualAlloc, via a unitializated section, etc...
Post 11 Jan 2005, 19:46
View user's profile Send private message Reply with quote
drocon



Joined: 14 Nov 2004
Posts: 8
drocon 19 Jan 2005, 18:00
uhhhhhhh... guys, just use the stack

Code:
push 256
pop ebx
sub esp,ebx
mov edi,esp

;do something

add esp,ebx    


but dont go over it, normal buffers are usually no problem
Post 19 Jan 2005, 18:00
View user's profile Send private message Reply with quote
Nikolay Petrov



Joined: 22 Apr 2004
Posts: 101
Location: Bulgaria
Nikolay Petrov 19 Jan 2005, 22:19
Wink


Description:
Download
Filename: huff256.rar
Filesize: 1.98 KB
Downloaded: 346 Time(s)

Post 19 Jan 2005, 22:19
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.