How to write hello world in assembler under Windows?

前端 未结 8 907
情书的邮戳
情书的邮戳 2020-11-22 12:01

I wanted to write something basic in assembly under Windows, I\'m using NASM, but I can\'t get anything working.

How to write and compile hello world without the he

8条回答
  •  长情又很酷
    2020-11-22 12:36

    NASM examples.

    Calling libc stdio printf, implementing int main(){ return printf(message); }

    ; ----------------------------------------------------------------------------
    ; helloworld.asm
    ;
    ; This is a Win32 console program that writes "Hello, World" on one line and
    ; then exits.  It needs to be linked with a C library.
    ; ----------------------------------------------------------------------------
    
        global  _main
        extern  _printf
    
        section .text
    _main:
        push    message
        call    _printf
        add     esp, 4
        ret
    message:
        db  'Hello, World', 10, 0
    

    Then run

    nasm -fwin32 helloworld.asm
    gcc helloworld.obj
    a
    

    There's also The Clueless Newbies Guide to Hello World in Nasm without the use of a C library. Then the code would look like this.

    16-bit code with MS-DOS system calls: works in DOS emulators or in 32-bit Windows with NTVDM support. Can't be run "directly" (transparently) under any 64-bit Windows, because an x86-64 kernel can't use vm86 mode.

    org 100h
    mov dx,msg
    mov ah,9
    int 21h
    mov ah,4Ch
    int 21h
    msg db 'Hello, World!',0Dh,0Ah,'$'
    

    Build this into a .com executable so it will be loaded at cs:100h with all segment registers equal to each other (tiny memory model).

    Good luck.

提交回复
热议问题