Author Topic: A probably dull question on a macro  (Read 906 times)

clamicun

  • Member
  • **
  • Posts: 240
A probably dull question on a macro
« on: February 24, 2015, 04:43:36 AM »
;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

  • Member
  • *****
  • Posts: 8749
  • Still using Abacus 2.0
    • DednDave
Re: A probably dull question on a macro
« Reply #1 on: February 24, 2015, 05:43:26 AM »
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

Code: [Select]
      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

  • Member
  • *****
  • Posts: 7730
  • Assembler is fun ;-)
    • MasmBasic
Re: A probably dull question on a macro
« Reply #2 on: February 24, 2015, 07:59:48 AM »
wsprintf writes to the buffer - and the buffer is in the code section, where writing is not allowed.
This works:

Code: [Select]
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

  • Member
  • **
  • Posts: 240
Re: A probably dull question on a macro
« Reply #3 on: February 25, 2015, 02:12:05 AM »
Thank you guys,
wsprintf writes to the buffer !
I'm learning.