News:

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

Main Menu

Win32 My First Window

Started by tda0626, April 24, 2024, 11:05:19 AM

Previous topic - Next topic

NoCforMe

Quote from: sudoku on May 03, 2024, 10:32:25 AMTip: for invoke statements we use 'addr' not 'offset', generally.
"We"? Who is the "we" you refer to here? Certainly not me.

Use OFFSET or ADDR depending:
  • Use OFFSET when the thing you want the address of is a global variable (i.e., in the .data or .data? sections), meaning that it resides at a known fixed address
  • Use ADDR when it's a LOCAL variable (on the stack of a procedure) or you need the addresss of a variable pointed to by a register (like [EDX].STRUCTURE.member), and therefore the assembler cannot determine a fixed address for it
When you use ADDR, the assembler inserts the following code:
  LEA  EAX, <variable>
OFFSETs are treated as constant values, just like any other number.
Assembly language programming should be fun. That's why I do it.

zedd151

Quote from: tda0626 on May 03, 2024, 10:55:46 AMSee that is why I come here, you guys are awesome and fast!  :biggrin:

Thanks Sudoku!
Not a problem.  :smiley:

NoCforMe, I never use offset in an invoke statement. And I have rarely seen any other source code with it used in that fashion. It may exist somewhere but it is much less common. Thats all I have to say about that.  :smiley:
https://masm32.com/board/index.php?msg=53727

NoCforMe

#107
    INVOKE    SendMessage, hWin, WM_SETTEXT, 0, OFFSET <text2set>

Nothing special about using OFFSET in an INVOKE. Don't be superstitious.

However, in this case you gotta use ADDR:
DoSomething    PROC
      LOCAL string[32]:BYTE

      INVOKE  SendMessage, hWin, WM_SETTEXT, 0, ADDR string
Assembly language programming should be fun. That's why I do it.

sinsi

.data?
hinst DWORD ?
.code
tester PROC
LOCAL LocalVar:DWORD
    mov eax,offset hinst          ;ok
    mov eax,addr [hinst]          ;syntax error
    mov eax,addr hinst            ;syntax error
    invoke MyProc,addr hinst      ;ok, hinst is in .data, ML emits "OFFSET hinst"
    invoke MyProc,offset hinst    ;ok
    invoke MyProc,addr LocalVar   ;ok, ML emits "LEA EAX,LocalVar"  "PUSH EAX"
    invoke MyProc,offset LocalVar ;error A2098:invalid operand for OFFSET

NoCforMe

More "refinement". Looking at Sudoku's converted code:
    WndProc proc hWnd:HWND, message:UINT, wParam:WPARAM, lParam:LPARAM
    local hdc:HDC
    local rect:RECT
   
    SWITCH message
    CASE WM_PAINT
        invoke BeginPaint, hWnd, offset ps    ; was addr ps. ps is a global so offset must be used
        mov hdc, eax
While that'll work, it's a bit strange to have some of the paint variables in the routine as LOCALs and some as globals. I'd make them all LOCALs, in which case you'd need to use ADDR instead of OFFSET:
    local ps:PAINTSTRUCT

    invoke BeginPaint, hWnd, ADDR ps
Those variables kind of go together as a "set". Plus you never have to worry about any collisions between variables if you have more than one WM_PAINT handler (I have programs that have several).
Plus the vars don't take up any space in the executable.
Assembly language programming should be fun. That's why I do it.

zedd151

Quote from: NoCforMe on May 03, 2024, 11:18:38 AMMore "refinement". Looking at Sudoku's converted code:
Link?

NoCforMe

It was in reply #102, and it sure looks like your (Sudoku's) code, although it was attached by the OP. ???
Assembly language programming should be fun. That's why I do it.

zedd151

Quote from: NoCforMe on May 03, 2024, 11:27:47 AMIt was in reply #102, and it sure looks like your (Sudoku's) code, although it was attached by the OP. ???
Surely you jest...
I won't even dignify that with a more suitable response. 

A more suitable response posted below in post #114.

NoCforMe

So that's not your code? If so, sorry. No need to get on your high horse. (I said it looked like your code.)
Assembly language programming should be fun. That's why I do it.

zedd151

Quote from: NoCforMe on May 03, 2024, 12:07:15 PMSo that's not your code? If so, sorry. No need to get on your high horse. (I said it looked like your code.)

quite certain, 100% in fact. further reading in case you missed it...

Quote from: tda0626 on May 03, 2024, 05:34:23 AM
Quote from: TimoVJL on May 02, 2024, 06:41:12 PMMany of us remember this, from Petzold book:
HelloWin petzold-pw5e

Yep, I have a copy of this book and translated his code over to assembler for practice.

Tim

TimoVJL

#115
Quote from: NoCforMe on May 03, 2024, 11:18:38 AMThose variables kind of go together as a "set". Plus you never have to worry about any collisions between variables if you have more than one WM_PAINT handler (I have programs that have several).
Plus the vars don't take up any space in the executable.

Neither .data? section take any space in the executable and are zeroes at beginning.
But globals aren't so good for portable functions.

EDIT:
Strings are global and optimizers use same similar strings to avoid duplicates.
This thread gives some info for converting C code to asm in many ways.
May the source be with you

jj2007

Quote from: sudoku on May 03, 2024, 10:58:24 AMI never use offset in an invoke statement. And I have rarely seen any other source code with it

You haven't seen my sources :biggrin:

Surely, it's a matter of taste, and offset is two characters more to type, but with more complex sources you will quickly appreciate to know at one glance "oh, it's a global variable".

sinsi

Quote from: jj2007 on May 03, 2024, 05:28:45 PMbut with more complex sources you will quickly appreciate to know at one glance "oh, it's a global variable".
Yep

Quote from: TimoVJL on May 03, 2024, 04:59:01 PMBut globals aren't so good for portable functions.
Or multithreaded functions.

NoCforMe

Quote from: tda0626 on May 03, 2024, 10:03:08 AM
Quote from: NoCforMe on May 03, 2024, 06:58:51 AM
Quote from: tda0626 on May 03, 2024, 05:48:47 AMLooked at his text metrics code and besides the custom include file he wants you to import into the program, I have no clue to translating some of his c code in that section.
What program is that exactly? I have his book (yes, actual book, the size of a boat anchor) in front of me. I could take a look at that code.
In the 5th edition, it is on page 73. The custom include file starts on page 70.
I just checked my book (yes, the actual physical book, which makes a pretty good doorstop), and there's no program on either of those pages. Could you tell me which program exactly it is you want to convert? Petzold names all his programs ("SYSMETS1", etc.). I'll be happy to take a look at it. I won't "do your homework" but I may be able to offer some pointers (pun intended).
Assembly language programming should be fun. That's why I do it.

zedd151

Quote from: NoCforMe on May 04, 2024, 04:12:29 AMI'll be happy to take a look at it.
I am fairly certain (75%) it is the code where TimoVJL's link points to. ( if you are refering to his 'converted' code) Thats why I posted that link above. #114

Quote from: TimoVJL on May 02, 2024, 06:41:12 PMMany of us remember this, from Petzold book:
HelloWin petzold-pw5e
Click the link and see...

Else there is a header file "SysMets.h" a bit further down that file tree there. (the "custom include file")???
 header file
That sounds to me like what he means by "Looked at his text metrics code and besides the custom include file he wants you to import into the program"