The MASM Forum

64 bit assembler => 64 bit assembler. Conceptual Issues => Topic started by: grimoire on August 05, 2013, 06:49:07 AM

Title: problem with socket and masm 64bits
Post by: grimoire on August 05, 2013, 06:49:07 AM
I tried to make a "socket" with masm, I used Visual Studio 2012 because is the IDE for C/C++ and asm

I made a code with nasm and MinGW  for 32 bits, but I tried to do the same code but this time for 64 bits with masm and it doesn't work.

Somebody can explian me how use a call in masm for 64 bits, because normally write something like this.

mov [dir], rax
call [dir]

and I can't use it in masm

this is my code, i hope somebody can explian me how use call, please.

Code (asm) Select


extrn LoadLibraryA:PROC
extrn ExitProcess:PROC
extrn GetProcAddress:PROC

.data

wstart db 400 dup (0)

wsdll db 'ws2_32.dll',0
wsaddr dq ?
WStp db 'WSAStartup',0
wstaddr dq ?
soc db 'socket',0
socaddr dq ?
conn db 'connect',0
conaddr dq ?
sen db 'send',0
senaddr dq ?

sre db "hola mundo",0
error db "%d", 10,0



.code
Start proc
push rbp
mov rbp, rsp

mov rdx, offset[wsdll]
call LoadLibraryA
mov[wsaddr], rax

mov r8, offset[WStp]
mov r9, [wsaddr]
call GetProcAddress
mov[wstaddr], rax

push qword ptr[wstart]
push 2
call [wstaddr]

mov r8, offset[soc]
mov r9, offset[wsaddr]
call GetProcAddress
mov[wstaddr], rax

push 0
push 1
push 2
call [wstaddr]
mov[socaddr], rax

mov r8, offset[conn]
mov r9, offset[wsaddr]
call GetProcAddress
mov[conaddr], rax

push 16
xor rax, rax
mov rax, 0100007f5c110002h
push rax
push qword ptr[socaddr]
call [conaddr]

mov r8, offset[sen]
mov r9, offset[wsaddr]
call GetProcAddress
mov[senaddr], rax

push 0
push 512
mov rcx, offset [sre]
push rcx
push qword ptr[socaddr]
call [senaddr]

xor ecx, ecx
call ExitProcess

mov rsp, rbp
pop rbp

Start endp

End
Title: Re: problem with socket and masm 64bits
Post by: japheth on August 05, 2013, 08:15:42 PM
Quote from: grimoire on August 05, 2013, 06:49:07 AM
mov [dir], rax
call [dir]

Looks quite good - 64-bit Masm will accept this.

However, the rest of your code looks messy. It won't work; you'll probably have to make yourself more familiar with the Win64 ABI.

For example, your code:


push 0
push 512
mov rcx, offset [sre]
push rcx
push qword ptr[socaddr]
call [senaddr]


"should" instead be written like this:


mov r9d, 0
mov r8d, 512
mov rdx, offset [sre]
mov ecx, [socaddr]
call [senaddr]

Title: Re: problem with socket and masm 64bits
Post by: wjr on August 06, 2013, 12:57:14 AM
Also, although registers are used to pass the first 4 parameters, there still is stack-backing for those registers. If you are not using more than 4 parameters, and not throwing the stack off when at a call, this will work...


.code
Start proc
push rbp
mov rbp, rsp
sub rsp,20h ;shadow space for register parameters
Title: Re: problem with socket and masm 64bits
Post by: grimoire on August 08, 2013, 05:23:28 AM
ok thank you