The MASM Forum

Microsoft 64 bit MASM => Examples => Topic started by: hutch-- on August 03, 2018, 01:31:47 PM

Title: Pass arguments in a structure.
Post by: hutch-- on August 03, 2018, 01:31:47 PM
Elegant simplicity of MASM, a clean clutter free technique for passing multiple arguments in a structure. This builds at a disk busting 1.5k.  :P


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    mbargs STRUCT                   ; custom structure (write your own)
      hndl dq ?
      mesg dq ?
      titl dq ?
      styl dq ?
    mbargs ends

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

entry_point proc

    LOCAL mba :mbargs               ; allocate local structure

    mov mba.styl, MB_OK             ; load the MB style
  ; -------------------------------------------
  ; assign the strings to the structure members
  ; -------------------------------------------
    sas mba.titl, "MessageBox Title"
    sas mba.mesg, "Text Message in a MessageBox"
    mov mba.hndl, 0                 ; handle in this instance is 0

    rcall MbProc,ptr$(mba)          ; call proc with structure address

    .exit

entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

MbProc proc

    USING r15                       ; preserve r15

    SaveRegs

    mov r15, rcx                    ; load structure address into r15

  ; ------------------------------------------------------------
  ; call the MessageBox using the structure members as arguments
  ; ------------------------------------------------------------
    rcall MessageBox,(mbargs PTR [r15]).hndl, \
                     (mbargs PTR [r15]).mesg, \
                     (mbargs PTR [r15]).titl, \
                     (mbargs PTR [r15]).styl
    RestoreRegs

    ret

MbProc endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end

comment #

.text:0000000140001000 C8800000                   enter 0x80, 0x0
.text:0000000140001004 4881EC80000000             sub rsp, 0x80
.text:000000014000100b 48C7459800000000           mov qword ptr [rbp-0x68], 0x0
.text:0000000140001013 488B0557100000             mov rax, qword ptr [0x140002071]
.text:000000014000101a 48894590                   mov qword ptr [rbp-0x70], rax
.text:000000014000101e 488B0578100000             mov rax, qword ptr [0x14000209d]
.text:0000000140001025 48894588                   mov qword ptr [rbp-0x78], rax
.text:0000000140001029 48C7458000000000           mov qword ptr [rbp-0x80], 0x0
.text:0000000140001031 488D4580                   lea rax, [rbp-0x80]
.text:0000000140001035 488BC8                     mov rcx, rax
.text:0000000140001038 E80D000000                 call sub_14000104a
.text:0000000140001038
.text:000000014000103d 48C7C100000000             mov rcx, 0x0
.text:0000000140001044 FF15BA100000               call qword ptr [ExitProcess]
.text:0000000140001044
; --------------------------------------------------------------------------
; sub_14000104a
; --------------------------------------------------------------------------
sub_14000104a   proc
.text:000000014000104a C8800000                   enter 0x80, 0x0
.text:000000014000104e 4883EC70                   sub rsp, 0x70
.text:0000000140001052 4C897D90                   mov qword ptr [rbp-0x70], r15
.text:0000000140001056 4C8BF9                     mov r15, rcx
.text:0000000140001059 4D8B4F18                   mov r9, qword ptr [r15+0x18]
.text:000000014000105d 4D8B4710                   mov r8, qword ptr [r15+0x10]
.text:0000000140001061 498B5708                   mov rdx, qword ptr [r15+0x8]
.text:0000000140001065 498B0F                     mov rcx, qword ptr [r15]
.text:0000000140001068 FF15A6100000               call qword ptr [MessageBoxA]
.text:0000000140001068
.text:000000014000106e 4C8B7D90                   mov r15, qword ptr [rbp-0x70]
.text:0000000140001072 C9                         leave
.text:0000000140001073 C3                         ret
sub_14000104a   endp

#
Title: Re: Pass arguments in a structure.
Post by: jj2007 on August 03, 2018, 04:18:55 PM
Nice. What's the purpose of using r15, can't you just take rax for that?
Title: Re: Pass arguments in a structure.
Post by: hutch-- on August 03, 2018, 05:28:06 PM
Its more or less a discipline of avoiding volatile registers where there is a risk of it being overwritten. I don't think there would be any problems in using rax in this context.