Author Topic: Pass arguments in an array.  (Read 4608 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10583
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Pass arguments in an array.
« on: August 24, 2016, 11:02:15 PM »
In this brave new world of mixed hybrid calling conventions, here is another one to add to the collection.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
;                              Pass procedure arguments in an array
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    .code

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

entry_point proc

    LOCAL arr[16] :QWORD                    ; create a QWORD array on the stack

    lea rcx, arr                            ; load its address into RCX
    mov QWORD PTR [rcx], 1
    mov QWORD PTR [rcx+8], 2
    mov QWORD PTR [rcx+16], 3
    mov QWORD PTR [rcx+24], 4
    mov QWORD PTR [rcx+32], 5
    mov QWORD PTR [rcx+40], 6
    mov QWORD PTR [rcx+48], 7
    mov QWORD PTR [rcx+56], 8
    call arrtest                            ; call the arrtest proc with address in RCX

    waitkey

    invoke ExitProcess,0

    ret

entry_point endp

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

arrtest proc parr:QWORD

    LOCAL .r15 :QWORD

    mov [rbp+16], rcx                       ; copy RCX into parr address
    mov .r15, r15                           ; preserve r15

    mov r15, parr

    conout str$(QWORD PTR [r15]),lf
    conout str$(QWORD PTR [r15+8]),lf
    conout str$(QWORD PTR [r15+16]),lf
    conout str$(QWORD PTR [r15+24]),lf
    conout str$(QWORD PTR [r15+32]),lf
    conout str$(QWORD PTR [r15+40]),lf
    conout str$(QWORD PTR [r15+48]),lf
    conout str$(QWORD PTR [r15+56]),lf

    mov r15, .r15                           ; restore r15

    ret

arrtest endp

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

    end
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: Pass arguments in an array.
« Reply #1 on: August 24, 2016, 11:47:30 PM »
Hmmm... where is the difference to passing a pointer to a STRUCT?

sinsi

  • Guest
Re: Pass arguments in an array.
« Reply #2 on: August 24, 2016, 11:58:52 PM »
Or
Code: [Select]
;assume stack is para aligned
push 8
push 7
push 6
push 5
push 4
push 3
push 2
push 1
mov rcx,rsp
sub rsp,32 ;maybe not needed?
call arrtest
add rsp,32+8*8 ;or reuse
16 array slots but only 8 filled?

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10583
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Pass arguments in an array.
« Reply #3 on: August 25, 2016, 03:44:42 AM »
JJ,

> Hmmm... where is the difference to passing a pointer to a STRUCT?

Less typing.  :P Each has its place but with a long list of identical items, an array is simpler.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10583
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Pass arguments in an array.
« Reply #4 on: August 25, 2016, 03:51:45 AM »
 :biggrin:

> 16 array slots but only 8 filled?

Alway allows room for more.  :P

If you push the args you must correct the stack, either in the tail end of the called proc or after the procedure call. Writing to a RSP relative address does not have the problem and it allows for all integer data types from BYTE to QWORD.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: