在32位Windows上与NASM汇编中创建一个exe文件 [英] Create an exe file in assembly with NASM on 32-bit Windows

查看:563
本文介绍了在32位Windows上与NASM汇编中创建一个exe文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用32位 Windows 7 上的 NASM 用汇编语言编写一个hello world程序.我的代码是:

I'm making a hello world program in assembly language with NASM on 32-bit Windows 7. My code is:

section .text 
global main ;must be declared for linker (ld) 
main: ;tells linker entry point 
    mov edx,len ;message length 
    mov ecx,msg ;message to write 
    mov ebx,1 ;file descriptor (stdout) 
    mov eax,4 ;system call number (sys_write) 
    int 0x80 ;call kernel 
    mov eax,1 ;system call number (sys_exit) 
    int 0x80 ;call kernel 

section .data 
    msg db 'Hello, world!', 0xa ;our dear string 
    len equ $ - msg ;length of our dear string

我将此程序另存为 hello.asm .接下来,我创建了 hello.o :

I save this program as hello.asm. Next, I created hello.o with:

nasm -f elf hello.asm 

现在,我正在尝试使用以下命令创建 exe 文件:

Now I'm trying to create the exe file with this command:

ld -s -o hello hello.o 

但是现在我收到此错误:

But now I receive this error:

ld无法识别为内部或外部命令,可操作程序或批处理

ld is not recognized as an internal or external command, operable program or batch

为什么会出现此错误,该如何解决?

Why am I getting this error, and how can I fix it?

推荐答案

下载并安装Mingw.然后将nasm放在Mingw bin文件夹中. 在bin文件夹中创建一个名为Hello的文件夹.在这个资料夹中 使用以下代码创建一个名为main.asm的文件:

Download and install Mingw. Then put nasm in the Mingw bin folder. Create a folder in the bin folder named Hello. In this folder, create a file named main.asm with the following code:

extern _printf
global _main

section .data
msg: db "Hello, world!",10,0

section .text
_main:
    push msg
    call _printf
    add esp,4   
    ret

从文件夹内部打开终端并进行编译, 首先,使用nasm目标代码:

Open the terminal from inside the folder and compile, first, to object code with nasm:

D:\MinGW\bin\Hello> ..\nasm -fwin32 main.asm

第二,致电gcc进行链接:

Second, call gcc to link:

D:\MinGW\bin\Hello> ..\gcc main.obj -o main.exe

最后,对其进行测试:

D:\MinGW\bin\Hello> main.exe
Hello, world!

这篇关于在32位Windows上与NASM汇编中创建一个exe文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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