NASM Forum > Programming with NASM

print on windows

(1/2) > >>

Duck:
i am new to nasm and i trying to print a number in windows but i can't  :(
i don't want to use constants or printf


--- Code: --- global _main
    extern  _GetStdHandle@4
    extern  _WriteFile@20
    extern  _ExitProcess@4

    section .text
_main:
    ; DWORD  bytes;   
    mov     ebp, esp
    sub     esp, 4
    mov     DWORD [ebp-4], 10

    push    -11
    call    _GetStdHandle@4
    mov     ebx, eax   

    push    0
    push    DWORD [ebp-4]
    lea     eax, [ebp-8]
    push    eax
    push    (message_end - message)
    push    message
    push    ebx
    call    _WriteFile@20

    push    0
    call    _ExitProcess@4

    hlt
message:
    db      "%d", 10
message_end:

--- End code ---

help me please...
please  :(

debs3759:
I've never programmed for Windows, but don't you need to include a .inc file (equivalent to a C header file) so nasm or your linker know where the routines you are calling are located?

Duck:

--- Quote from: debs3759 on August 27, 2021, 07:00:57 AM ---but don't you need to include a .inc file

--- End quote ---

not that i know, the truth, the following code works without problems


--- Code: --- global _main
    extern  _GetStdHandle@4
    extern  _WriteFile@20
    extern  _ExitProcess@4

    section .text
_main: 
    mov     ebp, esp
    sub     esp, 4

    push    -11
    call    _GetStdHandle@4
    mov     ebx, eax   

    push    0
    lea     eax, [ebp-4]
    push    eax
    push    (message_end - message)
    push    message
    push    ebx
    call    _WriteFile@20

    push    0
    call    _ExitProcess@4

    hlt
message:
    db      'Hello, World', 10
message_end:

--- End code ---

but the problem is that there the "hello word" is declared globally, that is, it is constant, i need it to be reserved in memory at runtime, not as constant if not using mov DWORD [ebp-4], 10

it has been very difficult for me to get info for windows, i know that it is easier and better for linux, but i need it for windows.  :(

fredericopissarra:
Use console API (not WriteFile). hello_win.asm (Windows x64), hello_win32.asm (Windows x86).

fredericopissarra:
If you need to print a string you'll need to pass the size of this string or, at least, use C style strings and put a NULL char at the and to get this size:


--- Code: ---; Entry RDI - pointer to char
; Exit: EAX, size of string
; Destroys RDI.
strlen_:
  xor  eax,eax
.loop:
  cmp  byte [rdi],0
  je   .exit
  inc  eax
  inc  rdi
  jmp  .loop
.exit:
  ret

; Entry ECX = stdout handle. RDX = pointer to string.
; Destroys RDI, R8 and R9.
printstr:
  sub  rsp,40  ; Win64 calling convetion reserves 32 bytes on stack (will reserve 8 bytes more)
 
  mov rdi,rdx
  call  strlen_
  mov r8d,eax
  mov r9,rsp
  push 0
  call  [__imp_WriteConsoleA]

  add  rsp,40
  ret
--- End code ---

Usage:

--- Code: ---section .data

msg: db 'hello', 0

section .text
...
  ; rcx must have the stdout handle
  lea rdx,[msg]
  call printstr
--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version