gpt4 book ai didi

linux - 对汇编中ADD指令或程序导出值的误解

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:14 24 4
gpt4 key购买 nike

在 64 位 Linux 计算机上再次修改汇编语言,尽管这应该不会有什么不同。

我会复制我的程序并用我的方式讲完它。目前我没有得到我期望的答案。我们开始吧:

global _start

section .data
v1 dq 151 ; first variable points to memory location containing "151d"
v2 dq 310 ; uint64_t v2 = 310d
sum dq 0

section .text
_start:

mov rax, 9 ; rax now contains 9
add [v1], rax ; v1 now points to a memory location containing 151 + 9 = 160

mov rax, [v2] ; rax contains the value 310
add rax, 10 ; rax contains the value 310 + 10 = 320
add rax, [v1] ; rax contains the value 320 + 160 = 480
mov [sum], rax ; sum now points to a memory location containing the value 480

mov eax, 1 ; system call to "exit"=1
mov ebx, [sum] ; return value of program is 480
int 0x080 ; call the system interrupt to terminate program

然后为了运行我的程序,我这样做:

./main.exec; echo $?

输出是:

224

不是 480 吗?我猜我误解了 add 的工作原理,或者误解了如何将退出代码返回给操作系统。我对此是否正确?

最佳答案

Linux 上保证支持的退出代码范围是 0-255(含)。保留退出状态的高位,以传达有关程序终止的其他信息。 480 超出此范围,因此实际退出代码未定义。

但是,大多数实现只会截断退出代码,这就是这里发生的情况:480 mod 256 = 224。

关于linux - 对汇编中ADD指令或程序导出值的误解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17863866/

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