gpt4 book ai didi

ubuntu - x86 程序集 jl 不起作用

转载 作者:行者123 更新时间:2023-12-04 18:31:45 24 4
gpt4 key购买 nike

我在 64 位 Ubuntu 上使用 x86 程序集(AT&T 语法)进行编码(所以我使用了 as --32ld -melf_i386 ,到目前为止,它在其他练习中运行良好)。
jl指令的工作与我的预期相反。我实际上可以让代码与 jg 一起正常工作,这基本上可以解决我的问题,但我想在这里找出根本问题。

代码片段如下:

   # Open file for reading
movl $SYS_OPEN, %eax # prepare syscall 5
movl $input_file_name, %ebx # move input file name into %ebx
movl $0, %ecx # open in read-only mode
movl $0666, %edx
int $LINUX_SYSCALL # execute system call,
# sys_open returns file descriptor in %eax

movl %eax, ST_INPUT_DESCRIPTOR(%ebp) # store the input file descriptor away

# This will test and see if %eax is negative.
# If it is not negative, it will jump to continue_processing.
# Otherwise it will handle the error condition that the negative number represents.
cmpl $0, %eax
jl continue_processing

pushl $no_open_file_msg
pushl $no_open_file_code
call error_exit

continue_processing:

# Open file for writing
movl $SYS_OPEN, %eax # prepare syscall 5

...并且该程序继续进行,尽管其余的应该与此问题无关。

使用 gdbgui 进行调试,我看到 open sys 调用返回输入文件描述符 (eax = 3) 没有问题。
然后你比较 0 和 3。如果我不是白痴,0 < 3 所以 jl指令 should take you to continue_processing , 但是 it does not .

但是,如果我改为使用 jg , 有用。开放系统调用 returns 3 in eaxjg properly jumpscontinue_processing .

我读过跳转操作数的顺序可能取决于汇编程序。这里会是这样吗?使用 gcc -m32 -nostdlib 编译具有相同的行为。我还尝试将订单交换为 cmpl %eax, $0但我得到错误:'cmp' 的操作数类型不匹配。

或者这可能只是这个 32 位代码在 64 位 Ubuntu 上运行这一事实的一个怪癖?

我在看书 Programming From the Ground Up .在第 125 页,本书示例插入了 .section .datajl continue_processing 之后, 插入一些标签和 .ascii命令,然后使用 .section .text 恢复代码就在 pushl $no_open_file_msg 之前.为了清理代码,我合并了 .section .data在顶部,因此不需要第二个 .section .text .它似乎不影响 jl问题,但我想我会提到它,以防问题确实存在。

最佳答案

在 AT&T 语法中,操作数的顺序与 Intel 语法相比是交换的。为此原因,

cmpl $0, %eax

实际上计算 eax - 0 并设置标志,而不是像您预期的那样计算 0 - eax。因此,所有标志的设置方式都与您最初预期的相反,从而导致您观察到的问题。没有真正的方法可以解决这个问题,因为它并没有真正的错误。我建议你习惯这个怪癖。

作为一般说明,使用 testl 通常是个好主意。指令而不是将值与零进行比较。 testl %eax,%eaxcmp $0,%eax 更紧凑在机器代码中,在某些微架构上也更快。 testl根据其操作数的按位和设置标志,因此如果 eax 为负数,则在 testl %eax,%eax 之后设置 SF你可以这样检查:
testl %eax,%eax
jns continue_processing

关于ubuntu - x86 程序集 jl 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47753157/

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