您的位置:首页 > 其它

汇编语言简介(二)

2016-06-13 00:00 585 查看
摘要: 通过《汇编语言程序设计》第四章学习

一、范例程序:cpuid.s

[code=plain].section .data

output:
.ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"

.section .text

.globl _start

_start:
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

这个程序查看CPUID指令产生的厂商ID字符串。

1、使用GNU汇编器和GNU连接器构建可执行程序

运行情况:



2、使用GNU通用编译器编译

gcc有默认的_start标签,查找的是main标签,所以代码要进行以下修改:

[code=plain].globl main

main:
movl $0, %eax
cpuid

运行情况:



二、调试程序

使用GDB

两种最常被检查的数据元素是用于变量的寄存器和内存位置。如下:









三、在汇编语言中使用C库函数

代码如下:cpuid2.s

[code=plain].section .data

output:
.asciz "The processor Vendor ID is '%s'\n"

.section .bss
.lcomm buffer, 12

.section .text

.globl _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

用连接器生成:



用gcc,只需把_start标签变为main。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: