#cpuid.s Sample program to extract the processor Vendor ID
.section .data
output:
.ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"
.section .text
.global _start
_start:
movl $2, %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
as -o cpuid.o cpuid.s
ld -o cpuid cpuid.o
$ ./cpuid
The processor Vendor ID is 'GenuineIntel'
beyes@beyes-groad:~/programming/assembly/cpuid$ gcc -o cpuid cpuid.s
beyes@beyes-groad:~/programming/assembly/cpuid$ ./cpuid
The processor Vendor ID is 'GenuineIntel'
$ as -gstabs -o cpuid.o cpuid.s
ld -o cpuid cpuid.o
-rwxr-xr-x 1 beyes beyes 991 2009-11-27 10:51 cpuid #使用了 -gstabs 选项
-rwxr-xr-x 1 beyes beyes 663 2009-11-27 10:53 cpuid #不使用 -gstabs 选项
beyes@beyes-groad:~/programming/assembly/cpuid$ gdb cpuid
GNU gdb (GDB) 7.0-ubuntu
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/beyes/programming/assembly/cpuid/cpuid...done.
(gdb)
_start:
nop
movl $0, %eax
cpuid
(gdb) b *_start+1
Breakpoint 1 at 0x8048075: file cpuid.s, line 12.
(gdb) r
Starting program: /home/beyes/programming/assembly/cpuid/cpuid
Breakpoint 1, _start () at cpuid.s:12
12 movl $0, %eax
数据命令 | 描述 |
info registers | 显示所有寄存器的值 |
print | 显示特定寄存器或来自程序的变量的值 |
x | 显示特定内存位置的内容 |
(gdb) info registers
eax 0xa 10
ecx 0x6c65746e 1818588270
edx 0x49656e69 1231384169
ebx 0x756e6547 1970169159
esp 0xbffff480 0xbffff480
ebp 0x0 0x0
esi 0x0 0
edi 0x80490ac 134516908
eip 0x8048081 0x8048081 <_start+13>
eflags 0x212 [ AF IF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x0 0
(gdb) info registers edi #查看 edi 这个寄存器
edi 0x80490ac 134516908
(gdb) print/x $ebx
$1 = 0x756e6547
(gdb) print/x $edx
$2 = 0x49656e69
(gdb) print/x $ecx
$3 = 0x6c65746e
x/nyz
(gdb) x/42cb &output
0x80490ac <output>: 84 'T' 104 'h' 101 'e' 32 ' ' 112 'p' 114 'r' 111 'o' 99 'c'
0x80490b4 <output+8>: 101 'e' 115 's' 115 's' 111 'o' 114 'r' 32 ' ' 86 'V' 101 'e'
0x80490bc <output+16>: 110 'n' 100 'd' 111 'o' 114 'r' 32 ' ' 73 'I' 68 'D' 32 ' '
0x80490c4 <output+24>: 105 'i' 115 's' 32 ' ' 39 '\'' 71 'G' 101 'e' 110 'n' 117 'u'
0x80490cc <output+32>: 105 'i' 110 'n' 101 'e' 73 'I' 110 'n' 116 't' 101 'e' 108 'l'
0x80490d4 <output+40>: 39 '\'' 10 '\n'
#define __NR_write 4
#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
.global _start
_start:
movl $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
beyes@beyes-groad:~/programming/assembly/cpuid$ ld -o cpuid2 cpuid2.o
cpuid2.o: In function `_start':
(.text+0x1f): undefined reference to `printf'
cpuid2.o: In function `_start':
(.text+0x29): undefined reference to `exit'
$ ld -o cpuid2 -lc cpuid2.o
$ ./cpuid2
bash: ./cpuid2: 没有该文件或目录
beyes@beyes-groad:~/programming/assembly/cpuid$ ld -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
beyes@beyes-groad:~/programming/assembly/cpuid$ ./cpuid2
The processor Vendor ID is 'GenuineIntel'
beyes@beyes-groad:~/programming/assembly/cpuid/temp$ ls -l cpuid2
-rwxr-xr-x 1 beyes beyes 8394 2009-11-28 21:27 cpuid2
beyes@beyes-groad:~/programming/assembly/cpuid$ ls -l cpuid2
-rwxr-xr-x 1 beyes beyes 2007 2009-12-01 13:39 cpuid2
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |