GNU Assembler, dot notation (current address)

房东的猫 提交于 2020-01-07 09:22:32

问题


I would like to ask why it is ok to write something like this:

.section .data
hello:
    .ascii "Hello World\n"
.equ lenhello, . - hello

but it is not right when i type:

.section .data
hello:
    .ascii "Hello World\n"
lenhello:
    .long . - hello

After calling sys_write function first code works fine, but the second one apart from writing hello world produces a lot of trash


回答1:


You have forgotten to show how you use the value. If you do movl lenhello, %edx it should work fine. I assume you did movl $lenhello, %edx instead.

The .equ directive defines a symbol whose value will be the length, so you will reference that as $lenhello. It doesn't reserve any memory. Using your second version, you define a variable in memory that contains the length. $lenhello in that case will be the address of your variable, and not the length.

Full sample code:

.section .data
hello:
    .ascii "Hello World\n"
lenhello:
    .long . - hello

.text
.globl _start
_start:
    movl $1, %ebx
    movl $hello, %ecx
    movl lenhello, %edx
    movl $4, %eax
    int $0x80
    movl $1, %eax
    movl $0, %ebx
    int $0x80

It has nothing to do with the . symbol.



来源:https://stackoverflow.com/questions/24047839/gnu-assembler-dot-notation-current-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!