News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Simple template for creating a window

Started by jj2007, October 14, 2013, 08:51:01 PM

Previous topic - Next topic

dedndave

i don't know, Jochen
a lot can be done with EAX, ECX, EDX and INVOKE
i try to be conservative with system resources, when it's practical to do so - push if you need to
true - that one instance of one app doesn't take much
but, i try to think in terms of what else the user may have going on
pretend he has 100 other windows open - lol
if they all use the same paradigm, his system slows to a crawl

Erol - i like the example
i have one, someplace - i use PUSH to put the parms on the stack, then call DialogBoxIndirectParam
it's clumsy to code, at first - but that part only has to be done once   :P

jj2007

Quote from: dedndave on October 15, 2013, 10:47:11 PM
pretend he has 100 other windows open - lol
if they all use the same paradigm, his system slows to a crawl

Dave,

Even if the user has a thousand windows open, only one is active and gets messages. Three cycles for push & pop is around 3/1500000000=2 nanoseconds per message. Not milliseconds, not microseconds, no: nanoseconds.

Our exercises in the lab typically run with a Million loops. Even with the most frequent message, WM_MOUSEMOVE, you cannot produce more than a few dozen per second if you move the mouse frenetically all over the desktop...

RuiLoureiro

#32
Hi MichaelW,
            Good explanation

«So you believe that Windows preserves EBX, ESI, and EDI around calls to callbacks?»

I believe the OS preserves EBX, ESI and EDI (if it needs to use it) when it calls the
WndProc callback and i know that the OS preserves them in any API like i
preserve them in procedures of my library because i need it.

Why ?
We can see the OS as a «server» and a program a «client».
Now, suppose i am the «server» and you are my «client».
In this way, i write myprocedures that call yourprocedures. When
i write myprocedures i need to use ebx, esi and edi before and
after calling yourprocedure. If i know that i need to use those
registers after calling your procedures, it makes no sense
to ask you «make me a favor, please, preserve ebx, esi, and edi».
It is stupid. (The same way it doesnt make sense to give you
a glass of something and to ask you to return the glass cleaned
because i need to use it again).

I never read any document (from Microsoft) or any remark
that says «you must preserve ebx, esi, edi in
any callback function like Window procedure WndProc»
-------------------------------------------------------------------------------------
Now, suppose i write the following procedure (ProcX) for my library.
It uses EBX but it doesnt preserve EBX before calling ProcY.
But i make a remark:

        «Hey people, please, preserves EBX when you define ProcY»

Do you think is this correct ?
For me, it is stupid.

ProcX       proc    pTblX:DWORD, pProcY:DWORD
            push    ebx
            push    esi
           
            mov     esi, pTblX
            mov     ebx, [esi-4]
            jmp     _start
           
    @@:     push    esi
                mov     eax, ebx
                shl     eax, 4
                add     esi, eax
                call    pProcY      ; esi is the table address
            pop     esi
       
    _start: sub     ebx, 1
            jns     short @B

            pop     esi
            pop     ebx
            ret
ProcX       endp
--------------------------------------------------------
It might be something like this and we have no problems

ProcX       proc    pTblX:DWORD, pProcY:DWORD
            push    esi
           
            mov     esi, pTblX
            mov     eax, [esi-4]
            jmp     _start
           
    @@:     push    eax
            push    esi
                shl     eax, 4
                add     esi, eax
                call    pProcY      ; ESI is the table address
            pop     esi
            pop     eax
       
    _start: sub     eax, 1
            jns     short @B

            pop     esi
            ret
ProcX       endp

RuiLoureiro

Hi Jochen,

«All three regs have a different value every time they enter the WndProc,
with ebx always being zero.»

        Does this means the OS use them after, as they are ?
       
        By the way, i never wrote procedures
        for anyone and when i do it you may read it or
        i say what they do (.txt file or ...).

RuiLoureiro

Hi qWord,

«
some Windows versions seems to check if ESI, EDI and EBX has been violated
by the window procedure and correct that.
More worse, since windows 7 (or maybe Vista),
there is exception handler around the WndProc** that silently catch
all exceptions thus you have no chance to realized hard errors
(unless you run the corresponding code in a debugger or disable that mechanism).
However this does not apply to all callbacks :
»
        Interesting !
        IMO, it is not a good solution,
        but i think that should be good reasons
        for that.

MichaelW

Quote from: RuiLoureiro on October 16, 2013, 02:51:03 AM
I never read any document or any remark
that says «you must preserve ebx, esi, edi in
any callback function like Window procedure WndProc»

See Register Usage in Agner Fog's calling_conventions.pdf, available here.

And note that the callee-save registers include EBP, and preserving EBP is essential if the caller uses a normal stack frame.
Well Microsoft, here's another nice mess you've gotten us into.

hutch--

I am still fascinated that after years of mistakes and crashes that people don't want to do it the right way, the Intel ABI !!! IS !!! the right way and Microsoft do it that way as well. How long have we been hearing "Oh but it worked on Win_whatever but now it crashes on Win_something_else".

Now if you want to write CRAP unreliable code that goes BANG in embarrassing places and make a fool of you, ignore the Intel ABI and do it the wrong way.  :P

Vortex

Hi Dave,

Thanks. Another nice tool is Hutch's macro based dialog templates. They are more efficient than a sequence of bytes in the DATA section.

RuiLoureiro

Quote from: MichaelW on October 16, 2013, 07:38:19 AM
Quote from: RuiLoureiro on October 16, 2013, 02:51:03 AM
I never read any document or any remark
that says «you must preserve ebx, esi, edi in
any callback function like Window procedure WndProc»

See Register Usage in Agner Fog's calling_conventions.pdf, available here.

And note that the callee-save registers include EBP, and preserving EBP is essential if the caller uses a normal stack frame.

        MichaelW, could you paste the relevant info here ?
        It is only to read it

        i have no problems with ALI and, as far as i know, no problems
        with my window applications. They are running for many years
        without problems. And if it has problems i solve it, dont worry.

        Could you tell me what happen with quickeditor ?
       
        There are a lot of times where i type things in the line
        x, and it is written in line the line y. For instance,
        x=14 and y=927. I use backspace in line x and it
        cleans in line y.
        I never saw this type of problems with word !

RuiLoureiro

Quote from: hutch-- on October 16, 2013, 03:48:59 PM
I am still fascinated that after years of mistakes and crashes that people don't want to do it the right way, the Intel ABI !!! IS !!! the right way and Microsoft do it that way as well. How long have we been hearing "Oh but it worked on Win_whatever but now it crashes on Win_something_else".

Now if you want to write CRAP unreliable code that goes BANG in embarrassing places and make a fool of you, ignore the Intel ABI and do it the wrong way.  :P
:biggrin:
Hutch,
        You are taking too much whiskey
        please, dont do that it is not
        good to your health

hutch--

You never waste good pure malt on excess but none the less, disregard the Intel ABI and your app will go BANG and make a fool of you when you least need it.  :biggrin:

qWord

Quote from: RuiLoureiro on October 16, 2013, 09:47:39 PMcould you paste the relevant info here ?
from msdn: x86 Architecture
Quote from: debug: x86 ArchitectureCalling Conventions

The x86 architecture has several different calling conventions. Fortunately, they all follow the same register preservation and function return rules:

    Functions must preserve all registers, except for eax, ecx, and edx, which can be changed across a function call, and esp, which must be updated according to the calling convention.
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

Quote from: hutch-- on October 16, 2013, 03:48:59 PM
I am still fascinated that after years of mistakes and crashes that people ... write CRAP unreliable code that goes BANG in embarrassing places and make a fool of you

Quote from: RuiLoureiro on October 17, 2013, 12:06:47 AMYou are taking too much whiskey

I am still fascinated that innocent threads can so easily turn into fascinating exchanges of ideas :greensml:

The Windows documentation is not exactly "crystal clear" about it, but it does say that you must preserve the non-volatile regs in callbacks. And technically speaking, WndProc is a callback, so it would indeed be bad practice not to preserve them.

Which has little to do with the observation that at least on Win XP and Win7-32, you can apparently trash esi edi ebx without crashing your app. My best guess is that Microsoft knows too well how many crappy coders and compilers are out there ;-)

RuiLoureiro

#43
Jochen,

Quote
The Windows documentation is not exactly "crystal clear" about it,
AFAIK there are many undocumented things
       
Quote
but it does say that you must preserve the non-volatile regs in callbacks.
And technically speaking, WndProc is a callback, so it would indeed be bad
practice not to preserve them.       

        only this:
        of course i understood it many years ago !

        onother thing:
        Doesnt sting me ! Sting Dave ! (bees are not with me) ;)

RuiLoureiro

Hutch,
        You need to change to a strong Dutch beear !
        I have here some bottles that came directly
        from Holland. Do you want one ?