Author Topic: Hello World of 64 bit programming  (Read 217 times)

zedd151

  • Member
  • ****
  • Posts: 847
Hello World of 64 bit programming
« on: August 05, 2018, 05:30:58 PM »
Another Hello World.
64 bit Message Box.  :D

Code: [Select]

    include \masm32\include64\masm64rt.inc

        .data
        msgMB db "Hello 64 bit assembly programming.  :D ", 0
        titleMB db "My first 64 bit Message Box", 0
        .code
       
        start proc
       
        mov r9, 0
        lea r8, titleMB
        lea rdx, msgMB
        mov rcx, 0
        call MessageBoxA
        mov rcx, 0
        call ExitProcess
       
        start endp
       
        end

And for those who thought I'd really given up on 64 bit asm,  :P
I'm not always the sharpest knife in the drawer, but I have my moments.  :P

Mikl__

  • Member
  • ****
  • Posts: 681
Re: Hello World of 64 bit programming
« Reply #1 on: August 05, 2018, 06:58:53 PM »
Code: [Select]
include \masm32\include64\masm64rt.inc
        .data
        msgMB db "Hello 64 bit assembly programming.  :D ", 0
        titleMB db "My first 64 bit Message Box", 0
        .code       
        start proc
       
        lea r8, titleMB
        lea rdx, msgMB
        invoke MessageBox,0,,,0
        invoke ExitProcess,0       
        start endp       
        end

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5659
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Hello World of 64 bit programming
« Reply #2 on: August 05, 2018, 08:36:39 PM »
 :biggrin:

I have not posted the revised version of the "rcall" macro but will shortly. Builds at 1.5k using Pelle's linker.


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

    include \masm32\include64\masm64rt.inc

    .code

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

 entry_point proc

    rcall MessageBox,0,"Message Text","The Title",0
    xor rcx, rcx
    call ExitProcess

 entry_point endp

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

    end

 comment #

  .text:0000000140001000 C8800000                   enter 0x80, 0x0
  .text:0000000140001004 4883EC60                   sub rsp, 0x60
  .text:0000000140001008 4D33C9                     xor r9, r9
  .text:000000014000100b 4C8B0558100000             mov r8, qword ptr [0x14000206a]
  .text:0000000140001012 488B1574100000             mov rdx, qword ptr [0x14000208d]
  .text:0000000140001019 4833C9                     xor rcx, rcx
  .text:000000014000101c FF15E2100000               call qword ptr [MessageBoxA]
  .text:000000014000101c
  .text:0000000140001022 4833C9                     xor rcx, rcx
  .text:0000000140001025 FF15C9100000               call qword ptr [ExitProcess]

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

zedd151

  • Member
  • ****
  • Posts: 847
Re: Hello World of 64 bit programming
« Reply #3 on: August 05, 2018, 08:48:25 PM »
Thanks, hutch----, Mikl____.



zedd....   :P
I'm not always the sharpest knife in the drawer, but I have my moments.  :P

Vortex

  • Member
  • *****
  • Posts: 1821
Re: Hello World of 64 bit programming
« Reply #4 on: August 05, 2018, 09:08:16 PM »
Hello,

Hello world with a size of 1K :

Code: [Select]
option casemap:none

EXTERN MessageBoxA:proc
EXTERN ExitProcess:proc

.code

main PROC

        sub     rsp,28h
        xor     r9d,r9d
        lea     r8,caption
        lea     rdx,text
        xor     rcx,rcx
        call    MessageBoxA

        xor     ecx, ecx
        call    ExitProcess

text    db 'Hello world', 0
caption db '64-bit test', 0

main ENDP

END

mineiro

  • Member
  • ***
  • Posts: 431
Re: Hello World of 64 bit programming
« Reply #5 on: August 05, 2018, 09:08:55 PM »
This message is out of scope;
I was doing some benchmark tests and sound to me that "xor ecx,ecx" is well optimized to clean rcx register.
I have tried "mov rcx,0","xor rcx,rcx","xor ecx,ecx","and rcx,0","sub rcx,rcx", ... .
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

zedd151

  • Member
  • ****
  • Posts: 847
Re: Hello World of 64 bit programming
« Reply #6 on: August 05, 2018, 09:19:36 PM »
This message is out of scope;
I was doing some benchmark tests and sound to me that "xor ecx,ecx" is well optimized to clean rcx register.
I have tried "mov rcx,0","xor rcx,rcx","xor ecx,ecx","and rcx,0","sub rcx,rcx", ... .

No, its fine. The information is useful.

I wonder now if xor ecx, ecx is optimized under x86 processors too...
I'm not always the sharpest knife in the drawer, but I have my moments.  :P

jj2007

  • Member
  • *****
  • Posts: 8619
  • Assembler is fun ;-)
    • MasmBasic
Re: Hello World of 64 bit programming
« Reply #7 on: August 05, 2018, 09:30:27 PM »
I wonder now if xor ecx, ecx is optimized under x86 processors too...
Yes, that's the officially recommended way of clearing a register. Shorter and faster than mov reg, 0.

zedd151

  • Member
  • ****
  • Posts: 847
Re: Hello World of 64 bit programming
« Reply #8 on: August 05, 2018, 09:54:56 PM »
Yes, that's the officially recommended way of clearing a register. Shorter and faster than mov reg, 0.

Damn, I can't 'cut' or 'paste' text from Pale Moon portable.....

> jj - Yeah, theres probably a 'benchmark test' thread here somewhere or on the old forum.  I kinda figured it would be a built-in optimization.

> vortex - time to start the 'smallest possible exe' again?  :P
just kidding, your input is welcome.


I'm not always the sharpest knife in the drawer, but I have my moments.  :P

Mikl__

  • Member
  • ****
  • Posts: 681
Re: Hello World of 64 bit programming
« Reply #9 on: August 05, 2018, 10:09:47 PM »
Olá, mineiro!
taken here
  • replace MOV with equivalent PUSH and POP
Code: [Select]
push <something-equal-to-0>
   pop  reg
  • imul reg,0
  • Shift X1 + X2 is more than/equal to the register size in bits, individually smaller. Less because it is taken in modulus
Code: [Select]
shr/shl/sal reg,X1; X1 <=31, X2<=31, X1+X2>=32
   shr/shl/sal reg,X2;
  • More perverted shift.
Code: [Select]
clc
   rcr/rcl reg,X1
   clc
   rcr/rcl reg,X2
  • and reg,0
  • Not quite an honest way, but ...
Code: [Select]
or reg, -1
   inc/not reg
  • Zero R/E/CX.
Code: [Select]
loop $
  • Zero the R/EDX
Code: [Select]
shr eax, 1
    cdq
  • Zero the AL. (AH = AL, AL = 0)
Code: [Select]
aam 1
  • Zero the AH
Code: [Select]
aad 0
  • Again AL
Code: [Select]
clc
    setalc; opcode: 0xD6
  • More tricky: read 0 from the port (for example port 81h)
Code: [Select]
mov dx,<some-port-number>
    in al, dx
  • Again AL
Code: [Select]
stc
    setnc al
  • 5 times bsf or bsr
Code: [Select]
bsf reg,reg
    bsf reg,reg
    bsf reg,reg
    bsf reg,reg
    bsf reg,reg
  • use the zero descriptor from GDT
Code: [Select]
sgdt[esp-6]
    mov reg,[esp-4]
    mov reg,[reg]
  • Read zero from the FS segment (PE file)
Code: [Select]
mov reg,fs:[10h]; constant to taste, would be zero
  • The cycle
Code: [Select]
inc/dec reg; it's a bit long
    jnz $ -1
  • Call a thread function with the parameters (return NULL to EAX)
Code: [Select]
call GetCurrentObject
  • Using the coprocessor
Code: [Select]
fldz
    fistp dword ptr [esp-4]
    mov reg,[esp-4]
  • The following variants of register zeroing are also proposed:
       
    • scan the chain of SEH processing until the winning zero
    • scanning the chain of file files to zero (to do this, first put in the handle of the open file zero ring, and before that go to zero and open this file)
    • reading zero from a random file (requires a random number generator)
    • calculation of the sine from Pi * n (multiply by the FMUL command)
    • Sorting memory and searching for zero as a minimum element
    • the definition of zero as a constant (in the source)
    • create a special macro for zero generation
« Last Edit: August 06, 2018, 12:03:52 AM by Mikl__ »

mineiro

  • Member
  • ***
  • Posts: 431
Re: Hello World of 64 bit programming
« Reply #10 on: August 05, 2018, 10:48:52 PM »
Привет sir Mikl__;
Interesting forms, some unknown to me; I did not get to test with pxor or the like.
Thank you.

Hello sir zedd151;
Yes, xor sounds to be optimized to both, size and processing.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5659
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Hello World of 64 bit programming
« Reply #11 on: August 06, 2018, 12:33:55 AM »
It all does not matter if it has to go into RCX, either SUB RCX, RCX or XOR RCX, RCX is the only economies that work at the minimum but in most cases it just does not matter.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: