Hello world using nasm in windows assembly

后端 未结 2 1598
轻奢々
轻奢々 2020-12-01 01:34

I\'m using nasm to compile the following assembly. However the code crashes in the console under Windows.

C:\\>nasm -f win32 test.asm -o test.o

相关标签:
2条回答
  • 2020-12-01 02:10

    The biggest problem is that you are trying to use Linux interupts on windows! int 80 will NOT work on windows.

    We are using Assembly, so your entry point can be ANY label you want. The standard entry point that ld looks for is _start, if you want to use another label, you need to tell ld with the -e option So if you want your start label to be main, then you need

    global main
    ld -e main test.o -o test.exe
    

    If you are going to use NASM on Windows, I will recommend using GoLink as your linker. Here is a simple windows console app:

    STD_OUTPUT_HANDLE   equ -11
    NULL                equ 0
    
    global GobleyGook
    extern ExitProcess, GetStdHandle, WriteConsoleA
    
    section .data
    msg                 db "Hello World!", 13, 10, 0
    msg.len             equ $ - msg
    
    section .bss
    dummy               resd 1
    
    section .text
    GobleyGook:
        push    STD_OUTPUT_HANDLE
        call    GetStdHandle
    
        push    NULL
        push    dummy
        push    msg.len
        push    msg
        push    eax
        call    WriteConsoleA 
    
        push    NULL
        call    ExitProcess
    

    makefile:

    hello: hello.obj
        GoLink.exe  /console /entry GobleyGook hello.obj kernel32.dll  
    
    hello.obj: hello.asm
        nasm -f win32 hello.asm -o hello.obj
    
    0 讨论(0)
  • 2020-12-01 02:14

    Although, this same program probably will run in WINE on Linux like a charm. :)

    WINE doesn't prevent using Linux system calls from inside Windows PE binaries; the machine instructions run natively and WINE only provides DLL functions.

    0 讨论(0)
提交回复
热议问题