News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

OFFSET forward reference

Started by wind, April 26, 2020, 03:02:12 PM

Previous topic - Next topic

wind

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

Siekmanski

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
Creative coders use backward thinking techniques as a strategy.

wind

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.

jj2007

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.

Vortex

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

wind

 :thumbsup: Thanks guys @jj2007, @Vortex

Vortex

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

wind

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