News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Playtime with ML64 and a question on spill space.

Started by hutch--, June 23, 2016, 01:52:21 PM

Previous topic - Next topic

hutch--

I have put together a test framework using the 64 bit binaries from VC2010 and the include files and libraries that Mikl_ has been using  below is a bare bones example so that my question will be understood.

The batch file to build the example.

@echo off

\masm64\bin\ml64.exe /c test1.asm

\masm64\bin\link.exe /SUBSYSTEM:WINDOWS /ENTRY:main test1.obj

pause


The bare minimum source code to demonstrate what I need to know.

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

    OPTION DOTNAME
   
    option casemap:none

    include \masm64\include\win64.inc
    include \masm64\include\temphls.inc

    include \masm64\include\kernel32.inc
    include \masm64\include\user32.inc

    includelib \masm64\lib\user32.lib   
    includelib \masm64\lib\kernel32.lib

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

  .data
    pmsg db "This example is written in ML64.EXE",0
    pttl db "Howdy Folks",0

  .data?

  .code

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

main proc

    sub rsp, 40

    invoke MessageBox,0,ADDR pmsg,ADDR pttl,0

    invoke ExitProcess,0

main endp

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

  end


What I need to know is why the spill space needs to be set at a specific size, I looked up Mikl_'s example which showed 40 bytes and if I add more it will not work and if I set it to less it will not work. I need to know why and if anyone has some definitive reference material on how and why spill space is configured, it will be most appreciated.

habran

Hi hutch :biggrin:
If you use HJWasm you don't have to worry about these intricacies, it will take care of everything :t
However, here is everything clearly explained with examples. Only HJWasm is able to do those things which you can find there.
I appreciate your will to step in to a "Brave New World" :t

Cheers!

Cod-Father

rrr314159

Have you read the ABI? And, there are many tutorials on the topic. Google is your friend.
I am NaN ;)

rrr314159

Still I must admit that many of the tutorials have mistakes, are hard to follow, and don't really answer your question. The trick is to include these keywords in your Google search: "good", "correct" "relevant", and "understandable". That filters out all the bad, wrong, irrelevant ones that are impossible to understand. In your case you should probably also use "written_in_Australian". You're welcome in advance!
I am NaN ;)

hutch--

I confess answer of this type are about as useful as a hip pocket in a singlet.

Mikl__

Hi, hutch--!
I'm not explain in English, but it may be to be clearly understood
Quote;immediately after entry into the program <-- rsp=2CFC58
13F921000: sub rsp,28h <-- rsp=2CFC30 <-- align 10h
13F921004: xor ecx,ecx
13F921006: xor r9d,r9d
13F921009: lea rdx,[140003000];"This example is written in ML64.EXE",0
13F921010: lea r8,[140003024];"Howdy Folks",0
13F921017: call MessageBoxA
;immediately after the call instruction
;          RSP = 2CFC28 [RSP]=13FFA101D<-- Address of Return
;                2CFC30 [RSP+8]=0 <-- RCX_Home
;                2CFC38 [RSP+10]=0 <-- RDX_Home
;                2CFC40 [RSP+18]=0 <-- R8_Home
;                2CFC48 [RSP+20]=0 <-- R9_Home
13FFA101D: xor ecx,ecx     
13F92101F: call ExitProcess
Now try this option, it runs in  Winx64 Seven
    OPTION DOTNAME
    option casemap:none

    include \masm64\include\win64.inc
    include \masm64\include\temphls.inc

    include \masm64\include\kernel32.inc
    include \masm64\include\user32.inc

    includelib \masm64\lib\user32.lib
    includelib \masm64\lib\kernel32.lib

  .data
    pmsg db "This example is written in ML64.EXE",0
    pttl db "Howdy Folks",0
  .code
main proc
    push rbp
    invoke MessageBox,0,ADDR pmsg,ADDR pttl,0
    pop rbp
    retn
main endp
end

hutch--

Thanks Mikl_, that worked fine but I am none the wise why. I am testing this on Win10 64 bit Professional.


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

    OPTION DOTNAME
   
    option casemap:none

    include \masm64\include\win64.inc
    include \masm64\include\temphls.inc

    include \masm64\include\kernel32.inc
    include \masm64\include\user32.inc
    include \masm64\include\msvcrt.inc

    includelib \masm64\lib\user32.lib   
    includelib \masm64\lib\kernel32.lib
    includelib \masm64\lib\msvcrt.lib

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

  .data?
    msize db 32 dup (?)

  .data
    ptrm dq msize
    pttl db "Memory Address",0

  .code

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

main proc

    LOCAL pMem  :QWORD

    ;;; sub rsp, 40
    push rbp

    invoke GlobalAlloc,GMEM_FIXED   ,1024*1024*1024*8         ; 8 gig
    mov pMem, rax

; char *_itoa(
;    int value,
;    char *str,
;    int radix

  ; ----------------------------------
  ; convert memory address into string
  ; ----------------------------------
    invoke _itoa,pMem,ptrm,10

    invoke MessageBox,0,ptrm,ADDR pttl,0

    invoke GlobalFree,pMem

    invoke ExitProcess,0

    pop rbp

main endp

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

hutch--

#7
Doing a quick search on Google, is there anything better in terms of reference material for the 64 bit calling convention than the following URL ?

https://msdn.microsoft.com/en-us/library/ms235286.aspx

Mikl__

OPTION DOTNAME
   
    option casemap:none

    include \masm64\include\win64.inc
    include \masm64\include\temphls.inc

    include \masm64\include\kernel32.inc
    include \masm64\include\user32.inc
    include \masm64\include\msvcrt.inc

    includelib \masm64\lib\user32.lib   
    includelib \masm64\lib\kernel32.lib
    includelib \masm64\lib\msvcrt.lib
    OPTION PROLOGUE:rbpFramePrologue

  .data?
    msize db 32 dup (?)

  .data
    ptrm dq msize
    pttl db "Memory Address",0

  .code
main proc

    LOCAL pMem  :QWORD

    invoke GlobalAlloc,GMEM_FIXED   ,1024*1024*1024*2*2;8         4 gig
    mov pMem, rax

; char *_itoa(
;    int value,
;    char *str,
;    int radix

  ; ----------------------------------
  ; convert memory address into string
  ; ----------------------------------
    invoke _itoa,rax,ptrm,10
    invoke MessageBox,NULL,ptrm,&pttl,MB_OK
    invoke GlobalFree,pMem
    invoke ExitProcess,0
main endp
end

habran

hutch, if you are looking for the pill try to contact Bradley Cooper otherwise, you will have to roll the sleeves and sweat blood :biggrin:
I've given you the address where to go in the post above 8)
Cod-Father

mineiro

I think you're talking about shadow space. Because fastcall calling convention arguments are passed throught registers (rcx,rdx,r8,r9), so you should store these registers on stack at start of your procedure to if anything goes wrong, system can recover that info.Rsp should be aligned to 10h multiple before a call instruction. Not sure, but appears that only even number of arguments, or you should do a foo on stack to align stack. On your program entry point rsp ends with 8h.
You have some way, you can wait while coding your procedure to see whats the biggest function parameters you're using and after do a sub rsp,?? and after add rsp,?? only one time (the biggests supports reusable space to less functions); or you can do this after each function call.
I don't have a 64 windows to try, I'm talking only using my memory, but veh or seh deals like start_address and end_address to be monitored, you setup a range of address, and with arguments on stack you can see what happened before a possible error.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

Mikl__

Olá, mineiro!
Desculpe, mas não é claro a quem você está se referindo? Para mim, habran ou hutch--?

mineiro

Para o senhor hutch senhor Mikl___, respondendo a questão sobre spill space. Um forte abraço irmão. Seus exemplos postados aqui no fórum são muito úteis, de grande valia.

I'm talking about spill space, answering author topic.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

Mikl__


mineiro

Seria interessante o senhor postar um exemplo sobre veh (manipulação estruturada de erros) para win64. Não é tão complicado quanto parece, e o senhor sabe usar o windbg pelo que pude perceber. Abraços senhor Mikl___. Não precisa se desculpar irmão, estamos no mesmo barco.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything