News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Translating from 32 bit to 64 bit

Started by frktons, August 25, 2010, 08:00:29 PM

Previous topic - Next topic

GregL

#15
OK, I found ml64 in VC 2010 Express.  Boy, you don't want to rely on Microsofts search do you, it never found it. I looked in the path you guys mentioned and there it was.   It was a Program Files directory versus Program Files (x86) directory problem on my end. The Windows Explorer search works like it should.

GregL

I'm not setup for assembling yet but here's the code I am going to assemble for Frank when I can. If someone else can assemble it, have at it.


EXTERN ExitProcess:PROC
EXTERN printf:PROC
EXTERN _getch:PROC

INCLUDELIB kernel32.lib
;INCLUDELIB user32.lib
INCLUDELIB msvcrt.lib

.DATA

    msg BYTE "Clearing done",13,10,13,10

.DATA?

    buf2clear CHAR_INFO 2000 DUP(<>)    ; SIZEOF CHAR_INFO = 4

.CODE

main PROC

    sub rsp, 40

    lea rcx, buf2clear
    mov rdx, 1000       ; (SIZEOF buf2clear) / 8
    call ClearBufferQw
   
    lea rcx, msg
    call wsprintfA
   
    call WaitKey

    xor ecx, ecx
    call ExitProcess

main ENDP
; -------------------------------------------------------------------------
ClearBufferQw PROC

    ; int ClearBuffer(char* AddrBuffer, int count);
   
    push rdi
   
    mov rdi, rcx

    mov rcx, rdx

    mov rax, 2020202020202020h

    rep stosq

    pop rdi

    ret

ClearBufferQw ENDP

; -------------------------------------------------------------------------
WaitKey PROC

    ; int WaitKey(void);

    LOCAL dwCrLf:DWORD
    LOCAL dwChar:DWORD

    .DATA

        szPrompt BYTE 13,10,"Press any key to exit ... ", 0

    .CODE

        sub rsp, 40

        mov dwCrLf, 00000A0Dh

        lea rcx, szPrompt
        call printf
        call _getch
        cmp eax, 0
        je again
        cmp eax, 0E0h
        je again
        jmp @F
     again:
        call _getch
     @@:
        mov dwChar, eax
        lea rcx, dwCrLf
        call printf

        mov eax, dwChar
        add rsp, 40

        ret
WaitKey ENDP
;-----------------------------------------------------------
END start


frktons

Quote from: GregL on September 06, 2010, 12:47:22 AM
I'm not setup for assembling yet but here's the code I am going to assemble for Frank when I can. If someone else can assemble it, have at it.


EXTERN ExitProcess:PROC
EXTERN printf:PROC
EXTERN _getch:PROC

INCLUDELIB kernel32.lib
;INCLUDELIB user32.lib
INCLUDELIB msvcrt.lib

.DATA

    msg BYTE "Clearing done",13,10,13,10

.DATA?

    buf2clear CHAR_INFO 2000 DUP(<>)    ; SIZEOF CHAR_INFO = 4

.CODE

main PROC

    sub rsp, 40

    lea rcx, buf2clear
    mov rdx, 1000       ; (SIZEOF buf2clear) / 8
    call ClearBufferQw
   
    lea rcx, msg
    call wsprintfA
   
    call WaitKey

    xor ecx, ecx
    call ExitProcess

main ENDP
; -------------------------------------------------------------------------
ClearBufferQw PROC

    ; int ClearBuffer(char* AddrBuffer, int count);
   
    push rdi
   
    mov rdi, rcx

    mov rcx, rdx

    mov rax, 2020202020202020h

    rep stosq

    pop rdi

    ret

ClearBufferQw ENDP

; -------------------------------------------------------------------------
WaitKey PROC

    ; int WaitKey(void);

    LOCAL dwCrLf:DWORD
    LOCAL dwChar:DWORD

    .DATA

        szPrompt BYTE 13,10,"Press any key to exit ... ", 0

    .CODE

        sub rsp, 40

        mov dwCrLf, 00000A0Dh

        lea rcx, szPrompt
        call printf
        call _getch
        cmp eax, 0
        je again
        cmp eax, 0E0h
        je again
        jmp @F
     again:
        call _getch
     @@:
        mov dwChar, eax
        lea rcx, dwCrLf
        call printf

        mov eax, dwChar
        add rsp, 40

        ret
WaitKey ENDP
;-----------------------------------------------------------
END start



Thanks Greg, very kind of you.  :U

I could assemble it myself if you tell me the line command parameters
to pass to ML and LINK. I'm using the MASM32 editor and I'm not used anymore to use
command line tools, but I used them for about 20 years. Now I'm trying to get
used to them again, after a "long pause"  :P

Frank
Mind is like a parachute. You know what to do in order to use it :-)

frktons

#18
Probably MASM 64 needs something different to assemble.
I tried ml64 c:\x64_examples\ClearBufferGreg.asm /link subsystem:console ClearBufferGreg.obj and got:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64>ml64 c:\x64_examples\ClearBufferGreg.asm /link subsystem:console ClearBufferGreg.obj
Microsoft (R) Macro Assembler (x64) Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

Assembling: c:\x64_examples\ClearBufferGreg.asm
c:\x64_examples\ClearBufferGreg.asm(21) : error A2008:syntax error : buf2clear
c:\x64_examples\ClearBufferGreg.asm(104) : error A2008:syntax error : start
c:\x64_examples\ClearBufferGreg.asm(104) : error A2088:END directive required at end of file
c:\x64_examples\ClearBufferGreg.asm(31) : error A2006:undefined symbol : buf2clear
c:\x64_examples\ClearBufferGreg.asm(36) : error A2006:undefined symbol : wsprintfA


Edit: I think MASM doesn't know anything about CHAR_INFO, so I changed:


buf2clear CHAR_INFO 2000 DUP(<>)


with:

buf2clear db  8000 DUP(?)


To get rid of the first syntax error.

After I changed:

END start


with:

END

because MASM 64 doesn't seem to like "END start".

And now the last " error A2006:undefined symbol : wsprintfA"
that I don't know how to manage. Let's think a bit about it.  :P
It reminds me of a C function...

What about changing it with printf ?
At least it assembles  :lol

Well, after getting rid of the /link etc..., using only: ml64 c:\x64_examples\ClearBufferGreg.asm
it compiled and linked.

Now we have a working example that says:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64>clearbuffergreg
Clearing done


Press any key to exit ...
Press any key to exit ...


Oh, two Press at once  ::)

It should be because I'm using command line, let's try again....

It's the same with clicking on the name from Windows....

It depends on the fact that wsprintfa is used to fill a buffer
with the string representation of what we print with it, so I used
printf and of course it displays two times the message.

Maybe we can get rid of wsprintfa?

Well, I've to go now. I leave it to you Greg, and thanks again for
being so kind and providing this example.  :U

Frank
Mind is like a parachute. You know what to do in order to use it :-)

frktons

Being attracted from 64 bit programming I started using ML64/LINK command line tools
and created a .BAT file to help me assembling and linking my future experiments.  :P

in the default directory of VS2010:


C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>


I put this .BAT file:


@echo off
rem ----------------------------------------
rem batch file to assemble/link
rem ML64 programs in c:\x64
rem ----------------------------------------
ml64 /c c:\x64\%1.asm 
link %1.obj /OUT:c:\x64\%1.exe
del %1.obj
echo --- Assemble/link END ------


Almost ready for enjoying some x64 code and samples.  :lol

Frank
Mind is like a parachute. You know what to do in order to use it :-)

GregL

Frank,

You are so close, wsprintfA should be changed to printf.  I'll get set up to assemble soon.


frktons

Quote from: GregL on September 06, 2010, 07:28:06 PM
Frank,

You are so close, wsprintfA should be changed to printf.  I'll get set up to assemble soon.

I did Greg in a previous experiment, but strangely it prints:

Clearing done


Press any key to exit ...
Press any key to exit ...


I don't understand why it prints twice: Press any key to exit ...



And I think it was not what you intended to do.

Frank
Mind is like a parachute. You know what to do in order to use it :-)

GregL

Frank,

I don't understand either. You can get rid of the Waitkey procedure entirely, if you want.




frktons

Quote from: GregL on September 06, 2010, 07:51:17 PM
Frank,

I don't understand either. You can get rid of the Waitkey procedure entirely, if you want.

The Waitkey PROC is a good example of waiting for a key stroke, I'd better try to fix it.
But there are some code I'm not able to understand:


        sub rsp, 40

        mov dwCrLf, 00000A0Dh


and after:


        mov dwChar, eax
        lea rcx, dwCrLf
        call printf

        mov eax, dwChar
        add rsp, 40

        ret


What should this code do?
Apparentely it prints again "Press any key to exit ..." on the screen.

After it has already printed it with:


        lea rcx, szPrompt
        call printf


But probably it should do something different.  ::)

Just trying to understand a bit more...

Frank


Mind is like a parachute. You know what to do in order to use it :-)

GregL

Frank,

OK, I got set up to assemble. This works. The problem was msg was not terminated. Waitkey should work with dwCrLf instead of szCrLf, either way.


EXTERN ExitProcess:PROC
EXTERN printf:PROC
EXTERN _getch:PROC

INCLUDELIB Kernel32.lib
INCLUDELIB msvcrt.lib

.DATA

    msg BYTE "Clearing done",13,10,0

.DATA?

    buf2clear BYTE 8000 DUP(?)

.CODE

main PROC

    lea rcx, buf2clear
    call ClearBuffer
   
    lea rcx, msg
    call printf
   
    call WaitKey

    xor ecx, ecx
    call ExitProcess

main ENDP
; -------------------------------------------------------------------------
ClearBuffer PROC

    push rdi
   
    mov rdi, rcx

    mov rcx, 1000   ; (SIZEOF buf2clear) / 8

    mov rax, 2020202020202020h

    rep stosq

    pop rdi

    ret

ClearBuffer ENDP

; -------------------------------------------------------------------------
WaitKey PROC

    ; int WaitKey(void);

    LOCAL dwChar:DWORD

    .DATA

        szPrompt BYTE 13,10,"Press any key to exit ... ", 0
        szCrLf   BYTE 13,10,0

    .CODE

        sub rsp, 40

        lea rcx, szPrompt
        call printf
        call _getch
        cmp eax, 0
        je again
        cmp eax, 0E0h
        je again
        jmp @F
     again:
        call _getch
     @@:
        mov dwChar, eax
        lea rcx, szCrLf
        call printf

        mov eax, dwChar
        add rsp, 40

        ret
WaitKey ENDP
;-----------------------------------------------------------
END


frktons

Very well done Greg  :U

Now to make it really complete we should add a couple of RDTSC
before and after the code that clears the buffer and print the result
after saying "Clearing done".

I'll try it but, being my first experience both with x64 and RDTSC,
better if somebody helps.

Frank
Mind is like a parachute. You know what to do in order to use it :-)

GregL

Frank,

Have a go at it.  I just noticed there should be a sub rsp, 40 at the beginning of the main procedure.


frktons

Quote from: GregL on September 06, 2010, 11:11:12 PM
Frank,

Have a go at it.  I just noticed there should be a sub rsp, 40 at the beginning of the main procedure.

Yes, there is a sub rsp, 40, and it is one of the things I have to understand yet.  :P

I'll take my time to do some reading and experiments after I'll be back to my pc, now I'm
going out of town for some days in a computerless/internetless environment.  :lol

Frank
Mind is like a parachute. You know what to do in order to use it :-)

GregL

#28
Frank,

Have a good time. Read up on the x64 calling convention.


sub rsp, 40

32 = The caller is responsible for allocating space for parameters to the callee, and must always allocate sufficient space for the 4 register parameters, even if the callee doesn't have that many parameters.

Any additional parameters are 8 bytes each.

Add 8 for the return address.

The resulting number is then aligned to 16 if needed.

[Edit]  Ooops, corrected above  :red

frktons

Quote from: GregL on September 06, 2010, 11:22:03 PM
Frank,

Have a good time. Read up on the x64 calling convention.
sub rsp, 40

32 = The caller is responsible for allocating space for parameters to the callee, and must always allocate sufficient space for the 4 register parameters, even if the callee doesn't have that many parameters.

8 = Align stack to 16


Thanks for the link and tip  :U

They'll be useful when I'll be back to my pc.  :8)

Have a nice time

Frank

Mind is like a parachute. You know what to do in order to use it :-)