天鹏变 發表於 2024-4-10 12:01:00

汇编语言简易教程(4):基本语法

<h1 id="汇编语言简易教程4基本语法">汇编语言简易教程(4):基本语法</h1>
<blockquote>
<blockquote>
<p>以yasm语法为主</p>
</blockquote>
<h1 id="注释">注释</h1>
<p>​<code>;</code>​ 分号之后的所有内容全都是注释, 没有实际作用.</p>
<h1 id="数值">数值</h1>
<p>数值必须是 10进制 / <span style="font-weight: bold" class="bold">16进制</span> / 八进制</p>
<p>最终都会被转为16进制的数字, 以<code>0x</code>​开头, 例如 <code>127</code>​ -&gt;<code>0x7f</code>​</p>
<p>当使用8进制的时候: <code>511</code>​ -&gt; <code>777q</code>​</p>
<p>默认基数(基数)为十进制,因此十进制(基数为 10)数字不需要特殊符号</p>
<h1 id="定义常量">定义常量</h1>
<p>表示方法为</p>
<p>​<code>&lt;name&gt;    equ   &lt;value&gt;</code>​</p>
<p>例如:</p>
<p>​<code>SIZE        equ        10000</code>​</p>
<p>在组装过程中,常量将替换为其定义的值。</p>
<p>因此,常量没有分配内存位置。</p>
<p>这使得常量更加灵活,因为它没有分配特定的类型/大小(字节、字、双字等)。</p>
<p>这些值受预期用途的范围限制。</p>
<h1 id="数据段">数据段</h1>
<p>初始化数据必须在“section.data”部分中声明</p>
<p>“section”一词后面必须有一个空格。</p>
<p>所有初始化的变量和常量都放置在段中。</p>
<p>变量名必须以字母开头,后跟字母或数字,包括一些特殊字符(例如下划线、“_”)。</p>
<p>变量定义必须包括变量的名称、数据类型和初始值.</p>
<h2 id="基本格式">基本格式</h2>
<p>​<code>&lt;variableName&gt;    &lt;dataType&gt;   &lt;initialValue&gt;</code>​</p>
<h2 id="类型">类型</h2>
<table>
<thead>
<tr>
<th>类型</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>​<code>db</code>​</td>
<td>8-bit variable(s)</td>
</tr>
<tr>
<td>​<code>dw</code>​</td>
<td>16-bit variable(s)</td>
</tr>
<tr>
<td>​<code>dd</code>​</td>
<td>32-bit variable(s)</td>
</tr>
<tr>
<td>​<code>dq</code>​</td>
<td>64-bit variable(s)</td>
</tr>
<tr>
<td>​<code>ddq</code>​</td>
<td>128-bit variable(s) → integer</td>
</tr>
<tr>
<td>​<code>dt</code>​</td>
<td>128-bit variable(s) → float</td>
</tr>
</tbody>
</table>
<h2 id="示例">示例​</h2>
<p>​<img src="https://img2023.cnblogs.com/blog/3407132/202404/3407132-20240410120142268-55993674.png">​</p>
<h1 id="bss-段">BSS 段</h1>
<blockquote>
<p>存储类似实现开辟的数组, 未填充值.</p>
</blockquote>
<p>初始化数据必须在“section.bss”部分中声明</p>
<p>“section”一词后面必须有一个空格。</p>
<p>所有初始化的变量和常量都放置在段中。</p>
<p>变量名必须以字母开头,后跟字母或数字,包括一些特殊字符(例如下划线、“_”)。</p>
<p>变量定义必须包括变量的名称、数据类型和初始值.</p>
<h2 id="基本格式-1">基本格式</h2>
<p>​<code>&lt;variableName&gt;    &lt;restype&gt;   &lt;coudnt&gt;</code>​</p>
<h2 id="类型-1">类型</h2>
<table>
<thead>
<tr>
<th>类型</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>​<code>resb</code>​</td>
<td>8-bit variable(s)</td>
</tr>
<tr>
<td>​<code>resw</code>​</td>
<td>16-bit variable(s)</td>
</tr>
<tr>
<td>​<code>resd</code>​</td>
<td>32-bit variable(s)</td>
</tr>
<tr>
<td>​<code>resq</code>​</td>
<td>64-bit variable(s)</td>
</tr>
<tr>
<td>​<code>resdq</code>​</td>
<td>128-bit variable(s) → integer</td>
</tr>
</tbody>
</table>
<h2 id="参考">参考</h2>
<p>​<img src="https://img2023.cnblogs.com/blog/3407132/202404/3407132-20240410120142865-1508743310.png">​</p>
<p>分配的数组未初始化为任何特定值.</p>
<h1 id="代码段">代码段</h1>
<p>初始化数据必须在“section.text”部分中声明</p>
<p>“section”一词后面必须有一个空格。</p>
<p>代码段部分将包含一些定义初始程序入口点的标题或标签。</p>
<p>例如,假设一个基本程序使用标准系统链接器,则必须包含以下声明:</p>
<pre><code class="language-yas">global _start
_start:
</code></pre>
<p>终止程序不需要特殊的标签或指令。</p>
<p>然而,应该使用系统服务来通知操作系统应该终止程序并恢复和重新利用资源(例如内存)</p>
<h1 id="示例程序">示例程序</h1>
<p>eg.asm</p>
<pre><code class="language-yas">; 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

;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>
</blockquote><br><br>
来源:https://www.cnblogs.com/pDJJq/p/18125755/simple-tutorial-of-assembly-language-4-basic-grammar-1em3jm
頁: [1]
查看完整版本: 汇编语言简易教程(4):基本语法