【发布时间】:2013-04-16 21:06:25
【问题描述】:
我正在尝试更多地了解汇编编程。我正在关注“从头开始编程”一书。我在使用第二个示例程序时遇到了问题。我一直在对其进行故障排除,并且还在线检查了任何已发布的勘误表。
该程序旨在输出所列数字中的最高值。此列表在 data_items 下标记。在终端中运行后,程序应该什么都不做。但是,在您输入 echo $? 后,它应该返回 222(列表中的最高数字)。目前它返回 3 或第一个数字。
我认为以下不是问题
-inc 语句必须工作,否则它将陷入无限循环,因为它永远不会到达 0 以退出循环
-我认为这可能是 64 位环境中 mov 语句中的 .long 和 4 的问题,但是我确实尝试用 8 代替 4 并且没有运气。
-除了上述声明之外,我认为这不是一般的 64 位与 32 位问题,因为我在这里查看过,并且共识似乎是 32 位程序在 64 位上运行良好,我有尝试了我认为可能出现的一个特定问题。
我认为问题可能出在 ebx 和 eax 寄存器之间的第二个 cmp 语句上。
提前谢谢你,如果我的上述任何或所有假设是错误的,我很抱歉我只是想让你们知道我已经尝试过、研究过什么,以及我的思考过程在哪里。
代码如下。
#PURPOSE: This program finds the maximum number of a
# set of data items.
#
#VARIABLES: The registers have the following uses:
#
# %edi - Holds the index of the data item being examined
# %ebx - Largest data item found
# %eax - Current data item
#
# The following memory locations are used:
#
# data_items - contains the item data. A 0 is used
# to terminate the data
#
.section .data
data_items: #These are the data items
.long 3,67,34,222,45,75,54,34,44,33,22,11,66,0
.section .text
.globl _start
_start:
movl $0, %edi # move 0 into the index register
movl data_items(,%edi,4), %eax # load the first byte of data
movl %eax, %ebx # since this is the first item,
# %eax is the biggest
start_loop: # start loop
cmpl $0, %eax # check to see if we've hit theend
je loop_exit # if so uncondition jmp to exit
incl %edi # load next value
movl data_items(,%edi,4), %eax
cmpl %ebx, %eax # compare values
jle start_loop # jump to loop beginning
loop_exit:
# %ebx is the return value, and it has the highest number availible
movl $1, %eax #1 is the exit() syscall
int $0x80
【问题讨论】:
-
您只将一个值移动到 %ebx 一次(第一项)。也许您应该在“jle start_loop”之后有一个“movl %eax, %ebx”,然后是一个“jmp start_loop”?
-
刚看到你的评论。看起来我在教程中错过了它,我一直在努力找出问题所在并寻找错别字。谢谢。
-
我对这本书不熟悉,但它是否可能故意给你错误的示例代码,而你应该自己找到错误?这是一种常见的教学策略。 (我不相信这是一个好的教学策略。)
-
不,我刚刚检查过。我完全错过了它。我认为其中一种情况是,当您多次看某物时,您的眼睛会呆滞而您不再真正看到它。
标签: x86