汇编语言简易教程(5):环境构建
<h1 id="汇编语言简易教程5环境构建">汇编语言简易教程(5):环境构建</h1><blockquote>
<blockquote>
<p>最近在学习assembly64 时, 需要对程序进行编写 -> 生成汇编代码 -> 调试 -> 执行. 本文聚焦于如果在Windows环境下, 尽可能精简并且完整的构建一个汇编环境.</p>
<p>基于 Windows11, WSL Ubuntu22.04, vscode, 其他的系统/WSL发行版本. 您可以以本文作为简单的参考.</p>
</blockquote>
<h1 id="安装">安装</h1>
<h2 id="wsl">WSL</h2>
<p>你首先需要确保自己有一个WSL的发行版本.</p>
<p>互联网上已经有非常多的类似的文章了, 不再赘述. 您可以参考微软官方文档</p>
<h2 id="wsl环境配置">WSL环境配置</h2>
<p>需要确保安装以下组件:</p>
<ol>
<li>yasm</li>
<li>ld</li>
<li>gdb</li>
</ol>
<pre><code class="language-bash">sudo apt install yasm ddd gcc
</code></pre>
<h2 id="vscode配置">VSCode配置</h2>
<p>需要一些必须的插件</p>
<p>x86 and x86_64 Assembly</p>
<h1 id="使用">使用</h1>
<h2 id="创建asm文件">创建asm文件</h2>
<p>创建<code>eg.asm</code>文件</p>
<pre><code class="language-bash">; Simple example demonstrating basic program format and layout.
; Ed Jorgensen
; July 18, 2014
; ************************************************************
;Some basic data declarations
section .data
EXIT_SUCCESS equ 0 ; successful operation
SYS_exit equ 60 ; call code for terminate
bVar1 db 17
bVar2 db 9
bResult db 0
wVar1 dw 17000
wVar2 dw 9000
wResult dw 0
dVar1 dd 17000000
dVar2 dd 9000000
dResult dd 0
qVar1 dq 170000000
qVar2 dq 90000000
qResult dq 0
section .bss
bArr resb 10
;Code Section
section .text
global _start
_start:
;Performs a series of very basic addition operations
;to demonstrate basic program format.
; ----------
;Byte example
; bResult = bVar1 + bVar2
mov al, byte
add al, byte
mov byte , al
; ----------
;Double-word example
; dResult = dVar1 + dVar2
mov eax, dword
add eax, dword
mov dword , eax
; ----------
;Quadword example
; qResult = qVar1 + qVar2
mov rax, qword
add rax, qword
mov qword , rax
; ************************************************************
;Done, terminate program.
last:
mov rax, SYS_exit ; Call code for exit
mov rdi, EXIT_SUCCESS ; Exit program with success
syscall
</code></pre>
<h2 id="汇编化">汇编化</h2>
<p><code>yasm -g dwarf2 -f elf64 eg.asm -l example.lst</code></p>
<h2 id="链接">链接</h2>
<p><code>ld -g -o eg eg.o</code></p>
<h2 id="运行">运行</h2>
<p><code>./eg</code></p>
<h2 id="调试">调试</h2>
<p>因为目前的WSL支持GUI的显示, 所以可以直接使用ddd进行调试工作.</p>
<p><code>ddd ./eg</code></p>
<h1 id="效果示意">效果示意</h1>
<p><img src="https://img2023.cnblogs.com/blog/3407132/202404/3407132-20240410120248624-2091814901.png"></p>
</blockquote><br><br>
来源:https://www.cnblogs.com/pDJJq/p/18125757/simple-tutorial-of-assembly-language-5-environmental-construction-1o5ahr
頁:
[1]