程序的组成部分
- 数据段(可选)
.section.data - 栈段(可选)
.section.bss - 文本段(必须)
.section.tex
模板
.section .data
< 初始化数据>
.section .bss
<初始化数据>
.section .text
.globl _start
_start:
<指令代码>
创建一个简单的程序
CPUID指令
- EAX 标识获得CPUID的什么信息
- 0 供应商信息
- 1 处理器类型,家族,模式等信息
- 2 处理器缓存配置
- 3 处理器序列号
- 4 缓存配置(线程数量,核心数以及物理特性)
- 5 监控信息
- 80000000h 扩展供应商ID信息和支持的等级
- 80000001h 扩展处理器类型,家族、模式等信息
- 80000002h~80000004h 扩展处理器名称
取得供应商信息
- EBX 存储低4位的字符串
- EDX 存储中4位的字符串
- ECX 存储高四位的字符串
程序实现
#cpuid.s Sample program to extra the processor Vendor ID
.section .data
output:
.ascii "The processor Vendor ID is `xxxxxxxxxxxx' \n"
.section .text
.globl _start
_start:
movl $0, %eax
cpuid
movl $output, %edi
movl %ebx, 28(%edi)
movl %edx, 32(%edi)
movl %ecx, 36(%edi)
movl $4, %eax
movl $1, %ebx
movl $output, %ecx
movl $42, %edx
int $0x80
movl $1, %eax
movl $0, %ebx
int $0x80
说明:
0x80系统调用标识访问控制台输出
* EAX 存储系统调用的值
* EBX 存储要写入的文件描述符
* ECX 存储字符串的起始位置
* EDX 存储字符长度
编译可执行程序
* as -o cpuid.o cpuid.s
* ld -o cpuid cpuid.o
运行程序
./cpuid
调试程序
* 使用gdb
* as -gstabs -o cpuid.o cpuid.s
* ld -o cpuid cpuid.o
* gdb cpuid
* break *_start+1
* run
* 查看数据
* info registers 显示寄存器内容
* print 打印数据的值
* x 显示指定内存的值
使用C库的方法
- 使用printf
#cpuid2.s View the cpuid Vendor ID string using C library calls
.section .data
output:
.asciz "The processor Vendor ID is '%s'\n"
.section .bss
.lcomm buffer, 12
.section .text
.globl _start
_start:
mov $0, %eax
cpuid
movl $buffer, %edi
movl %ebx, (%edi)
movl %edx, 4(%edi)
movl %ecx, 8(%edi)
pushl $buffer
pushl $output
call printf
addl $8, %esp
pushl $0
call exit
- 编译运行
- as -o cpuid2.o cpuid2.s
- ld -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
- ./cpuid2