跳转至

程序


  • 数据段
  • bss段
  • 文本段

数据段

关键字.section声明段类型

.data,数据段 .bss.text,文本段 .rodata,只读数据段 例如: .section .data

定义变量

类型 说明
.ascii 文本字符串
.asciz 以空字符结束的字符串
.double 双精度浮点数
.float 单精度浮点数
.byte 字节
.short 2个字节整数
.int 4字节整数
.long 32bit整数
.fill
.equ 声明静态数据符号
其他略

变量名+类型

例如:

Text Only
1
2
3
4
5
6
7
output:
  .ascii "helloworld\n"
#数组
array:
  .int 100,200,300
#声明静态符号
.equ PI 3.1415

bss段

无需声明特定的数据类型,只需声名占用内存大小即可

类型 说明
.comm 声明未初始化的数据的通用内存区域
.lcomm 声明未初始化的数据的本地通用内存区域,不能从本地汇编代码之外访问

格式:.comm sysbol, length

Text Only
1
2
3
例如:
.section .bss
.lcomm buffer, 1000

入口

_start 表明程序的入口点 global 声明外部程序可以访问的程序标签

helloworld示例

GAS
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
.section .data
output:
  .asciz "Hello, world!\n"
.section .text
.global _start
_start:
    movl $4, %eax      #系统调用输出
    movl $1, %ebx      #写入文件描述符,1表示标准输出
    movl $output, %ecx #字符串
    movl $42, %edx     #字符串长度
    int $0x80          #软中断,系统调用
    movl $1, %eax      #系统调用返回
    movl $0, %ebx      #返回值为0
    int $0x80          #触发返回
Bash
1
2
3
4
5
#编译
as -o test.o test.s
#链接
ld -o a.out test.o
# gcc编译失败