汇编语言脚本:cpuid

简介: 最近在拜读Richard Blum的汇编语言程序设计,记录下学习中的脚本,以及遇到的问题和处理过程

版本1:

#cpuid.s Sample program to extract the processor Vendor ID
  .section .data
output:
  .ascii "The processor Vendor ID is 'xxxxxxxxxxxxxxxxx'\n"
  .section .text
  .global _start
_start:
  mov $0,%eax
  cpuid
  mov $output,%edi
  mov %ebx,28(%edi)
  mov %edx,32(%edi)
  mov %ecx,36(%edi)
  mov $4,%eax
  mov $1,%ebx
  mov $output,%ecx
  mov $42,%edx
  int $0x80
  mov $1,%eax
  mov $0,%ebx
  int $0x80

使用as编译,ld连接:

as -o cpuid.o cpuid.s
ld -o cpuid cpuid.o
./cpuid

图片.png

使用GCC进行汇编,需要修改脚本:将start换成main

#cpuid.s Sample program to extract the processor Vendor ID
  .section .data
output:
  .ascii "The processor Vendor ID is 'xxxxxxxxxxxxxxxxx'\n"
  .section .text
  .global main
main:
  mov $0,%eax
  cpuid
  mov $output,%edi
  mov %ebx,28(%edi)
  mov %edx,32(%edi)
  mov %ecx,36(%edi)
  mov $4,%eax
  mov $1,%ebx
  mov $output,%ecx
  mov $42,%edx
  int $0x80
  mov $1,%eax
  mov $0,%ebx
  int $0x80

使用gcc汇编:

gcc -o cpuid cpuid2.s

但是会报错,错误信息如下:relocation R_X86_64_32 against `.data' can not be used when making a PIE object; recompile with -fPIE


解决方法是:增加-no-pie选项

gcc -o cpuid cpuid2.s -no-pie

图片.png

连接C库函数

#cpuid.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
.global _start
_start:
  mov $0,%eax
  cpuid
  mov $buffer,%edi
  mov %ebx,(%edi)
  mov %edx,4(%edi)
  mov %ecx,8(%edi)
  push $buffer
  push $output
  call printf
  add $8,%esp
  push $0
  call exit

编译:

as -o cpuid3.o cpuid3.s

连接:

ld -dynamic-linker /lib/ld-linux.so.2 -o cpuid3 -lc cpuid3.o

执行报错,现阶段还不是太熟悉汇编语言,留待后续再解决

图片.png

目录
相关文章
|
10月前
|
存储 程序员 C语言
在编写C语言程序时
在编写C语言程序时
67 0
|
1月前
|
Ubuntu Shell Linux
Linux命令行解释器的模拟实现
Linux命令行解释器的模拟实现
|
10月前
|
存储 Unix 编译器
汇编语言----X86汇编指令
汇编语言----X86汇编指令
421 2
|
10月前
|
存储 机器学习/深度学习 移动开发
汇编语言指令系列
汇编语言指令系列
1742 0
|
9月前
|
数据处理 调度 数据格式
源程序的编程理解是怎样的
从编译程序(或抽象)的视角理解源程序
|
10月前
|
存储 缓存 Linux
C语言编译过程——预处理、编译汇编和链接详解
C语言编译过程——预处理、编译汇编和链接详解
|
编译器 程序员 Linux
C语言——可执行程序过程
C语言——可执行程序过程
|
编译器 C语言 数据安全/隐私保护
汇编语言和本地代码及通过编译器输出汇编语言的源代码
汇编语言和本地代码及通过编译器输出汇编语言的源代码
142 0
|
存储 JavaScript
汇编语言的所有指令
汇编语言的所有指令
|
编译器 C语言 C++
Win知识 - 程序是怎样跑起来的——通过编译器输出汇编语言的源代码
Win知识 - 程序是怎样跑起来的——通过编译器输出汇编语言的源代码
241 0

相关实验场景

更多