gpt4 book ai didi

assembly - 在汇编中获取字符串长度的短格式

转载 作者:行者123 更新时间:2023-12-02 02:38:51 26 4
gpt4 key购买 nike

要获取字符串的长度,我使用以下函数:

string:     .asciz "hello world!\n"
get_string_length:
mov $0, %eax # size goes in rax
.L1_loop:
movzbw string(,%eax,1), %ebx
cmp $0, %ebx
je .L1_exit
inc %eax
jmp .L1_loop
.L1_exit:
ret

不过,我也看到了以下内容:

hello_world:
.ascii "hello world\n"
hello_world_len = . - hello_world

以下是如何工作的?那就是 . 符号和所有获得长度的东西?例如,在此处的 github 片段中:https://github.com/cirosantilli/linux-kernel-module-cheat/blob/9dccafe00d9b0affa8847836a71ebb4c37be7090/userland/arch/x86_64/freestanding/linux/hello.S

最佳答案

第一个版本在运行时确定长度,第二个版本在汇编时设置长度。

第二个表达式中的.表示当前地址(在数据段中)。然后,表达式

hello_world_len = . - hello_world

当前地址中减去标签hello_world:表示的字符串.ascii "hello world\n"的起始地址(由 . 指示)导致长度值 hello_world_len.

关于assembly - 在汇编中获取字符串长度的短格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63928862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com