Hey,
I am going through his tutorials. I have built, assembled, linked and run my first all Windows App. I have added a few features beyond.
I was curious about 2 instructions and what they are doing in the WinMain proc:
push hInstance
pop wc.hInstance
I have read of the technique to swap registers with push and pop, is this what this is doing? if so, how are these swapping, from a global var to a member of a struct?
why not just like:
mov ebx, offset hInstance
mov wc.hInstance, ebx
Thanks
Quote from: TBRANSO1 on January 16, 2019, 04:08:16 AM
how are these swapping, from a global var to a member of a struct?
If hinstance is a global var then yes (don't remember that example). :idea:
Quote from: TBRANSO1 on January 16, 2019, 04:08:16 AM
mov ebx, offset hInstance
mov wc.hInstance, ebx
You don't need the address of the variable hinstance, you need hinstance value, so you should code something like:
mov ebx,hInstance
mov wc.hInstance, ebx
:idea:
Quote from: TBRANSO1 on January 16, 2019, 04:08:16 AM
why not just like:
Maybe because you don't want to trash that register. :idea:
Quote from: felipe on January 16, 2019, 05:26:57 AMMaybe because you don't want to trash that register. :idea:
Yes, that's often the reason: push somevar, pop anothervar does not need a register. There are two dedicated macros:
m2m destvar, sourcevar uses push+pop
mrm destvar, sourcevar uses eax to move the values.
I watched that sequence in the VS debugger and watched that wc.hInstance get filled with the pointer from the global hInstance... it's baffling, but I guess that is what is happening, swapping.
I read about the techniqe, instead of xchg, but this is baffling. I think that I will have to draw this on paper or get the .lst file and work with it, this is warping my brain.
EDITED LATER:
While reading something else entirely, I stumbled across the answer to this question: http://www.hep.wisc.edu/~pinghc/asm5.html
Quote
If it is allocated in the .DATA section, it can be initialised to preset values but if it is allocated on the stack as a LOCAL value within a procedure, the values have to be coded into the structure.
LOCAL Rct :RECT
; code
mov Rct.left, 1
mov Rct.top, 2
mov Rct.right, 3
mov Rct.bottom, 4
It should be noted that a structure member is a memory operand so you cannot directly move another memory operand into it, you must either use a register to copy it or the stack mnemonics, push / pop.
If you are writing high level API style code, the push / pop technique is easily fast enough but at the other end with a pure mnemonic algorithm, you design it to use a register as it is a faster technique. Avoid XCHG as its a relic of the DOS days and only exists in micro-code. This variety is normal in assembler where you have multiple ways of doing things, with practice, you just pick the technique that best suits your application.