The MASM Forum

General => The Campus => Topic started by: wind on April 26, 2020, 03:02:12 PM

Title: OFFSET forward reference
Post by: wind on April 26, 2020, 03:02:12 PM
Iczelion's tut02 mentioned that offset handles forward reference, I moved down 2 lines from .data to after invoke, and it didn't assemble or build any more. ideas?

.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib

.data
       
.code
start:
invoke MessageBox, NULL,offset MsgBoxText, offset MsgCaption, MB_OK
invoke ExitProcess,NULL

    MsgCaption     db "Iczelion's tutorial no.2",0
    MsgBoxText     db "Win32 Assembly is Great!",0
end start
Title: Re: OFFSET forward reference
Post by: Siekmanski on April 26, 2020, 03:18:53 PM
Strings are data and belong in the .data section

.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib

.data
    MsgCaption     db "Iczelion's tutorial no.2",0
    MsgBoxText     db "Win32 Assembly is Great!",0
       
.code
start:
invoke MessageBox, NULL,offset MsgBoxText, offset MsgCaption, MB_OK
invoke ExitProcess,NULL
end start
Title: Re: OFFSET forward reference
Post by: wind on April 26, 2020, 03:53:28 PM
Thanks man :biggrin:, yes your code indeed works  :thumbsup:.

But my question is about OFFSET to handle forward reference. This is the quote from tutorial, I don't think it's correct these days.

Quote
1. addr cannot handle forward reference while offset can. For example, if the label is defined somewhere further in the source code than the invoke line, addr will not work.
invoke MessageBox,NULL, addr MsgBoxText,addr MsgBoxCaption,MB_OK
......
MsgBoxCaption  db "Iczelion Tutorial No.2",0
MsgBoxText       db "Win32 Assembly is Great!",0
MASM will report error. If you use offset instead of addr in the above code snippet, MASM will assemble it happily.
Title: Re: OFFSET forward reference
Post by: jj2007 on April 26, 2020, 06:00:45 PM
The quote is half correct. Test piece (note the string is in the .code section):
include \masm32\include\masm32rt.inc
.code
start:
  invoke MessageBox, 0, offset TheMsg, chr$("The title"), MB_OK
  exit
TheMsg db "Just a message", 0
end start


Now it depends on the assembler you are using:
MASM throws an error for addr and offset
AsmC throws an error for addr and offset only if you use the /Zne "masm compatibility" option
UAsm and JWasm never throw an error.

Note this is one of the very rare cases where UAsm etc behave differently from MASM.
Title: Re: OFFSET forward reference
Post by: Vortex on April 26, 2020, 06:53:18 PM
Hi wind,

You can handle your forward references by using a custom invoke macro :

.386
.model flat, stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
includelib  \masm32\lib\kernel32.lib
include     \masm32\include\user32.inc
includelib  \masm32\lib\user32.lib

include     invoke.inc

.data
       
.code

start:

    _invoke     MessageBox,NULL,OFFSET MsgBoxText,\
                OFFSET MsgCaption,MB_OK
   
    _invoke     ExitProcess,NULL

    MsgCaption  db "Iczelion's tutorial no.2",0
    MsgBoxText  db "Win32 Assembly is Great!",0
   
END start
Title: Re: OFFSET forward reference
Post by: wind on April 26, 2020, 07:53:06 PM
 :thumbsup: Thanks guys @jj2007, @Vortex
Title: Re: OFFSET forward reference
Post by: Vortex on April 26, 2020, 11:18:51 PM
Hi wind,

Here is another trick for the forward references :

.386
.model flat, stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
includelib  \masm32\lib\kernel32.lib
include     \masm32\include\user32.inc
includelib  \masm32\lib\user32.lib

.data

text dd MsgBoxText
capt dd MsgCaption
       
.code

start:


    invoke      MessageBox,NULL,text,capt,MB_OK
   
    invoke      ExitProcess,NULL

    MsgCaption  db "Iczelion's tutorial no.2",0
    MsgBoxText  db "Win32 Assembly is Great!",0
   
END start
Title: Re: OFFSET forward reference
Post by: wind on April 26, 2020, 11:41:17 PM
Excellent. Works perfectly.  :thumbsup:

Quote from: Vortex on April 26, 2020, 11:18:51 PM
Hi wind,

Here is another trick for the forward references :

.386
.model flat, stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
includelib  \masm32\lib\kernel32.lib
include     \masm32\include\user32.inc
includelib  \masm32\lib\user32.lib

.data

text dd MsgBoxText
capt dd MsgCaption
       
.code

start:


    invoke      MessageBox,NULL,text,capt,MB_OK
   
    invoke      ExitProcess,NULL

    MsgCaption  db "Iczelion's tutorial no.2",0
    MsgBoxText  db "Win32 Assembly is Great!",0
   
END start