nasm未在Windows 8中执行文件 [英] nasm is not executing file in Windows 8
问题描述
最近开始学习Assembly,所以我对此还比较陌生.我们回到学校时就使用Linux,但我想尝试在PC上进行编码.我在Win8.1 64位系统上使用nasm.
Recently started learning Assembly so I'm relatively new to this. We use Linux back at school but I wanted to try coding on my PC. I'm using nasm on a Win8.1 64-bit system.
代码如下:
section .text
global _WinMain@16
_WinMain@16:
mov edx, len
mov ecx, msg
mov ebx, 1
mov eax, 4
ret 16
mov eax, 1
ret 16
section .data
msg db 'Hello, world!', 0xA
len equ $ - msg
nasm似乎正在运行,因为它响应命令,但不执行程序:
nasm seems to be running since it responds to the commands, but it doesn't execute the program:
如何运行该程序?代码是否有问题或系统兼容性?我的最后一招就是安装Ubuntu.
How can I run the program? Is there something wrong with the code or is it system compatibilities? My last resort would be to install Ubuntu.
推荐答案
我将从返回开始.
main.asm
[section] .text
global _main
_main:
mov eax, 6
ret ; returns eax (exits)
该程序所做的只是返回6.
All this program does is return 6.
像这样组装和链接:
c:\Users\James\Desktop>nasm -fwin32 main.asm
c:\Users\James\Desktop>ld -e _main main.obj -o main.exe
c:\Users\James\Desktop>main.exe
c:\Users\James\Desktop>echo %errorlevel%
6
使用堆栈框架.
Using a stack frame.
main.asm
[section] .text
global _main
_main:
push ebp ; set up stack frame
mov ebp,esp
push 6
pop eax; ; mov 6 into eax using the stack
leave ; destroy stack frame
ret ; return eax
编译:
c:\Users\James\Desktop>nasm -fwin32 main.asm
c:\Users\James\Desktop>ld -e _main main.obj -o main.exe
c:\Users\James\Desktop>main
c:\Users\James\Desktop>echo %errorlevel%
6
要使用自己的外部功能,您将:
To use your own external functions you would:
func.asm
[section] .text
global _func
_func:
push ebp ; set up stack frame
mov ebp,esp
mov eax, [ebp+8] ; move argument into eax
add eax, 3 ; eax = eax + 3
leave ; destroy stack frame
ret ; return to caller with (eax + 3)
main.asm
[section] .text
global _main
extern _func
_main:
push ebp ; set up stack frame
mov ebp,esp
push 3 ; push argument onto stack for function
call _func ; calling the function
add esp, 4 ; clean 1 argument
leave ; destroy stack frame
ret ; return what func returned in eax
编译:
c:\Users\James\Desktop>nasm -fwin32 func.asm
c:\Users\James\Desktop>nasm -fwin32 main.asm
c:\Users\James\Desktop>ld -e _main main.obj func.obj -o main.exe
c:\Users\James\Desktop>main
c:\Users\James\Desktop>echo %errorlevel%
6
我非常确定您的代码中是int 80h
,而不是ret 16
.
I'm pretty sure in your code instead of ret 16
, you mean int 80h
.
如果是这样,您将无法像在linux
中那样在windows
中进行系统调用,
但是,例如,您可以使用gcc
链接到c's
库函数; stdio's puts
.
If so, you can't make system calls in windows
like you can in linux
,
but you can use gcc
to link with c's
library functions, for instance; stdio's puts
.
要使用c
或puts
之类的c
库函数,您可以执行以下操作:
To use c
library functions like printf
or puts
you would do:
[section] .text
global _main
extern _puts
_main:
push ebp ; set up stack frame
mov ebp,esp
push helloStr ; push argument onto stack for function
call _puts ; calling the function
add esp, 4 ; clean 1 argument
mov eax, 0
leave ; destroy stack frame
ret ; return 0 (exit)
[section] .data
helloStr db "Hello World!",0
,而不是使用ld
(因为参数很难正确设置),
您可以像在普通c
源文件中一样在obj
文件中使用gcc
.
And instead of using ld
(because the parameters are tough to get right),
you can just use gcc
on the obj
file like you would an ordinary c
source file.
c:\Users\James\Desktop>nasm -fwin32 main.asm
c:\Users\James\Desktop>gcc main.obj -o main.exe
c:\Users\James\Desktop>main
Hello World!
(输入会自动添加新行)
这篇关于nasm未在Windows 8中执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!