flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Macro Temp Labels

Author
Thread Post new topic Reply to topic
Mercury Knight



Joined: 09 Nov 2008
Posts: 15
Mercury Knight
I was browsing along the board when I came upon the 'mz segments' thread, and the final result was very similar to what I needed such that I made a few changes and stored the result in segments.inc.

Code:
; sets the format of the file and also set the model flags for use with _declaresegment
; the main difference between format and .model is that a comma separates
; the format type from the flags when using .model
; i.e. .model pe
;       .model pe, gui <-- comma separates format flag from type
;       .model coff
;       .model ms coff ; <-- does not need a comma between ms and coff
macro .model name, [args]
{
   common ModelFlag$$ = 0

   match =pe, name
   \{
      ModelFlag$$ = 1b
   \}
   match =ms coff, name
   \{
      ModelFlag$$ = 1b
   \}
   match =coff, name
   \{
      ModelFlag$$ = 10b
   \}

   format name args
   _segment ModelDummySegment$$
}

; use the macro .segment to declare a new segment (or one previously declared)
; dont use this one directly
macro _segment name
{
   local mac
   __#name equ __#name, mac
   macro mac
   {
}

; internal macro to *really* declare the segments, do not use directly
macro _declaresegment name
{
   common

   if ModelFlag$$ and 1b
      match =text, name
      \{
         section '.text' code readable executable
      \}
      match =data, name
      \{
         section '.data' data readable writable
      \}
      match =bss, name
      \{
         section '.bss' readable writable
      \}

   else if ModelFlag$$ and 10b
      match =text, name
      \{
         section '.text' code 
      \}
      match =data, name
      \{
         section '.data' data 
      \}
      match =bss, name
      \{
         section '.bss' data
      \}

   end if
}

; macro to declare and write all segments in order
; do not use this directly, use .end
macro _end [segments]
{
   common
   local mac
   macro mac dummy,[n]
   \{
     n
     purge n
   \}
   forward
   _declaresegment segments
   match I, __#segments
   \{
      mac I
   \}
}

; use these two instead of _segment and _end
.segment    fix } _segment
.endx        fix } _end ModelDummySegment$$ ,
    


I made several test programs and it passed in all but two. One I am in the process of fixing, but the other (this one here) has really got me stumped.

Code:
include 'win32w.inc'
include 'segments.inc'

.model ms coff   ; same parameters as format (so dont use format directly)
public testproc

.segment text

proc testproc
local temp
      ; .... code
      mov   eax,[temp] ; <-- sup with the temporary label being temp?Pi ??
      ; .... code
endp

; resource.inc already defined .end.... the segments.inc file will have few new names to reduce pollution in namespace
.endx text   

    


The failure point is at the mov eax,[temp] line when the _end macro was called. Fasm failed with an undefined symbol 'temp?Pi' and no matter which way I look at it, I am unable to figure out what modifications is needed for the local variables (as defined by proc32's locals) to not have additional strings added at the end of the name. This file is directly compilable, and a disassembler should show the instruction assembled to mov eax,[ebp-4].

I'd appreciate any help y'all can give, I've been on this for nearly two days (when I'm not in class or working on homework!). It's almost 4AM now, so I am going to bed after I post this. I'm tired enough to stop and just ask for help! LOL

Cheers!
M
Post 17 Jan 2009, 09:03
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Mercury Knight,

local was interpreted as preprocessor's directive because it is inside macro definition. Use locals/endl.
Post 17 Jan 2009, 20:52
View user's profile Send private message Reply with quote
Mercury Knight



Joined: 09 Nov 2008
Posts: 15
Mercury Knight
DOH! I should have thought of that! If its inside a macro, like my segment macros, then local is interpreted as the preprocessor's directive. Otherwise, outside of a macro, it is interpreted as the macro defined in proc32.inc. I tested it by temporarily renaming the local macro to localx, now it compiles properly. Now I need to find another name for the local macro that will remain clear without needing to use locals/endl.

Just out of curiosity, is there an updated version of fasmpre? I found an older version here (1.65) but it would not preprocess *any* of my source files, thus all that time wasted trying to visualize mentally what the compiler is doing.

Thanks again baldr! *grinz*

M
Post 17 Jan 2009, 23:50
View user's profile Send private message Reply with quote
Mercury Knight



Joined: 09 Nov 2008
Posts: 15
Mercury Knight
Am I right in understanding that when you include a file, its the same as if the included file was embedded *unmodified* in the source at the point that you included the file?

The following below will compile successfully using the segments.inc file listed in the first post, but if you move everything between START and STOP to a new file, and then in this file include the new file, you will get an error when you reach the .endx macro (apparently the segment names are invalid). Seems like include makes some changes where it shouldn't. I haven't been able to track down this failure, so I'm wondering if any of you could.

Code:
; example of simplified Win32 programming using complex macro features

include 'win32wx.inc'
include 'segments.inc'

.model pe,gui 4.0

;-- START
.segment text

  start:
        invoke   MessageBox,HWND_DESKTOP,str1,str2,MB_OK
   invoke   ExitProcess,0

.segment data
str1 TCHAR "Hi! I'm the example program!",0
str2 TCHAR "Win32 Assembly",0

;-- STOP

.endx text, data
.end start
    


Thanks!

M
Post 25 Jan 2009, 00:35
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
Mercury Knight wrote:
Am I right in understanding that when you include a file, its the same as if the included file was embedded *unmodified* in the source at the point that you included the file?
You can't break a macro between files. Your .segment macro is opened in the included file but not closed until .endx is encountered.
Post 25 Jan 2009, 04:44
View user's profile Send private message Visit poster's website Reply with quote
Mercury Knight



Joined: 09 Nov 2008
Posts: 15
Mercury Knight
revolution wrote:
Mercury Knight wrote:
Am I right in understanding that when you include a file, its the same as if the included file was embedded *unmodified* in the source at the point that you included the file?
You can't break a macro between files. Your .segment macro is opened in the included file but not closed until .endx is encountered.


Go figure, I had a suspicion something like that was happening. Confused Already back to the drawing board rewriting segments.inc! Thanks rev.

M
Post 25 Jan 2009, 06:00
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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.