[ATT汇编]程序举例:xxx.S 编译、链接、运行、调试 (C
2019-05-23 本文已影响0人
AkuRinbu
使用教材
《汇编语言程序设计》
https://www.jianshu.com/p/8473cd0e92b6
第4章 汇编语言程序范例
汇编程序举例 cpuid.s
- 汇编源码:
cpuid.s
#cpuid.s Sample program to extract the processor Vendor ID
.section .data
output:
.ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"
.section .text
.globl _start
_start:
nop
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
- 编译、链接、运行、调试
[anno@localhost ATT]$ as -gstabs -o cpuid.o cpuid.s
[anno@localhost ATT]$ ld -o cpuid cpuid.o
[anno@localhost ATT]$ ./cpuid
The processor Vendor ID is 'GenuineIntel'
[anno@localhost ATT]$ gdb cpuid
(gdb) break *_start+1
Breakpoint 1 at 0x4000b1: file cpuid.s, line 9.
(gdb) run
Starting program: /home/anno/Desktop/ATT/cpuid
Breakpoint 1, _start () at cpuid.s:9
9 movl $0, %eax
(gdb) next
10 cpuid
(gdb) next
11 movl $output, %edi
(gdb) step
12 movl %ebx, 28(%edi)
(gdb) step
13 movl %edx, 32(%edi)
(gdb) s
14 movl %ecx, 36(%edi)
(gdb) info registers
(gdb) s
15 movl $4, %eax
(gdb) info registers
(gdb) print/x $ebx
$1 = 0x756e6547
(gdb) print/x $ecx
$2 = 0x6c65746e
(gdb) print/x $edx
$3 = 0x49656e69
(gdb) x/42cb &output
0x6000ec <output>: 84 'T' 104 'h' 101 'e' 32 ' ' 112 'p' 114 'r' 111 'o'99 'c'
0x6000f4 <output+8>: 101 'e' 115 's' 115 's' 111 'o' 114 'r' 32 ' ' 86 'V' 101 'e'
0x6000fc <output+16>: 110 'n' 100 'd' 111 'o' 114 'r' 32 ' ' 73 'I' 68 'D' 32 ' '
0x600104 <output+24>: 105 'i' 115 's' 32 ' ' 39 '\'' 71 'G' 101 'e' 110 'n'117 'u'
0x60010c <output+32>: 105 'i' 110 'n' 101 'e' 73 'I' 110 'n' 116 't' 101 'e'108 'l'
0x600114 <output+40>: 39 '\'' 10 '\n'
(gdb)
源码阅读
汇编语言模板
汇编语言模板
CPUID命令
- 传入参数 EAX
EAX Value CPUID Output
- 传出参数 EBX、EDX、ECX
❑ EBX contains the low 4 bytes of the string.
❑ EDX contains the middle 4 bytes of the string.
❑ ECX contains the last 4 bytes of the string.
When the value of zero is placed in the EAX register, and the CPUID instruction is executed, the processor returns the Vendor ID string in the EBX, EDX
int $0x80 系统调用
eax=4 write系统调用
ebx=1 文件描述符
输出到控制台屏幕 STDOUTeax=1 退出函数
ebx=0 返回给shell的程序码
零则表示程序成功执行
查看数据
-
查看:全部寄存器、单个寄存器、内存位置
Viewing the data
print 显示寄存器的值
- ❑
print/d
to display the value in decimal
❑print/t
to display the value in binary
❑print/x
to display the value in hexadecimal
x/nyz 显示特定内存位置的值
- where
n
is the number of fields to display -
y
is the format of the output, and can be
❑c
for character(字符)
❑d
for decimal(十进制)
❑x
for hexadecimal(十六进制) - and
z
is the size of the field to be displayed:
❑b
for byte
❑h
for 16-bit word (half-word)
❑w
for 32-bit word
如果要使用gcc进行汇编
