Author Topic: Win32 Windows - Iczelion's Win32 Assembly Tutorial  (Read 1018 times)

TBRANSO1

  • Member
  • **
  • Posts: 81
Win32 Windows - Iczelion's Win32 Assembly Tutorial
« on: January 16, 2019, 04:08:16 AM »
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

felipe

  • Member
  • *****
  • Posts: 1265
  • Eagles are just great!
Re: Win32 Windows - Iczelion's Win32 Assembly Tutorial
« Reply #1 on: January 16, 2019, 05:26:57 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:

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:

Code: [Select]
mov ebx,hInstance
mov wc.hInstance, ebx
  :idea:

why not just like:

Maybe because you don't want to trash that register.  :idea:

Felipe.

jj2007

  • Member
  • *****
  • Posts: 10098
  • Assembler is fun ;-)
    • MasmBasic
Re: Win32 Windows - Iczelion's Win32 Assembly Tutorial
« Reply #2 on: January 16, 2019, 05:41:36 AM »
Maybe 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.

TBRANSO1

  • Member
  • **
  • Posts: 81
Re: Win32 Windows - Iczelion's Win32 Assembly Tutorial
« Reply #3 on: January 16, 2019, 06:06:53 AM »
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.
« Last Edit: January 16, 2019, 08:35:26 AM by TBRANSO1 »

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7037
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Win32 Windows - Iczelion's Win32 Assembly Tutorial
« Reply #4 on: January 16, 2019, 11:03:07 AM »
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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: