Search code examples
windowsconsoleyasm

How can I write to console in MS Windows with yasm?


I tried this code (using interruptions), but it is for DOS:

mov eax, 42 ; write 42 to console
mov ecx, 10 ; in decimal
out_int_loop:
    xor edx, edx
    div ecx
    push eax
        add dl, '0' ; one digit 
        mov ah, 2 ; 2 is code for writing character
        int 21h
    pop eax
    cmp eax, 0
    jnz out_int_loop

Solution

  • I used WriteConsoleA function from winapi.

    Also I used GetStdHandle to get stdout.

    To import this functions you need these lines:

    extern  __imp__GetStdHandle@4
    extern  __imp__WriteConsoleA@20