问题描述
我正在尝试在 Windows 上设置程序集编程环境.我确实安装了 Linux,但出于好奇和学习 WSL 的目的,我试图让它在 Windows 10 上通过 WSL2 运行.
I'm trying to set up an Assembly Programming Environment on Windows.I do have Linux installed, but for curiosity sake, and also studying purposes for learning WSL, I'm trying to make it run through WSL2 on Windows 10.
我尝试设置 Alpine 和 Ubuntu(基础),只安装以下工具:
I've tried to setup both Alpine and Ubuntu (base) installing only the following tools:
bash bash-doc bash-completion
util-linux pciutils usbutils coreutils binutils findutils grep
它是通过 apk add
或 apt install
安装的.
It was installed with either apk add
or apt install
.
然后我从Programming Ground Up中编写了以下示例:
Then I've wrote the following example from Programming Ground Up:
.section .data
.section .text
.globl _start
_start:
movl $1, %eax
movl $0, %ebx
int $0x80
我进行了编译和链接,没有任何花哨的标志,就像书中例子中的那样.
I did the compilation and link without any fancy flags, just the minimum like in the book example.
as exit.s -o exit.o
ld exit.o -o exit
我知道像 int $0x80
这样的调用取决于架构、操作系统等.
I know that those calls like int $0x80
depends on architecture, operation system, etc.
当我尝试在 WSL 图像上运行它时,它返回 Segmentation fault
.
It's returning Segmentation fault
when I try to run it on the WSL image.
环顾四周,我知道分段错误可能与程序未退出执行有关,例如当某个部分没有 ret
语句时.(在这种情况下,int $0x80
应该足以激活内核并通过调用 ax 寄存器上设置的值 1 返回/退出)
Searching around I've got to know that Segmentation fault is probably regarding the program not exiting the execution, like when a section doesn't have a ret
statement.(In this case, int $0x80
should be enough for activating the kernel and returning/exiting by calling the value 1 set on ax register)
我以为是因为我试图在 64 位机器上编译和运行 32 位 ASM 代码.但是,我通过 ssh 连接到 pub unix 服务器,它也是 Ubuntu x86_64,并且相同的代码可以毫无问题地运行.
I thought it was because I'm trying to compile and run a 32bit ASM code in a 64bit machine.But, I did a ssh connection to a pub unix server, which is also a Ubuntu x86_64, and the same code could run without any problem.
这是 WSL2 限制吗?或者这个pubnix是否安装或配置了一些东西来接受32位代码而没有任何额外的标志?
Is it a WSL2 limitation?Or do this pubnix have something installed or configured for it to accept the 32bit code without any additional flag?
推荐答案
WSL2 确实支持 32 位库和系统调用.
The WSL2 do have support to 32-bit libraries and system calls.
因此,这是在 Windows 上进行程序集编程的好方法,无需双启动和程序集模拟器.
So it's a nice way of programming assembly on Windows without the need for dual-boot and assembly emulators.
但是假设既然 WSL2 已经安装,它也在被使用是错误的.
But it is a mistake to assume that since WSL2 is installed, it's also being used.
- 如果您没有安装 WSL2,则需要安装它.
如果您已经安装了 WSL2,您可以将 WSL1 图像转换为 WSL2,或者将 WSL 版本设置为您希望在创建/导入下一个图像时使用的版本 (WSL2).
If you already have WSL2 installed, you can either convert a WSL1 image to WSL2, or set the WSL version to the version you want (WSL2) to be used when creating/importing your next image.
(就我而言,Docker Desktop for Windows 自动为我安装了 WSL2)
(In my case, Docker Desktop for Windows automatically installed WSL2 for me)
- 您可以通过以下方式检查您的图像是 WSL1 还是 WSL2:
wsl -l -v
- 您可以通过设置要使用的 WSL2 来创建 WSL2 图像.
wsl --set-default-version 2
每次创建新图像或导入时,都会将其设置为 WSL2.如果你想回到 WSL1,你可以wsl --set-default-version 1
.
Everytime you create a new image or import, it will be set as WSL2.If you want to go back to WSL1 you can wsl --set-default-version 1
.
现在您可以照常创建新图像了.
Now you can create a new image as usual.
- 如果您的图像已经是版本 1,您可以使用以下命令将其转换为版本 2:
wsl --set-version image_name 2
(转换图片可能需要很长时间)
(Converting an image may take a long time)
- 相反,您可以导出当前图像,然后将其重新导入/创建为 WSL2 版本,以便在需要时保留两个版本.
wsl --export current-image image-backup.tar
mkdir new-folder
wsl --import newimg new-folder image-backup.tar
(导出和导入将保留您的 $HOME 和文件,就像备份整个图像一样)
(Exporting and Importing will keep your $HOME and files, it's like making a backup of the entire image)
这篇关于(WSL2) x86_64 Unix 中的编程汇编不能进行 32 位系统调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!