美文网首页
[ATT汇编]程序举例:xxx.S 编译、链接、运行、调试 (C

[ATT汇编]程序举例:xxx.S 编译、链接、运行、调试 (C

作者: AkuRinbu | 来源:发表于2019-05-23 01:40 被阅读0次

使用教材

《汇编语言程序设计》
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 文件描述符
输出到控制台屏幕 STDOUT

eax=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进行汇编

使用gcc进行汇编

相关文章

  • [ATT汇编]程序举例:xxx.S 编译、链接、运行、调试 (C

    使用教材 《汇编语言程序设计》https://www.jianshu.com/p/8473cd0e92b6第4章 ...

  • 七.第一个程序

    汇编程序的步骤:1.编写代码2.编译,链接3.调试运行 debug模式下的数组是16进制,但是我们在编写汇编程序的...

  • 深入理解Java虚拟机之虚拟机类加载机制

    C/C++在运行前需要完成预处理、编译、汇编、链接;而在Java中,类加载(加载、连接、初始化)是在程序运行期间第...

  • C语言跨平台移植

    C语言编译流程: C源程序-->预编译处理(.c)-->编译、优化程序(.s、.asm)-->汇编程序(.obj、...

  • 有关C++语言基础的问题

    c/c++程序的编译过程(GCC的编译流程) 主要分为四个阶段:预处理,编译阶段,汇编阶段,链接阶段 当我们写好一...

  • 基础知识

    程序的生成过程:预处理:#define,include编译:生成汇编语言程序汇编:生成机器代码链接:动态链接(程序...

  • C语言程序内存布局

    C语言笔记之02-C语言程序内存布局 我们知道C语言的编译过程分为:预处理-编译-汇编-链接-生成可执行文件,而这...

  • C程序的编译链接

    C程序大体上分成预处理、编译、汇编和链接4个阶段。汇编器主要是根据汇编源代码中的section或者segment关...

  • C 语言编译流程

    C语言编译四个阶段: 预处理、编译、汇编、链接。 预处理阶段:预处理器cpp根据字符#开头的命令,修改C程序。通常...

  • 重新学习 c 语言(4)- 库和宿主实现(二) 程序如何运行

    (二) 程序如何运行 (涉及到编译原理,操作系统,动态链接等知识 解释运行先不谈) (1) c程序的编译过程...

网友评论

      本文标题:[ATT汇编]程序举例:xxx.S 编译、链接、运行、调试 (C

      本文链接:https://www.haomeiwen.com/subject/tudmzqtx.html