Hello world 在 Windows 程序集中使用 nasm [英] Hello world using nasm in windows assembly

查看:26
本文介绍了Hello world 在 Windows 程序集中使用 nasm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 nasm 来编译以下程序集.然而,代码在 Windows 下的控制台中崩溃了.

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

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

C:>ld test.o -o test.exe

C:>ld test.o -o test.exe

section .data
  msg   db    'Hello world!', 0AH
  len   equ   $-msg

section .text
  global _WinMain@16

_WinMain@16:
  mov   edx, len
  mov   ecx, msg
  mov   ebx, 1
  mov   eax, 4
  int   80h

  mov   ebx, 0
  mov   eax, 1
  int   80h

根据此帖子.main 函数在 Windows 下不可用,必须用 WinMain 代替.

According to this post. The main function is not available under Windows and must be replaced by WinMain.

因此,如果您的入口点是 _startmain,则应将其更改为 _WinMain@16 并更改 ret 在程序结束时ret 16:

So if your entry point is _start or main, it should be changed to _WinMain@16 and change the ret at the end of the procedure to ret 16:

我的工作示例:

section .text       
 global _WinMain@16       

_WinMain@16:       
 mov eax, 0       
 ret 16 

推荐答案

最大的问题是你试图在 windows 上使用 Linux 中断!int 80 不适用于 Windows.

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

我们使用的是Assembly,因此您的入口点可以是您想要的任何标签.ld查找的标准入口点是_start,如果要使用其他标签,需要用-e选项告诉ld所以如果你想让你的开始标签成为主要的,那么你需要

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

如果您打算在 Windows 上使用 NASM,我建议您使用 GoLink 作为您的链接器.这是一个简单的 Windows 控制台应用程序:

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

制作文件:

hello: hello.obj
    GoLink.exe  /console /entry GobleyGook hello.obj kernel32.dll  

hello.obj: hello.asm
    nasm -f win32 hello.asm -o hello.obj

这篇关于Hello world 在 Windows 程序集中使用 nasm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆