flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > macro local

Author
Thread Post new topic Reply to topic
system error



Joined: 01 Sep 2013
Posts: 670
system error 01 Sep 2013, 14:07
hi. this my first post.

just want to ask how to properly declare local variables in macro in protected mode. I read the manual but not that satisfying. lets say i have an array of 20 chars like

hello db 20 dup ('F')

macro foo
{
local hello

hello db 20 dup ('F')

mov eax, 'ABCD'
mov byte [hello], al ; not error, but stuck here
}

i don't know whats wrong there. This is just an example code.

regards
Post 01 Sep 2013, 14:07
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 01 Sep 2013, 16:45
The declaration is correct but foo will expand to the buffer you created and the code afterwards, so if you tried to execute foo, you're actually executing the local data as code. A possible correction would be:
Code:
macro foo 
{ 
 local hello 
 local skip

 jmp skip
 hello db 20 dup ('F') 
 skip:
 mov eax, 'ABCD' 
 mov byte [hello], al ; not error, but stuck here 
}    
Post 01 Sep 2013, 16:45
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 01 Sep 2013, 18:19
Hello cod3b453, appreciate your reply but that code didn't work either. This is my updated code. Just a test code with no specific purpose.

Code:
format PE console
include 'win32ax.inc'
entry main
macro foo
{
        local hello
        local skip

        jmp skip
            hello db 10 dup ('XyXyXyXyXy')
        skip:
        mov eax, 'ABCD'
        mov byte [hello], al ; not error, but stuck here
        cinvoke printf, "%c", dword [hello]
}

section '.code' code executable
main:
foo
invoke system, "pause"
invoke exit, 0

section '.data' data readable writable
anything db ?

section '.idata' import data readable
          library msvcrt, 'msvcrt.dll'
          import msvcrt,\
                 system, 'system',\
                 printf,'printf',\
                 exit, 'exit'    


I opened the VC++ debugger and it gave me:
Unhandled exception in foo32exe: 0xC0000005:Access Violation.

This is the problematic region I got from the debugger

00401001 pop eax
00401003 jns 0040105D
00401005 jns 0040105F
00401007 jns 00401061
---cut---
0040105F jns 004010B9
00401061 jns 004010BB
00401063 jns 004010BD
00401065 jns 0040101F
00401067 inc ecx
00401068 inc edx
00401069 inc ebx
0040106A inc esp
0040106B mov [00401002],al ;<<--- Debugger stops here.
Post 01 Sep 2013, 18:19
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4060
Location: vpcmpistri
bitRAKE 01 Sep 2013, 20:14
"Access Violation" means that permission to write is not allowed. So, we would look at the permissions of memory location. Code section is read only (default). Need to add WRITEABLE to the SECTION definition.
Post 01 Sep 2013, 20:14
View user's profile Send private message Visit poster's website Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 01 Sep 2013, 20:27
bitRAKE wrote:
"Access Violation" means that permission to write is not allowed. So, we would look at the permissions of memory location. Code section is read only (default). Need to add WRITEABLE to the SECTION definition.


Great! I got it working now. Thanks to you and cod3b45e. I didn't know FASM macro is that 'powerful'.
Post 01 Sep 2013, 20:27
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.