Home > OS >  How to call Windows API in x64 MASM program
How to call Windows API in x64 MASM program

Time:01-03

I am using MASM in x64 Windows operating system. I wanted to call Windows APIs and then output some strings to the screen.

I am learning x64 assembly programming in Windows operating system. Today I wanted to print some result to the console. However, unlike linux operating system, in windows we can use syscalls directly without any pain. I've written the following program to print a hello world message to the console but it doesn't show anything. I couldn't figure out what is wrong with this code.


GetStdHandle PROTO
ExitProcess PROTO
WriteConsoleA PROTO

.data
    message         DB "Hello World",0
    message_size    DW SIZEOF message

.code
    main PROC
        SUB RSP, 5 * 8  
        
        MOV RCX, -11           
        CALL GetStdHandle

        MOV RCX, RAX     
        LEA RDX, message
        MOV R8, SIZEOF message - 1
        LEA R9, message_size  
        MOV  QWORD PTR [RSP   4 * SIZEOF QWORD], 0
        CALL WriteConsoleA

        MOV RCX, 0      
        CALL ExitProcess
    main ENDP
END

CodePudding user response:

Are you linking it as a console app? Windows distinguishes between console apps that write to stdout (which is what yours is doing) and GUI apps that create windows and run a message loop.

You need to specify /subsystem:console as a linker flag. There might be a MASM option/directive that does the same.

CodePudding user response:

The code works fine after enter image description here

  • Related