flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > out of memory error when i try compile this code

Author
Thread Post new topic Reply to topic
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 18 Jan 2019, 18:26
Code:
macro   lea     Dst,Src
{
local ...Byte,...Zero
  virtual at $
  ...Zero:
        lea     eax,Src
  load ...Byte byte from ...Zero
  end virtual
}

macro   xor     Dst,Src
{
 if (Dst eqtype Src)&(Dst eq Src)&(Dst in <rax,rbx,rcx,rdx,rsi,rdi,rsp,rbp>)
   if Dst in <rax>
        xor     eax,eax
   else if Dst in <rbx>
        xor     ebx,ebx
   else if Dst in <rcx>
        xor     ecx,ecx
   else if Dst in <rdx>
        xor     edx,edx
   else if Dst in <rsi>
        xor     esi,esi
   else if Dst in <rdi>
        xor     edi,edi
   else if Dst in <rsp>
        xor     esp,esp
   else if Dst in <rbp>
        xor     ebp,ebp
   end if
 else
        xor     Dst,Src
 end if
}

macro   mov     Dst,Src
{
  if (Dst eqtype eax)&(Src eqtype 0)&(Src relativeto $)
    if Dst in <rax>
        lea     eax,[Src]
    else if Dst in <rbx>
        lea     ebx,[Src]
    else if Dst in <rcx>
        lea     ecx,[Src]
    else if Dst in <rdx>
        lea     edx,[Src]
    else if Dst in <rsi>
        lea     esi,[Src]
    else if Dst in <rdi>
        lea     edi,[Src]
    else if Dst in <rsp>
        lea     esp,[Src]
    else if Dst in <rbp>
        lea     ebp,[Src]
    else if Dst in <eax,ebx,ecx,edx,esi,edi,esp,ebp,r8d,r9d,r10d,r11d,r12d,r13d,r14d,r15d,r8,r9,r10,r11,r12,r13,r14,r15>
        lea     Dst,[Src]
    else
        mov     Dst,Src
    end if
  else
        mov     Dst,Src
  end if
}

macro   mov     Dst,C
{
  if (Dst eqtype eax)&((C eqtype 0)&~(C relativeto $))&(C eqtype 0)&(C=0)
        xor     Dst,Dst
  else
    if (Dst in <rbp>)&(C in <rsp>)
        mov     ebp,esp
    else
        mov     Dst,C
    end if
  end if
}

        use64
rept 64*1024
{
        mov     dx,[rsp]
        mov     al,1
}    

_________________
smaller is better
Post 18 Jan 2019, 18:26
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 18 Jan 2019, 18:40
In fasm 1 architecture the entire preprocessed source is kept in memory (you can see it when you generate .FAS file) and REPT is preprocessor's directive, so all copies of text generated by REPT are accumulated in memory, which can quickly run out - especially in this case, where "mov" and in turn "xor" are also macros, so the text generated by a single iteration of REPT is quite long. If you reduce the number of repetitions, generate .FAS file and use PREPSRC tool to extract preprocessed source, you're going to see exactly what happens there.

On the other hand, if you replace REPT with REPEAT, which is assembler's directive and does not generate copies of the text, the problem should not occur.
Post 18 Jan 2019, 18:40
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 18 Jan 2019, 18:50
I have enough memory (1GB). REPT is just an example, in my program I do not use rept and the problem also occurs. The error is somewhere in the macro lea.

_________________
smaller is better
Post 18 Jan 2019, 18:50
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20340
Location: In your JS exploiting you and your system
revolution 18 Jan 2019, 18:53
You can use the memory option "-m" to control how much memory to allocate at startup.

The Linux build doesn't do memory size detection and it defaults to a small memory footprint.

The Windows build tries to allocate as much RAM as it can so often the -m switch isn't required unless it interferes with other applications trying to use memory.
Post 18 Jan 2019, 18:53
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 18 Jan 2019, 18:56
When I generate .FAS file out of your source, it is nearly 1 GB (1 053 730 997 bytes exactly). So if you have just 1 GB of memory, this text of preprocessed source is enough to run out of it.
Post 18 Jan 2019, 18:56
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 18 Jan 2019, 19:08
after removing the "load" line from the lea macro, the problem disappears

_________________
smaller is better
Post 18 Jan 2019, 19:08
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 18 Jan 2019, 19:35
The generated .FAS file is only 863 MB then, so it fits in your 1 GB.

Note that every iteration of your REPT translates to one "xor" macro and two "mov" macros, and each of these "mov" translates to one "xor" and two previous "mov", and each of them evaluates "lea" 9 times. Therefore every iteration is 2*2*9 "lea" macro calls, so every line in "lea" ends up being repeated 64*1024*36 times. Obviously removing even a single line from "lea" macro is going to affect memory requirements by dozens if not hundreds of MBs.

You could just as well decrease the memory usage by reducing that 9 multiplier, that is removing one of "lea" in the "mov" macro definition. Just comment out a few "lea" (like 3 of them) and you should notice a difference.
Post 18 Jan 2019, 19:35
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 413
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 18 Jan 2019, 19:46
I understand everything. I did not expect that it would take so much memory.
I apologize for unnecessary confusion.
Post 18 Jan 2019, 19:46
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 674
l4m2 23 Jan 2019, 15:52
So the temp code too large 咯?
Post 23 Jan 2019, 15:52
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.