News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Local and global labels problem

Started by jj2007, August 29, 2008, 08:22:02 AM

Previous topic - Next topic

jj2007

I ran into something weird. Snippet:
test al, al
jne @F ; <-- this jump to f2sL0 works fine

dec edi
mov al, [edi]
test al, al
jne f2sL0 ; <-- this jump fails

dec edi

.endif

f2sL0: mov al, [edi]
... 25 lines of code ...
cmp ecx, ebx
jb f2sL0 ; this jump works fine


Jumps to f2sL0 from below this code worked fine. But then I discovered in OllyDbg that
jne f2sL0
was encoded as a jump to a pretty far location; replacing with jne @F helped. When digging deeper, I found out that I had used f2sL0 far down for a label outside a proc (a label to get the size of the xxx proc)...:

xxx ENDP
f2sL0:
invoke MessageBox, 0, chr$("Gotcha!"), chr$("Title"), MB_OK
exit
end start


Questions:
- Why does the code work correctly for jumps to the "local" f2sL0 when they come from below?
- Would there be a correct way to use the same keyword for one global one one or more local labels?

I promise I will not use the latter option, as it would lead to spaghetti code, but I am still curious to know how it would work.

hutch--

JJ,

Ther is nothing wrong with spaghetti code if the sauce is OK.  :P

Was the lower label in module or procedure scope ?
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Sean1337

What's strange is how your code compiled in the first place..I'm assuming that last "f2sL0:" label is global because it falls outside of a proc. With that said..the program shouldn't compile because the local "f2sL0:" label found inside one of your procs would cause the compiler to give some type of "already defined" error or something.

I'm assuming that the lower label was in module scope (e.g. global).

I personally just write all my code in procedures to avoid any problems like that. If I want a label to be global then I just add an extra colon to the end (e.g. f2sL0::).

jj2007

Quote from: hutch-- on August 29, 2008, 08:46:55 AM
JJ,

There is nothing wrong with spaghetti code if the sauce is OK.  :P
Did you know that a plate of spaghetti with minced meat sauce aka Bolognese has twice as many calories compared to a plate with just plain tomatoes, basilico etc...?

Quote
Was the lower label in module or procedure scope ?

As shown, just below an endp:
xxx ENDP
f2sL0:


What about the extra colon mentioned by Sean? Is that a clearly defined way of telling Masm "its' global, stupid" ? Or is it just another variant of "decoration" ?
I also vaguely remember having seen labels of the form :heythere, i.e. colon first, but maybe that was inside macros ::)

hutch--

Defining a label with the double :: is the documented method of making a label with global scope within a module. I guess the label being defined outside of a proc leavs it at global scope so it may take precedence over the local label. Its probably worth the effort to separate the names of global versus local labels, local labels are very reliable, I regularly use "lbl0: lbl1: etc ..." in diferent procs and have never had a problem with it.

> Did you know that a plate of spaghetti with minced meat sauce aka Bolognese has twice as many calories compared to a plate with just plain tomatoes, basilico etc...?

Thats probably why I like it, we have an award winning Italian restaurant around the corner from where I live so I know what good bolognese tastes like. (I learnt it from the Calabrese).
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php