Author Topic: 64 Bit "Hello, world!" syscall not working  (Read 14401 times)

Offline Cu29p

  • Jr. Member
  • *
  • Posts: 9
64 Bit "Hello, world!" syscall not working
« on: November 20, 2015, 06:00:46 PM »
Can someone explain me why this isn't working?

Quote
SECTION .data

msg   db "Hello, world!"

SECTION .text
   global main
main:

   MOV rax, 1
   MOV rdi, 1
   MOV rsi, msg
   MOV rdx, 13
   syscall

   MOV rax, 60
   MOV rdi, 0
   syscall

Using YASM and VS14 linker:
Code: [Select]
yasm-1.3.0-win64.exe -f x64 source.asm -o source.obj
link.exe source.obj /OUT:source.exe /ENTRY:main /MACHINE:X64 /SUBSYSTEM:console /LARGEADDRESSAWARE:no
Program crashes without output to the console.

You can recommend a simple linker for me if you want

Thanks

Edit: Windows 7, AMD64
« Last Edit: November 20, 2015, 06:07:18 PM by Cu29p »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: 64 Bit "Hello, world!" syscall not working
« Reply #1 on: November 20, 2015, 07:12:53 PM »
Looks like you've got a Linux program and are trying to run it on 'doze. Either graduate to Linux (the source code looks fine but different assembler command line and link it with ld), or find some code for Windows. You'll be looking for something like...
Code: [Select]
extern GetStdHandle
extern WriteFile
extern ExitProcess
global WinMain
; or similar
; I think first parameter in rcx?
; or so
Surely someone can help you with exact instructions... or poke around.

Best,
Frank


Offline Cu29p

  • Jr. Member
  • *
  • Posts: 9
Re: 64 Bit "Hello, world!" syscall not working
« Reply #2 on: November 20, 2015, 07:21:52 PM »
Thank you, but I tried not using anything like that.
If this is linux code there should be something similar for windows :/

Hopefully someone can show me what's going on

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: 64 Bit "Hello, world!" syscall not working
« Reply #3 on: November 20, 2015, 08:12:22 PM »
Quote
If this is linux code there should be something similar for windows :/

I think you'll find that this is not true. If you find that I'm mistaken, please post!

Best,
Frank


Offline Cu29p

  • Jr. Member
  • *
  • Posts: 9
Re: 64 Bit "Hello, world!" syscall not working
« Reply #4 on: November 20, 2015, 09:23:11 PM »
You are right it doesn't work on windows

Now I got this:
Code: [Select]
extern ExitProcess
extern WriteFile
extern GetStdHandle

SECTION .data

msg db "Hello, world!"

SECTION .text
global main
main:

MOV rcx, -11
CALL GetStdHandle

MOV rcx, rax
MOV rdx, msg
MOV r8, 13
MOV r9, 0
PUSH 0
CALL WriteFile

MOV rcx, 0
CALL ExitProcess

Nothing is happening on the console.
This should be pretty close, but I think the "PUSH 0" isn't doing the correct job.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: 64 Bit "Hello, world!" syscall not working
« Reply #5 on: November 20, 2015, 09:44:55 PM »
I get the impression from here....

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365747(v=vs.85).aspx

that r9 and the pushed parameter can't both be zero. In Linux, the number of bytes actually written is returned in eax/rax. In 'doze, I think there's a variable whose address is given as a parameter where the function puts the number of bytes written. Try creating such a variable (resq 1 ?) and putting its address in r9. Maybe then push 0 will work?

Courage!

Best,
Frank


Offline Cu29p

  • Jr. Member
  • *
  • Posts: 9
Re: 64 Bit "Hello, world!" syscall not working
« Reply #6 on: November 20, 2015, 10:19:37 PM »
Thank you

I think I did what you meant, still not working though
and now it's crashing again..

Code: [Select]
extern ExitProcess
extern WriteFile
extern GetStdHandle

SECTION .bss

wrtn resq 1

SECTION .data

msg db "Hello, world!"

SECTION .text

main:

MOV rcx, -11
CALL GetStdHandle

MOV rcx, rax
MOV rdx, msg
MOV r8, 13
MOV r9, wrtn
PUSH 0
CALL WriteFile

MOV rcx, 0
CALL ExitProcess

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: 64 Bit "Hello, world!" syscall not working
« Reply #7 on: November 20, 2015, 10:40:06 PM »
That's exactly what I meant. Sorry to hear it didn't work! Possible that you need to adjust the stack? I'm over my head in 64-bit code, and doubly over my head in Windows 64-bit code! I hope that in a while one of our many Windows users will wake up and give you a hand.

Best,
Frank


Offline Cu29p

  • Jr. Member
  • *
  • Posts: 9
Re: 64 Bit "Hello, world!" syscall not working
« Reply #8 on: November 21, 2015, 12:43:46 PM »
So I got this now:

Code: [Select]
extern ExitProcess
extern WriteConsoleA
extern GetStdHandle
extern ReadFile

SECTION .bss

wrtn resq 1

SECTION .data

msg db "Hello, world!"

SECTION .text

main:
SUB rsp, 0x28

MOV rcx, -11
CALL GetStdHandle

MOV rcx, rax
MOV rdx, msg
MOV r8, 13
MOV r9, wrtn
MOV qword [rsp+0x20], 0
CALL WriteConsoleA

MOV rcx, 0
CALL ExitProcess
ADD rsp, 0x28
RET

And it looks pretty good to me, but still nothing is happening.
I can't believe no one made a "Hello, world" on x64 Windows without using printf.
I hope someone can still help me, because google can't

Offline shaynox

  • Full Member
  • **
  • Posts: 118
  • Country: gr
Re: 64 Bit "Hello, world!" syscall not working
« Reply #9 on: November 21, 2015, 12:54:33 PM »
>>>I hope someone can still help me, because google can't<<<

Try Bing instead x)

Else try to use a program that use puts/printf and that work, then replace it by your function
http://pastebin.com/zsw2v5b3

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: 64 Bit "Hello, world!" syscall not working
« Reply #10 on: November 21, 2015, 02:15:32 PM »
Great idea! First, pretend that the Windows API doesn't exist and use the C library. Then we can pretend assembly language doesn't exist and do it in C. Then for a final exam we can pretend C doesn't exist and just do:
Code: [Select]
echo hello world

Or... we can be persistant like Cu29p is doing and learn how to do 64-bit Windows. Google finds this:
https://software.intel.com/en-us/articles/introduction-to-x64-assembly
which has a MessageBoxA example... in Masm syntax, to be sure, but it's a start.

I'll look some more. I'm embarrased!

Best,
Frank


Offline shaynox

  • Full Member
  • **
  • Posts: 118
  • Country: gr
Re: 64 Bit "Hello, world!" syscall not working
« Reply #11 on: November 21, 2015, 02:43:32 PM »
What is C library, apart simple functions into huge libraries deliver with Windows OS ?

It's so stupid to call this library: C belonging, because it just function "low" level that is use by every program that run on Windows, even if they are written into higher language than C.

And by the way I said:
>>> Else try to use a program that use puts/printf and that work, then replace it by your function

And the functions still the functions, no language is link with any function, it's only the ABI (and macro) that is linked and in asm, we can write multiple ABI for be compatible with the function used.
« Last Edit: November 21, 2015, 03:15:01 PM by shaynox »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: 64 Bit "Hello, world!" syscall not working
« Reply #12 on: November 21, 2015, 04:26:41 PM »
Expletive deleted! I'm beginning to think this is impossible. I can find examples for 64-bit Linux, BSD, MACOS, printf examples for Windows MessageBoxA - everything but Writefile (or WriteConsole). About the closest I can come is this:
http://www.asmcommunity.net/forums/topic/?id=30464
... and I think it's about the same thing you've got. Take a look and see if it's any help at all. I thought I was going to have to send you over to Stack Overflow, but it's the same deal there - ask for Win64, get DOS. I really don't think there's any advantage to using the 64-bit API over the 32-bit API, but you ought to be able to do it! Expletive Deleted! I'm going back to drooling by the fire...

Best,
Frank


Offline Cu29p

  • Jr. Member
  • *
  • Posts: 9
Re: 64 Bit "Hello, world!" syscall not working
« Reply #13 on: November 21, 2015, 04:50:12 PM »
The link wasn't new for me :D
Thank you for your effort, I will switch to Win32 if no one else can help :/
It really bothers me that this is so hard...

Offline Cu29p

  • Jr. Member
  • *
  • Posts: 9
Re: 64 Bit "Hello, world!" syscall not working
« Reply #14 on: November 21, 2015, 10:03:15 PM »
So it was my fault, I switched to GoLink and used
Code: [Select]
"GoLink.exe source.obj /entry main /largeaddressaware /ni Kernel32.dll User32.dll" instead of
Code: [Select]
"GoLink.exe source.obj /entry main /console /ni Kernel32.dll User32.dll"