查看: 84|回复: 0

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

[复制链接]

4

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2008-6-21
发表于 2024-4-10 12:01:00 | 显示全部楼层 |阅读模式

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

以yasm语法为主

注释

;​ 分号之后的所有内容全都是注释, 没有实际作用.

数值

数值必须是 10进制 / 16进制 / 八进制

最终都会被转为16进制的数字, 以0x​开头, 例如 127​ ->0x7f

当使用8进制的时候: 511​ -> 777q

默认基数(基数)为十进制,因此十进制(基数为 10)数字不需要特殊符号

定义常量

表示方法为

<name> equ <value>

例如:

SIZE equ 10000

在组装过程中,常量将替换为其定义的值。

因此,常量没有分配内存位置。

这使得常量更加灵活,因为它没有分配特定的类型/大小(字节、字、双字等)。

这些值受预期用途的范围限制。

数据段

初始化数据必须在“section.data”部分中声明

“section”一词后面必须有一个空格。

所有初始化的变量和常量都放置在段中。

变量名必须以字母开头,后跟字母或数字,包括一些特殊字符(例如下划线、“_”)。

变量定义必须包括变量的名称、数据类型和初始值.

基本格式

<variableName> <dataType> <initialValue>

类型

类型 含义
db 8-bit variable(s)
dw 16-bit variable(s)
dd 32-bit variable(s)
dq 64-bit variable(s)
ddq 128-bit variable(s) → integer
dt 128-bit variable(s) → float

示例​

BSS 段

存储类似实现开辟的数组, 未填充值.

初始化数据必须在“section.bss”部分中声明

“section”一词后面必须有一个空格。

所有初始化的变量和常量都放置在段中。

变量名必须以字母开头,后跟字母或数字,包括一些特殊字符(例如下划线、“_”)。

变量定义必须包括变量的名称、数据类型和初始值.

基本格式

<variableName> <restype> <coudnt>

类型

类型 含义
resb 8-bit variable(s)
resw 16-bit variable(s)
resd 32-bit variable(s)
resq 64-bit variable(s)
resdq 128-bit variable(s) → integer

参考

分配的数组未初始化为任何特定值.

代码段

初始化数据必须在“section.text”部分中声明

“section”一词后面必须有一个空格。

代码段部分将包含一些定义初始程序入口点的标题或标签。

例如,假设一个基本程序使用标准系统链接器,则必须包含以下声明:

global _start
_start:

终止程序不需要特殊的标签或指令。

然而,应该使用系统服务来通知操作系统应该终止程序并恢复和重新利用资源(例如内存)

示例程序

eg.asm

; 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 [bVar1]
    add     al, byte [bVar2]
    mov     byte [bResult], al

; ----------
;  Double-word example
;   dResult = dVar1 + dVar2
    mov     eax, dword [dVar1]
    add     eax, dword [dVar2]
    mov     dword [dResult], eax

; ----------
;  Quadword example
;   qResult = qVar1 + qVar2
    mov     rax, qword [qVar1]
    add     rax, qword [qVar2]
    mov     qword [qResult], rax

; ************************************************************
;  Done, terminate program.

last:
    mov     rax, SYS_exit       ; Call code for exit
    mov     rdi, EXIT_SUCCESS   ; Exit program with success
    syscall


来源:https://www.cnblogs.com/pDJJq/p/18125755/simple-tutorial-of-assembly-language-4-basic-grammar-1em3jm
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部