flat assembler
Message board for the users of flat assembler.

Index > Main > How to declare array of labels?

Author
Thread Post new topic Reply to topic
SergeASM



Joined: 13 Nov 2015
Posts: 21
SergeASM
I want to simulate case operator in a procedure and do jump on a list of labels like this:

proc <name>...
local list dd m1,m2,m3

mov ebx,[list]
add ebx,4
jmp [ebx] ; jump to m2

m1:
...
m2:
...
m3:

But it does not work. Any ideas?

Serge
Post 20 Nov 2015, 08:20
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17346
Location: In your JS exploiting you and your system
revolution
You probably want to use LEA instead of MOV and a dword override:
Code:
lea ebx,[list]
;...
jmp dword[ebx]    
Or you can use ebx as an index and no override is needed because 'list' already has the dword size:
Code:
mov ebx,1
jmp [list+ebx*4]    


Last edited by revolution on 20 Nov 2015, 09:14; edited 1 time in total
Post 20 Nov 2015, 08:30
View user's profile Send private message Visit poster's website Reply with quote
SergeASM



Joined: 13 Nov 2015
Posts: 21
SergeASM
Thanks, but

list dd m1,m2

causes error in proc32.inc if list contains more than 1 label...

Serge
Post 20 Nov 2015, 09:02
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17346
Location: In your JS exploiting you and your system
revolution
You can declare each one on a separate line.
Post 20 Nov 2015, 09:13
View user's profile Send private message Visit poster's website Reply with quote
SergeASM



Joined: 13 Nov 2015
Posts: 21
SergeASM
Thanks, now FASM translate it successful.

Serge
Post 20 Nov 2015, 09:59
View user's profile Send private message Visit poster's website Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
SergeASM
I'd like to give a clarification here. The macro local has different syntax and is not supposed to support initialization at all.

For unclear reasons it has a fallback handling if the local variable declaration could not be parsed according to the defined syntax. This allows to write things like local <list dd m1,m2,m3>, but these should be considered a hack. What's not gonna work anyway is using multiple lines with local, of which only the first one contains the label name.

A more compliant (but not a cleverer way) is to use the macros locals/endl for this use case.

A better way is to define the label table outside of the procedure (e.g. directly after it), because this won't produce a runtime overhead for local variables initialization.

_________________
Faith is a superposition of knowledge and fallacy
Post 20 Nov 2015, 15:40
View user's profile Send private message Reply with quote
SergeASM



Joined: 13 Nov 2015
Posts: 21
SergeASM
l_inc
Please give a small example how to do that, thanks.

Serge
Post 20 Nov 2015, 19:17
View user's profile Send private message Visit poster's website Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
SergeASM
Code:
proc myproc selector
    mov ecx,[selector]
    and ecx,3
    jmp [.table+ecx*4]
    ;.table dd .m0, .m1, .m2, .m3 ;<-- the table could be put here as well, but this might affect performance
    .m0:
        xor eax,eax ;mov eax,0
    jmp .exit
    .m1:
        mov eax,1
    jmp .exit
    .m2:
        mov eax,2
    jmp .exit
    .m3:
        mov eax,3
    .exit:
ret
endp

.table dd .m0, .m1, .m2, .m3    

_________________
Faith is a superposition of knowledge and fallacy
Post 21 Nov 2015, 02:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17346
Location: In your JS exploiting you and your system
revolution
I think you forgot the 'align 4' Razz
Post 21 Nov 2015, 02:50
View user's profile Send private message Visit poster's website Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
revolution
Yes, I did. Smile

_________________
Faith is a superposition of knowledge and fallacy
Post 21 Nov 2015, 02:53
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.