汇编语言学习笔记——4

汇编语言学习[2018-05-10],第 4 天

汇编语言的数据定义和操作

汇编程序的数据块定义

  • .section .data 静态数据,带初始化值,程序编译连接后会直接包含在程序文件中
  • .section .rodata 静态数据,只读
  • .section .bss 动态数据,不带初始化值,程序运行时会动态分配内存,不会占用程序文件大小

.data和.rodata块的数据类型

数据类型指示 数据类型描述
.ascii Text string
.asciz Null-terminated text string
.byte Byte value
.double Double-precision floating-point number
.float Single-precision floating-point number
.int 32-bit integer number
.long 32-bit integer number (same as .int)
.octa 16-byte integer number
.quad 8-byte integer number
.short 16-bit integer number
.single Single-precision floating-point number (same as .float)

.data和.rodata块数据变量定义

变量标识符:数据类型 初始化值

  • output:.ascii “The processor Vendor ID is ‘xxxxxxxxxxxx’\n”
  • pi:.float 3.14159
  • sizes:.long 100,150,200,250,300 #相当于数组

    .section .data
        msg:.ascii “This is a test message”
        factors:.double 37.45, 45.33, 12.30
        height:.int 54
        length:.int 62, 35, 47

定义常量,在.data块

  • .equ factor, 3
  • .equ LINUX_SYS_C

使用常量时,必须在常量前加上$符

  • movl $LINUX_SYS_CALL, %eax

.bss块数据类型

数据类型指示 数据类型描述
.ascii Declares a common memory area for data that is not initialized
.lcomm Declares a local common memory area for data that is not initialized

.bss块变量定义

.comm symbol, length
变量标识符 数据类型, 长度

.section .bss
.lcomm buffer, 10000

汇编语言数据操作

数据操作指令: movx source, destination
movx指令中的x可以是以下内容

  • l for a 32-bit long word value
  • w for a 16-bit word value
  • b for an 8-bit byte value

如下示例:

movl %eax, %ebx
movw %ax, %bx
movb %al, %bl

注意,还有MOVS指令,用于在内存间移动字符串

汇编语言数据操作规则:

  • An immediate data element to a general-purpose register
  • An immediate data element to a memory location
  • A general-purpose register to another general-purpose register
  • A general-purpose register to a segment register
  • A segment register to a general-purpose register
  • A general-purpose register to a control register
  • A control register to a general-purpose register
  • A general-purpose register to a debug register
  • A debug register to a general-purpose register
  • A memory location to a general-purpose register
  • A memory location to a segment register
  • A general-purpose register to a memory location
  • A segment register to a memory location

将立即数放到寄存器和内存

  • movl $0, %eax # moves the value 0 to the EAX register
  • movl $0x80, %ebx # moves the hexadecimal value 80 to the EBX register
  • movl $100, height # moves the value 100 to the height memory location

在寄存器间转移数据

  • movl %eax, %ecx # move 32-bits of data from the EAX register to the ECX register
  • movw %ax, %cx # move 16-bits of data from the AX register to the CX register
  • movb %al, %bx

在内存和寄存器间转移数据

  • movl value, %eax # 内存向寄存器
  • movl %ecx, value # 寄存器向内存

使用索引内存地址(类数组情况)

.section .data
    values:.int 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60
    ......
    movl $2, %edi
    movl values(, %edi, 4), %eax

索引内存地址的访问规则:

  • base_address(offset_address, index, size)
  • offset_address 和 index 值必须使用寄存器,但size可以使用普通数字
    # movtest3.s – Another example of using indexed memory locations
    .code32
    .section .data
        output: .asciz "The value is %d\n"
        values: .int 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60
    .section .text
    .globl main
        main:
            nop
            movl $0, %edi
        loop:
            movl values(, %edi, 4), %eax
            pushl %eax
            pushl $output
            call printf
            addl $8, %esp
            inc %edi
            cmpl $11, %edi
            jne loop
            movl $0, %ebx
            movl $1, %eax
            int $0x80

在寄存器中使用间接内存地址 ,类同指针

    movl $output, %edi
    movl %ebx, 28(%edi)
    movl %edx, 32(%edi)
    movl %ecx, 36(%edi)
  • 在变量前加上$符,获取变量的内存地址
  • 在寄存器外面加上(),标识操作寄存器指向的内存地址
  • 在寄存器的外面的()前面的数字,表示地址偏移量
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页