News:

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

Macro trap

Started by jj2007, August 16, 2010, 10:07:04 AM

Previous topic - Next topic

jj2007

Just ran into a noob problem: .elseif len(offset Str7)==7 will create bugs that are very difficult to chase. The reason is that macros are being expanded right before the line that invokes them.

include \masm32\include\masm32rt.inc

.code
AppName db "MacTest:", 0
Str8 db "12345678", 0
Str7 db "1234567", 0

start:
.if len(offset Str8)==9
MsgBox 0, "9 bytes, wrong", offset AppName, MB_OK
.elseif len(offset Str7)==7
MsgBox 0, "7 bytes, correct", offset AppName, MB_OK
.else
MsgBox 0, "Surprise, nothing is correct", addr AppName, MB_OK
.endif

.if len(offset Str8)==9
MsgBox 0, "9 bytes, wrong", offset AppName, MB_OK
.elseif len(offset Str8)==8 ; this looks like we are testing the string again, but...
nop
MsgBox 0, "8 bytes, yeah - but check that in Olly!", offset AppName, MB_OK
nop
.else
MsgBox 0, "Surprise, nothing is correct", addr AppName, MB_OK
.endif

exit

end start


Excerpt from Olly - check the green part...

Quote00401076    |. 68 09104000        push 00401009               ;  ASCII "12345678"
0040107B    |. E8 90000000        call szLen
00401080    |. 83F8 09            cmp eax, 9
00401083    |. 75 1F              jnz short 004010A4
00401085    |. 6A 00              push 0                      ; /Style = MB_OK|MB_APPLMODAL
00401087    |. 68 00104000        push 00401000               ; |Title = "MacTest:"
0040108C    |. 68 3D404000        push 0040403D               ; |Text = "9 bytes, wrong"
00401091    |. 6A 00              push 0                      ; |hOwner = NULL
00401093    |. E8 BE000000        call MessageBoxA            ; \MessageBoxA
00401098    |. 68 09104000        push 00401009               ;  ASCII "12345678"
0040109D    |. E8 6E000000        call szLen
004010A2    |. EB 2F              jmp short 004010D3
004010A4    |> 83F8 08            cmp eax, 8
004010A7    |. 75 17              jnz short 004010C0
004010A9    |. 90                 nop
004010AA    |. 6A 00              push 0                      ; /Style = MB_OK|MB_APPLMODAL
004010AC    |. 68 00104000        push 00401000               ; |Title = "MacTest:"
004010B1    |. 68 4C404000        push 0040404C               ; |Text = "8 bytes, yeah - but check that in Olly!"
004010B6    |. 6A 00              push 0                      ; |hOwner = NULL
004010B8    |. E8 99000000        call MessageBoxA            ; \MessageBoxA
004010BD    |. 90                 nop