我如何在 Linux 子系统中的 VSCode 上运行程序集 (.s) 文件 [英] How do i run assembly (.s) files on VSCode in Linux subsystem
问题描述
我刚刚接触 ARM 编程.学习了一些基础知识,但在运行代码时遇到问题.我在 Linux 子系统上使用 VSCode 作为我的 IDE.
I have just got into ARM programming. Learned a few basics but am having issues with running code. I use VSCode on Linux Subsystem as my IDE.
我的计算机上没有安装任何东西,我想运行 ARM 代码.我在网上阅读了一些关于qemu"的内容.和内核"和东西,但我不确定它们的意思.如果有人针对此类问题提供详细的演练,那就太好了.我没有树莓派.
I have nothing installed on my computer and i would like to run ARM code. I have read online something about "qemu" and "kernel" and stuff, but am not sure what they mean. It would be great if someone provides a detailed walkthrough for such a problem. I do not have a raspberry pi.
例如,我如何在 VSCode 上运行以下 division.s
文件?
For example, how do i run the following division.s
file on VSCode?
.global _start
_start:
MOV R1, #X
MOV R2, #Y
MOV R3, #Z
CMP R1, R2 @ is x>y ?
BGT _tryx
CMP R2, R3 @ is y>z ?
BGT _isy
MOV R4, R3
B _exit
_isy:
MOV R4, R2
B _exit
_tryx:
CMP R1, R3 @ is x>z ?
BGT _isx
MOV R4, R3
B _exit
_isx:
MOV R4, R1
_exit:
MOV R0, R4
MOV R7, #1
SWI 0
.data
.equ X, 3
.equ Y, 5
.equ Z, 4
我需要安装任何扩展吗?有什么我需要下载的吗?我使用 gcc
来编译 C 代码.这里也可以用吗?
Are there any extensions i need to install? Is there anything i need to download? I have used gcc
to compile C code. Can it be used here too?
提前谢谢!:D
推荐答案
您的问题相当广泛.话虽如此,可以使用以下过程在 WSL 中执行稍微修改过的程序版本:
Your question is rather a broad one. This being said, a slightly modified version of your program can be executed in WSL using the following procedure:
sudo apt-get install qemu-user
sudo mkdir -p /opt/arm/10
wget 'https://developer.arm.com/-/media/Files/downloads/gnu-a/10.2-2020.11/binrel/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz?revision=d0b90559-3960-4e4b-9297-7ddbc3e52783&la=en&hash=985078B758BC782BC338DB947347107FBCF8EF6B' -O gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz
sudo tar Jxf gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz -C /opt/arm/10
/tmp/division.s:
/tmp/division.s:
@ count how often we can take Y from X
.global main
main:
MOV R1, #X
MOV R2, #Y
MOV R3, #0 @ Q
_loop:
CMP R1, R2
BLT _exit
SUB R1, R2
ADD R3, #1
B _loop
_exit:
MOV R0, R3
MOV R7, #1
SWI 0
.data
.equ X, 23
.equ Y, 4
编译:
/opt/arm/10/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc -static -o /tmp/division /tmp/division.s
正在执行 - WSL:
Executing - WSL:
qemu-arm /tmp/division
echo $?
5
这是预期的结果,因为 23 div 4 是 5.
Which is the expected result, since 23 div 4 is 5.
正在执行 - Windows 10:
Executing - Windows 10:
C:\>c:\Windows\System32\bash -c "qemu-arm /tmp/division; echo $?"
5
C:\>
或者:
C:\>c:\Windows\System32\bash -c "qemu-arm /tmp/division"
C:\>echo %ERRORLEVEL%
5
请注意,通过下载/安装 gcc-arm-10.2-2020.11-mingw-w64-i686-arm-none-linux-gnueabihf.tar.xz,可能也已在 Windows 10 中编译了 Division.s/code> 而不是
gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz
- 你的选择.
Note that division.s may have been compiled in Windows 10 as well by downloading/installing gcc-arm-10.2-2020.11-mingw-w64-i686-arm-none-linux-gnueabihf.tar.xz
instead of gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz
- your choice.
我让你知道使用上述信息从 vscode
运行你的程序的细节,恕我直言,你的问题有点太宽泛了.
I let it to you than to go into the details of using the information above for running your program from vscode
, you question being a bit too broad IMHO.
更新:division.s 是静态编译的,目的是避免为任何不需要的动态库指定位置.
Update: division.s was compiled statically on purpose for avoiding having to specify the locations for any non-needed dynamic libraries.
在不使用-static
选项的情况下编译它并执行它会导致显示以下错误消息:
Compiling it without using the -static
option, and executing it would result in the following error message to be displayed:
qemu-arm division
/lib/ld-linux-armhf.so.3: No such file or directory
可以通过告诉 qemu-arm
在哪里寻找所需的动态库来避免,在这种情况下是 /lib/ld-linux-armhf.so.3
:
It can be avoided by telling qemu-arm
where to look for required dynamic libraries, /lib/ld-linux-armhf.so.3
in this case:
qemu-arm -L /opt/arm/10/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc division
echo $?
5
这篇关于我如何在 Linux 子系统中的 VSCode 上运行程序集 (.s) 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!