flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > iterate through labels

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 01 Dec 2010, 09:37
Is there any way you can iterate through all existing labels in a file?

_________________
This is a block of text that can be added to posts you make.
Post 01 Dec 2010, 09:37
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 01 Dec 2010, 10:13
The straight answer to your question is "No".
But what actually you want to achieve?
Post 01 Dec 2010, 10:13
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 01 Dec 2010, 10:19
I want to collate pairs of labels and names for debugging purposes.
Post 01 Dec 2010, 10:19
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
Z3N



Joined: 01 Oct 2009
Posts: 17
Z3N 01 Dec 2010, 15:16
You can... just write some code to extend fasm Wink

1) Add command which collect all label in assembly-time.

2)???

3) PROFIT!!!!!!

_________________
"There will be no more delay!" (Revelation 10)
Post 01 Dec 2010, 15:16
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 01 Dec 2010, 17:10
wouldn't it be better to work directly with .FAS file?
Post 01 Dec 2010, 17:10
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 02 Dec 2010, 00:24
Can I access .fas during compilation?
Post 02 Dec 2010, 00:24
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
Z3N



Joined: 01 Oct 2009
Posts: 17
Z3N 02 Dec 2010, 09:26
NO, you can't

Can you explain you task?

_________________
"There will be no more delay!" (Revelation 10)
Post 02 Dec 2010, 09:26
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 02 Dec 2010, 11:50
I want to build an export table automatically
Post 02 Dec 2010, 11:50
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 02 Dec 2010, 12:21
mindcooler,
I have an impression we are forcing you to accept some help. Excuse us, please!
Post 02 Dec 2010, 12:21
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 02 Dec 2010, 13:15
Hm?
Post 02 Dec 2010, 13:15
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
Z3N



Joined: 01 Oct 2009
Posts: 17
Z3N 02 Dec 2010, 15:42
mindcooler wrote:
I want to build an export table automatically
Shocked Shocked Shocked

Use programs which can generate import. In this forum you can find one. Rolling Eyes

You way is good for errors.

_________________
"There will be no more delay!" (Revelation 10)
Post 02 Dec 2010, 15:42
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 02 Dec 2010, 16:58
Errors? What kind of errors?

Anyway, I realized I have to build the export table outside of FASM, so I built this quickie. With some macro code and auto-appending it will make an OK solution.

The code and example:

Code:
                ;TARGET  equ DEBUG
                include 'pe.inc'
                pe      CUI,".text",".bss"

start:
                invoke  GetStdHandle,STD_OUTPUT_HANDLE
                mov     [stdout],eax

                invoke  GetCommandLineW
                call    GetParameter
                mov     [filename],eax

                cmp     word [eax],0
                je      .out

                invoke  CreateFileW,eax,GENERIC_READ,FILE_SHARE_READ+FILE_SHARE_WRITE,NULL,\
                                    OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
                mov     [hfile],eax

                invoke  GetFileSize,[hfile],filesizeh
                mov     [filesize],eax
                inc     eax

                invoke  VirtualAlloc,NULL,eax,MEM_COMMIT+MEM_RESERVE,PAGE_READWRITE
                mov     esi,eax

                invoke  ReadFile,[hfile],eax,[filesize],dummy,NULL

                invoke  CloseHandle,[hfile]

                mov     ebx,[filesize]
                mov     byte [esi+ebx],$4

 .restart:
                mov     edi,labelbuf
                mov     [length],2
                xor     eax,eax
                cmp     byte [esi],'.'
                setz    al
                mov     [sublabel],eax
 .loop:
                lodsb
                inc     [length]
                cmp     eax,$4
                je      .out

                call    alphanum
                jnc     .check

                stosb
                jmp     .loop
 .check:
                cmp     eax,':'
                jne     .restart

                cmp     [length],3
                jbe     .restart

                stosb
                mov     word [edi-1],$0a0d
                dec     [length]

                cmp     [sublabel],0
                jne     .sublabel

                push    esi

                mov     esi,labelbuf
                mov     edi,masterlabelbuf
                mov     ecx,[length]
                sub     ecx,2
                mov     [masterlength],ecx
                rep     movsb

                pop     esi

                invoke  WriteConsoleA,[stdout],labelbuf,[length],dummy,NULL
                jmp     .loop
 .sublabel:
                invoke  WriteConsoleA,[stdout],masterlabelbuf,[masterlength],dummy,NULL
                invoke  WriteConsoleA,[stdout],labelbuf,[length],dummy,NULL
                jmp     .loop

 .out:
                invoke  Sleep,-1

alphanum:
                cmp     al,'_'
                je      .ok

                cmp     al,'.'
                je      .ok

                mov     edx,eax
                sub     edx,'0'
                cmp     edx,'9'-'0'
                jbe     .ok

                sub     edx,'A'-'0'
                cmp     edx,'Z'-'A'
                jbe     .ok

                sub     edx,'a'-'A'
                cmp     edx,'z'-'a'
                jbe     .ok

                clc
                ret
.ok:            stc
                ret

;---

GetParameter:   ;<eax
        cmp     word [eax],'"'
        je      .quote
        cmp     word [eax],0
        je      .out
 .nqloop:
        cmp     word [eax],' '
        je      .lastspace
        inc     eax
        inc     eax
        jmp     .nqloop

 .quote:
        inc     eax
        inc     eax
        cmp     word [eax],'"'
        jne     .quote

 .lastspace:
        inc     eax
        inc     eax
        cmp     word [eax],' '
        jne     .out
        inc     eax
        inc     eax
 .out:
RemoveQuotes:   ;eax
        cmp     word [eax],'"'
        jne     .out

        mov     edx,eax
        inc     eax
        inc     eax
 .loop:
        inc     edx
        inc     edx
        cmp     word [edx],'"'
        jne     .loop
        mov     word [edx],0
 .out:
        retn

import          kernel32,<Sleep,WriteConsoleA,GetStdHandle,CreateFileW,ReadFile,\
                         GetCommandLineW,GetFileSize,VirtualAlloc,CloseHandle>

bss:

masterlabelbuf  rd 1024
labelbuf        rd 1024

dummy           rd 1
stdout          rd 1
filename        rd 1
hfile           rd 1
filesize        rd 1
filesizeh       rd 1
length          rd 1
sublabel        rd 1
masterlength    rd 1

endpe:    


When run it strips out all normal labels:



Code:
start
start.restart
start.loop
start.check
start.sublabel
start.out
alphanum
alphanum.ok
GetParameter
GetParameter.nqloop
GetParameter.quote
GetParameter.lastspace
GetParameter.out
RemoveQuotes
RemoveQuotes.loop
RemoveQuotes.out
bss
endpe    

_________________
This is a block of text that can be added to posts you make.
Post 02 Dec 2010, 16:58
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 02 Dec 2010, 22:24
My guess at what he is trying to do but failing to explain: Export every defined symbol, so that OllyDbg (or something else) knows its name and displays it. I used this trick in FASMLIB to get "debugging symbols".

Impossible at compilation time. Your way is a quite good "quickie" solution (with support for recursive "include" added). A bit more proper way would still IMO be to get symbols from .fas file.
Post 02 Dec 2010, 22:24
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 03 Dec 2010, 03:45
Yes, that was basically the idea.

I will take a look at the fas format, perhaps it will help me get data labels too.
Post 03 Dec 2010, 03:45
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 03 Dec 2010, 17:26
A quick glance at the fas file with this program:

Code:
                ;TARGET  equ DEBUG
                include 'pe.inc'
                pe      CUI,".text",".bss"

start:
                invoke  GetStdHandle,STD_OUTPUT_HANDLE
                mov     [stdout],eax

                invoke  GetCommandLineW
                call    GetParameter

             mov eax,fasfile
                mov     [filename],eax

                cmp     word [eax],0
                je      .out

                invoke  CreateFileW,eax,GENERIC_READ,FILE_SHARE_READ+FILE_SHARE_WRITE,NULL,\
                                    OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
                mov     [hfile],eax

                invoke  GetFileSize,[hfile],filesizeh
                mov     [filesize],eax
                inc     eax

                invoke  VirtualAlloc,NULL,eax,MEM_COMMIT+MEM_RESERVE,PAGE_READWRITE
                mov     esi,eax

                invoke  ReadFile,[hfile],eax,[filesize],dummy,NULL

                invoke  CloseHandle,[hfile]

                mov     ecx,[esi+28]      ;len

                mov     edx,[esi+24]      ;sym
 .loop:
                movzx   eax,word [esi+edx+8]
                and     eax,2
                jnz     .test

                mov     eax,[esi+edx]
                cmp     eax,$400000
                jb      .test

                ;mov     eax,[esi+edx]
                ;call    int2hex32

                mov     eax,[esi+edx+24]
                and     eax,$80000000
                jnz     .string

                mov     eax,[esi+edx+24]
                test    eax,eax
                jz      .test

                mov     ebx,[esi+32]    ;preprocessed
                lea     eax,[ebx+eax]
                add     eax,esi
                pusha
                push    0
                push    dummy
                movzx   ebx,byte [eax]
                push    ebx
                inc     eax
                push    eax
                invoke  WriteConsoleA,[stdout]
                invoke  WriteConsoleA,[stdout],CRLF,2,dummy,NULL
                popa
                jmp     .test
 .string:
                mov     eax,[esi+edx+24]
                and     eax,$7fffffff
                mov     ebx,[esi+16]     ;string
                lea     eax,[eax+ebx]
                add     eax,esi
                push    esi
                mov     esi,eax
                call    strlen
                pop     esi
                pusha
                invoke  WriteConsoleA,[stdout],ebx,eax,dummy,NULL
                popa

 .test:         add     edx,32
                sub     ecx,32
                jne     .loop

                ;invoke  WriteConsoleA,[stdout],labelbuf,[length],dummy,NULL
 .out:
                invoke  Sleep,-1


;---

strlen: ;>esi,<eax
        xor     eax,eax
 .loop:
        cmp     byte [esi+eax],0
        je      .out
        inc     eax
        jmp     .loop
 .out:
        retn

;--

int2hex32: ; >eax,>esp <hexbuffer
        pusha
        pxor    mm4,mm4
        movd    mm0,eax
        punpcklbw mm0,mm4
        movq    mm1,mm0
        psllw   mm0,12
        psrlw   mm1,4
        psrlw   mm0,12
        packuswb mm0,mm4
        packuswb mm1,mm4
        punpcklbw mm0,mm1
        movq    mm2,mm0
        pcmpgtb mm2,[hexcmp]
        paddb   mm0,[hexadd]
        pand    mm2,[hexalpha]
        paddb   mm0,mm2
        movd    eax,mm0
        bswap   eax
        mov     dword [hexbuffer+4],eax
        psrlq   mm0,32
        movd    eax,mm0
        bswap   eax
        mov     dword [hexbuffer],eax
        emms
        invoke  WriteConsoleA,[stdout],hexbuffer,10,dummy,0
        popa
        retn

;---

GetParameter:   ;<eax
        cmp     word [eax],'"'
        je      .quote
        cmp     word [eax],0
        je      .out
 .nqloop:
        cmp     word [eax],' '
        je      .lastspace
        inc     eax
        inc     eax
        jmp     .nqloop

 .quote:
        inc     eax
        inc     eax
        cmp     word [eax],'"'
        jne     .quote

 .lastspace:
        inc     eax
        inc     eax
        cmp     word [eax],' '
        jne     .out
        inc     eax
        inc     eax
 .out:
RemoveQuotes:   ;eax
        cmp     word [eax],'"'
        jne     .out

        mov     edx,eax
        inc     eax
        inc     eax
 .loop:
        inc     edx
        inc     edx
        cmp     word [edx],'"'
        jne     .loop
        mov     word [edx],0
 .out:
        retn

hexcmp          dq      $0909090909090909
hexadd          dq      $3030303030303030
hexalpha        dq      $2727272727272727
hexbuffer       rq      1
CRLF            db      $d,$a,0,0
fasfile         du      'E:\_projekt\fasm\32\labels.fas',0

import          kernel32,<Sleep,WriteConsoleA,GetStdHandle,CreateFileW,ReadFile,\
                         GetCommandLineW,GetFileSize,VirtualAlloc,CloseHandle>

bss:

dummy           rd 1
stdout          rd 1
filename        rd 1
hfile           rd 1
filesize        rd 1
filesizeh       rd 1

endpe:    


Yields these labels (Value <= $400000, not assembly-time) for the same program:

Quote:
endpe
bss
idata
start
peof
mzstart
optheader
endcode
rawstart
GetStdHandle
stdout
GetCommandLineW
GetParameter
filename
CreateFileW
hfile
filesizeh
GetFileSize
filesize
VirtualAlloc
dummy
ReadFile
CloseHandle
labelbuf
length
sublabel
alphanum
masterlabelbuf
masterlength
WriteConsoleA
Sleep
RemoveQuotes
kernel32.name
kernel32.iat
Sleep.name
WriteConsoleA.name
GetStdHandle.name
CreateFileW.name
ReadFile.name
GetCommandLineW.name
GetFileSize.name
VirtualAlloc.name
CloseHandle.name


I got data labels, but a lot of sublabels seem to be missing.

_________________
This is a block of text that can be added to posts you make.
Post 03 Dec 2010, 17:26
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 05 Dec 2010, 05:51
I set up an export macro,

Code:
macro export name*,[labels*]
{
   common
      edata:
      dd 0
      dd 0
      dw 0
      dw 0
      dd exportname-imgbase
      dd 0
      dd (ot-npt)/4
      dd (ot-npt)/4
      dd eat-imgbase
      dd npt-imgbase
      dd ot-imgbase
      eat:
   forward
      dd labels-imgbase
   common
      npt:
   forward
      dd labels#.name-imgbase
   common
      ot:
      ordinal=0
   forward
      dw ordinal
      ordinal=ordinal+1
   common
      ent:
   forward
      labels#.name: db `labels,0
   common
      exportname: db `name,0
      edata.size = $-edata
      edata.end:
}     


and exported the labels I got from parsing the asm file: (minus 'E:') Razz

Code:
export          'Labels',start, start.restart, start.loop, start.check, start.sublabel, start.out,\
                         alphanum, alphanum.ok, GetParameter, GetParameter.nqloop, GetParameter.quote,\
                         GetParameter.lastspace, GetParameter.out, RemoveQuotes, RemoveQuotes.loop,\
                         RemoveQuotes.out, asmfile, bss, bss.end     


and it makes a world of difference when debugging!

Image

http://files.sys5.se/Graph%20of%20.text_00401000...text_004011E1.pdf

_________________
This is a block of text that can be added to posts you make.
Post 05 Dec 2010, 05:51
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 06 Dec 2010, 09:11
With some help from SFeLi I got the .fas processor working.

But every time I build symbols with exports and export those labels, there are more symbols to export and so on, making the export table grow each time. Can't see any solution to that problem, guess I'll need to comment out the export table every time I build symbols.
Post 06 Dec 2010, 09:11
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 06 Dec 2010, 11:51
maybe you can simply ignore all local labels when parsing .fas, and build export table only using local labels. Or am I misunderstanding your problem?
Post 06 Dec 2010, 11:51
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 06 Dec 2010, 15:10
Wouldn't local labels get added anyway? Just name-mangled?

I got the tip to skip all labels generated by a macro. That will probably do nicely.
Post 06 Dec 2010, 15:10
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 06 Dec 2010, 16:32
Not a best idea IMO. That way labels generated by "proc" macro would not be exported either.

BTW, in previous post I meant macro-local labels (marked with "local" directive), not plain local labels (starting with dot). IMO macro-local labels never need to be shown in debugger.
Post 06 Dec 2010, 16:32
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.