flat assembler
Message board for the users of flat assembler.

Index > Windows > When will GetMessage return -1?

Author
Thread Post new topic Reply to topic
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Vitally important information: http://blogs.msdn.com/b/oldnewthing/archive/2013/03/22/10404367.aspx

It's a shame I can't remember the several threads this was discussed.
Post 23 Mar 2013, 22:19
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2917
Location: [RSP+8*5]
bitRAKE
Oh, one of those tri-state Booleans. Very Happy
MS seems to like them.
Post 24 Mar 2013, 22:17
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2917
Location: [RSP+8*5]
bitRAKE
I've used:
Code:
        test eax,eax ; -1,0,1
        jg .mloop    
Since I learned of the tri-state booleans. It's possible to branch on any one or two combination - making it an elegant kludge really.
Post 26 Mar 2013, 09:28
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1412
Location: Toronto, Canada
AsmGuru62
This code will never return -1:
Code:
        ;
        ; Main application message loop
        ;
        sub     esp, sizeof.MSG
        mov     edi, esp
        xor     ebx, ebx
@@:
        invoke  GetMessageW, edi, ebx, ebx, ebx
        test    eax, eax
        jz      .game_over

        invoke  TranslateMessage, edi
        invoke  DispatchMessageW, edi
        jmp     @r

.game_over:
        add     esp, sizeof.MSG
        ...
    
Post 26 Mar 2013, 12:53
View user's profile Send private message Send e-mail Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6038
Location: Poland
MHajduk
bitRAKE wrote:
I've used:
Code:
        test eax,eax ; -1,0,1
        jg .mloop    
Since I learned of the tri-state booleans. It's possible to branch on any one or two combination - making it an elegant kludge really.
bitRAKE, how do we know that 'GetMessage' function returns only those three values? MSDN states that "return value can be nonzero, zero, or -1" (kind of ridiculous redundancy, I know, because -1 is also nonzero value), so if there would be only -1, 0, and 1 they should say it explicitly. What for they have said "nonzero" instead simply "1"?
Post 28 Mar 2013, 21:33
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2917
Location: [RSP+8*5]
bitRAKE
The comment on the code is error.

It actually should say: -,0,+

My (non-MS approved) assumption is that all negative values are errors - not just (-1). All positive values are assumed to be valid window messages, and zero is of course WM_QUIT. It's important to have the Z and S (O=0 too) flags set for the jump to operate effectively given these assumptions.

Some might think CMP instruction must be needed to get the proper signed response -- I did, since TEST is a logical instruction. The comment is a reminder to myself that TEST works for signed evaluation because it clears the O flag.

Imagine if we have a hash table using open addressing: We can use the bits of the pointer and this three-state trick to make the code more efficient: TEST EAX,$80000003. (When testing the state of the table entry.)

_________________
¯\(°_o)/¯ unlicense.org
Post 28 Mar 2013, 23:56
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
bitRAKE,

Condition flags are hard to master, yet they're thankful when you did. Particularly PF can simplify efforts to detect bit combinations that are hard to identify using straightforward technique (test eax, 3/jpe equal jumps if lower two bits are equal).
Post 29 Mar 2013, 07:23
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17351
Location: In your JS exploiting you and your system
revolution
bitRAKE wrote:
I've used:
Code:
        test eax,eax ; -1,0,1
        jg .mloop    
Since I learned of the tri-state booleans. It's possible to branch on any one or two combination - making it an elegant kludge really.
MHajduk wrote:
bitRAKE, how do we know that 'GetMessage' function returns only those three values? MSDN states that "return value can be nonzero, zero, or -1" (kind of ridiculous redundancy, I know, because -1 is also nonzero value), so if there would be only -1, 0, and 1 they should say it explicitly. What for they have said "nonzero" instead simply "1"?
I think this code can properly distinguish the three states according to the official documentation:
Code:
        inc     eax
        cmp     eax,1
        jb      .return_value_was_negative_one
        je      .return_value_was_zero
        ja      .return_value_was_non_zero
        ;other tests possible also
        jae     .return_value_was_not_negative_one
        jne     .return_value_was_not_zero
        jbe     .return_value_was_not_non_zero    
Post 04 Apr 2013, 02:13
View user's profile Send private message Visit poster's website 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.