加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

在32位Windows上使用NASM在程序集中创建exe文件

发布时间:2020-12-14 02:54:57 所属栏目:Windows 来源:网络整理
导读:我正在使用32位 Windows 7上的NASM以汇编语言编写一个hello world程序.我的代码是: 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
我正在使用32位 Windows 7上的NASM以汇编语言编写一个hello world程序.我的代码是:

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:

nasm -f elf hello.asm

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

ld -s -o hello hello.o

但是现在我收到了这个错误:

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

为什么我会收到此错误,我该如何解决?

解决方法

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

extern _printf
global _main

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

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

从文件夹里面打开终端并编译,
首先,使用nasm对象代码:

D:MinGWbinHello> ..nasm -fwin32 main.asm

二,调用gcc链接:

D:MinGWbinHello> ..gcc main.obj -o main.exe

最后,测试一下:

D:MinGWbinHello> main.exe
Hello,world!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读