#cpuid.s simple program to extract the processor Vendor ID
#HOw TO complie and run
#as -o cpuid.o cpuid.s
#ld -o cpuid cpuid.o
.section .data
output:
.ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"
.section .text
.globl _start
_start:
movl $4, %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
这段代码的作用很简单通过汇编指令输出CPU Verdor ID
他是那是怎么实现的呢?
首先在数据段(.data)中声明了一个字符串:
output:
.ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"
字符串元素被预定义并且放在内存中,用output标示。
接下来,程序声明了指令代码和一般的起始标签。
程序要做的第一件事是使EAX寄存器加载零值,零标示输出CPU厂商ID,然后运行CPUID指令。
接下来:
movl $output, %edi
movl %ebx, 28(%edi)
movl %edx, 32(%edi)
movl %ecx, 36(%edi)
第一条指令创建了一个指针,output标签的内存位置会加载到EDI中。
接下来,按照EDI指针。包含CPU ID的相关字符串的寄存器的内容被数据内存的正确位置。
后半部分是调用系统的。
movl $4, %eax #调用的系统函数
movl $1, %ebx #输出目的地,也就是标准输出(当前的会话终端)
movl $output, %ecx #
movl $42, %edx #字符串长度
int $0x80 #Linux内核调用
movl $1, %eax #使用退出函数
movl $0, %ebx #程序退出后,返回给shell的值