News:

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

Main Menu

A probably dull question on a macro

Started by clamicun, February 24, 2015, 04:43:36 AM

Previous topic - Next topic

clamicun

;I am trying to make friends with MASM macros.
;I ain't yet.
;Why doesn' the following code compile if ws_msg is LOCAL ?

include \masm32\MasmBasic\MasmBasic.inc     

;=============================================
show_value MACRO arg1,arg2

LOCAL  over
LOCAL  showint
LOCAL  author
;LOCAL  ws_msg                   ;This doesn't work

jmp over

showint TCHAR 'Value %s = %lu',0
author  TCHAR 'CMC',0
;ws_msg TCHAR 50 dup(?),0        ;This doesn't work
   
over:

pushad
;INVOKE wsprintf,addr ws_msg,addr showint,reparg(arg2),arg1      ;This should be
INVOKE wsprintf,offset ws_msg,addr showint,reparg(arg2),arg1
     
;INVOKE MessageBox,0,addr ws_msg,addr author,MB_OK               ;This should be
INVOKE MessageBox,0,offset ws_msg,addr author,MB_OK
popad

ENDM
;=============================================
.data

ws_msg TCHAR 50 dup(?),0     ;This shouldn't be necessary

.code
Init

mov eax,4294967294
show_value eax, "EAX"

exit
end start

dedndave

i am not very macro-friendly, either   :lol:

but, i can see where you will have problems if you define data in the .code section
the code section has a default attribute of PAGE_EXECUTE_READ
that means you can execute code or read data - but, not write data

https://msdn.microsoft.com/en-us/library/windows/desktop/aa366786%28v=vs.85%29.aspx

rather that using JMP, just open a different section
for some examples, see the \masm32\macros\macros.asm file

      sstr$ MACRO number
        LOCAL buffer
        .data?
          buffer TCHAR 40 dup (?)
          align 4
        .code
        IFNDEF __UNICODE__
          invoke crt__ltoa,number,ADDR buffer,10
        ELSE
          invoke crt__ltow,number,ADDR buffer,10
        ENDIF
        EXITM <eax>
      ENDM

jj2007

wsprintf writes to the buffer - and the buffer is in the code section, where writing is not allowed.
This works:

show_value MACRO arg1,arg2
; LOCAL  over
LOCAL  showint
LOCAL  author
LOCAL  ws_msg                   ;This works now

; jmp over

.data
showint TCHAR 'Value %s = %lu',0
author  TCHAR 'CMC',0
ws_msg TCHAR 50 dup(?),0        ; This works now
.code
; over:

pushad

clamicun

Thank you guys,
wsprintf writes to the buffer !
I'm learning.