Win10系统下用汇编代码输出Hello World
其他
2020-04-01 12:44:48
阅读次数: 0
Win10系统下用汇编代码输出Hello World
一、准备
- 在写代码之前把相关环境准备好,这里要用到
MingW, NASM
和 Visual Studio 的 link
程序,以及 Windows kit 的 kernel32.lib
文件,一一准备好。
1、Mingw
- 首先是
MingW
的下载、安装配置。
- 下载地址:MingW
- 选择图片上圈着的那个,下载下来后解压到适当路径,
- 解压后如下,现在打开 bin 目录并复制路径,去到环境变量设置中的 path 下添加 MingW 的 bin 目录。
- 设置好后,打开命令窗口输入:
gcc -v
测试环境是否配置好,配置好的情况下会输出如下信息:λ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=D:/Program\ Files/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-sjlj-rt_v6-rev0/mingw64 --enable-shared --enable-static --enable-targets=all --enable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --enable-sjlj-exceptions --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch-32=i686 --with-arch-64=nocona --with-tune-32=generic --with-tune-64=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-sjlj-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-sjlj-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-sjlj-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-sjlj-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-sjlj-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 8.1.0 (x86_64-posix-sjlj-rev0, Built by MinGW-W64 project)
- 其实最关键的也就最后一句话。
2、NASM
3、LINK 配置
- 这里的 LINK 程序主要是 Visual Studio 提供的,我用的是 2019,在软件目录的
\Visual Studio 2019\VC\Tools\MSVC\14.24.28314\bin\Hostx86\x86
下,同样的复制目录并配置好环境变量。
4、工作目录
- 新建一个文件夹,用于存放待会编译过程中生成的文件和相关的 lib 文件,这里主要是
kernel32.lib
文件,该文件通常位于 Windows Kit 目录下。
二、编写汇编代码
- 在工作目录下新建一个
.asm
文件,可以通过生成文本文件再改后缀的形式生成,用记事本打开,并编辑如下代码:; hello.asm
STD_OUTPUT_HANDLE equ -11
NULL equ 0
global GobleyGook
extern _ExitProcess@4, _GetStdHandle@4, _WriteConsoleA@20
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@4
push NULL
push dummy
push msg.len
push msg
push eax
call _WriteConsoleA@20
push NULL
call _ExitProcess@4
- 将文件命名为
hello.asm
文件。这份文件待会用 LINK 链接,为了区分开来,再创建一个 helloworld.asm
文件,代码如下:SECTION .data
msg db 'Hello World!', 0Ah
SECTION .text
global _start
_start:
mov edx, 13
mov ecx, msg
mov ebx, 1
mov eax, 4
int 80h
mov ebx, 0 ; return 0 status on exit - 'No Errors'
mov eax, 1 ; invoke SYS_EXIT (kernel opcode 1)
int 80h
三、汇编链接执行
1、使用 NASM 汇编
- 首先就是要将汇编文件汇编成目标文件,打开 CMD 窗口并 cd 到工作目录,使用命令:
nasm -f win32 hello.asm -o hello.obj
,该命令的意义就是将一个 .asm
文件汇编生成一个 .obj
文件,在没有出错的情况下,工作目录会生成一个 heelo.obj
文件。
- 接着用
nasm -fwin32 helloworld.asm
生成 .o
文件,这是待会用 ld 程序链接要用到的。
2、使用 LINK 或 MingW 的 ld 链接
- 先使用 LINK 链接 hello.obj,使用命令:
link /subsystem:console /nodefaultlib /entry:GobleyGook hello.obj kernel32.lib
- 回车运行,无错误的情况下,工作目录下会生成一个
hello.exe
,在命令行输入 hello
回车控制台将打印出 Hello World!
。
- 接着用 Mingw 提供的工具,使用命令:
ld -m i386pe helloworld.obj -o helloworld.exe
,命令无错误,目录下也生成了对应的可执行文件,奇怪的是,执行可执行文件,控制台并不会输出任何东西,只是一闪而过,可能 windows 系统不适合这种方式。
上一篇
下一篇
发布了184 篇原创文章 ·
获赞 24 ·
访问量 2万+
转载自blog.csdn.net/qq_42896653/article/details/105012129