0

Trying to implement a system-independend socket api in assembler as private project, I wrote the following code to resolve a string address (DNS or direct IP) to a sockaddr record. MSDN told me to use getaddrinfo.

[import getaddrinfo Ws2_32.dll]
[extern getaddrinfo]

[section .data use32 class=data]
  google db 'www.google.com', 0

[section .code use32 class=code]

main:
  push ebp
  mov ebp, esp
  call socket.initialise   ; calls successfully (debugger) WSAStartup 

  push google
  call address.translate

  [...]

; address.translate(string) : address
  address.translate:
    push ebp
    mov ebp, esp
    sub esp, 0x04
  .prepareSystemCall:
    xor eax, eax
    mov dword [ebp-0x04], eax
  .callSystemLookup:
    lea edx, [ebp-0x04]
    push edx
    push 0
    push 0
    push dword [ebp+0x08]
    call [getaddrinfo]    ; never returns
  .return:
    leave
    ret 4

But the call of getaddrinfo never returns, so there is no error code or something else. To find the problem I wrote the same program in c, which works (!?) and looked at the resulting assembler code:

004016DD 83EC 08              sub     esp, 8
004016E0 C745 DC 00000000     mov     [dword ss:ebp-24], 0
004016E7 8D45 DC              lea     eax, [dword ss:ebp-24]
004016EA 894424 0C            mov     [dword ss:esp+C], eax
004016EE C74424 08 00000000   mov     [dword ss:esp+8], 0
004016F6 C74424 04 00000000   mov     [dword ss:esp+4], 0
004016FE C70424 64504000      mov     [dword ss:esp], a.00405064  ;  ASCII "www.google.de"
00401705 E8 12020000          call    <jmp.&ws2_32.getaddrinfo>

It's not equal but at the moment before the call the stack is exactly the same in both examples. Why does getaddrinfo not return?

I'm working on Windows 7 64-bit, using nasm and alink.

Lars M.
  • 179
  • 10
  • Seems to work fine here, although I use `nasm` and `wine` on linux ;) I also noticed your C version seems to have `www.google.de` and the asm version `www.google.com` but I guess both should be equally good. – Jester Apr 11 '14 at 22:04
  • Thanks for this interesting answer, seems to be a local problem... Very strange: on my local machine and one some other 64bit machines it is not working, on some other it works very well, and the c equivalent works everywhere!? Whats that!? – Lars M. Apr 12 '14 at 15:25
  • Problem found: make sure that the direction flag is not set, i.e. call cld before invoking this function if you ever used std in your code. – Lars M. Nov 08 '14 at 14:09
  • Took your time :D Note, that applies in general, the calling convention mandates that DF is clear upon entry to a function. – Jester Nov 08 '14 at 14:20

0 Answers0