寻找最大数字的简单汇编算法 - 为什么它返回错误的数字?
我正在开发一个简单的例程,它将循环遍历数字列表并返回最大值。它总是返回 11,我看不出我的逻辑有什么问题。为了测试例程,我有一个正在循环的数字列表(data_items)。我在这里做错了什么?
.section .data
data_items: #these are the data items
.long 3,67,34,222,45,75,857,858,983,11,55,43,23,123,785,4356,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:
cmpl $0, %eax #check to see if we've hit the end
je loop_exit
incl %edi #load the next value
movl data_items(,%edi,4), %eax
cmpl %ebx, %eax #compare values
jle start_loop #jump to the start of the loop if the value is not larger
movl %eax, %ebx #move the value as the largest
jmp start_loop #jump to the loop beginning
loop_exit:
movl $1, %eax #1 is the exit() syscall
int $0x80
I am working on a simple routine that will loop through a list of numbers and return the max. It is always returning 11 and I cannot see what's wrong with my logic. To test the routine I have a list of numbers (data_items) that I am looping through. What am I doing wrong here?
.section .data
data_items: #these are the data items
.long 3,67,34,222,45,75,857,858,983,11,55,43,23,123,785,4356,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:
cmpl $0, %eax #check to see if we've hit the end
je loop_exit
incl %edi #load the next value
movl data_items(,%edi,4), %eax
cmpl %ebx, %eax #compare values
jle start_loop #jump to the start of the loop if the value is not larger
movl %eax, %ebx #move the value as the largest
jmp start_loop #jump to the loop beginning
loop_exit:
movl $1, %eax #1 is the exit() syscall
int $0x80
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基于 Unix 的操作系统仅支持 8 位返回值(即 0-255)。
因此您的程序确实找到了最大值,并将其存储在
%ebx
中,但是您无法将其作为程序的退出代码返回。我运行了你的程序,没有大于 255 的数字,并且它工作正常。Unix based operating systems only support 8-bit return value (so 0-255).
So your program does find the maximum value, and store it in
%ebx
, but you cannot return it as the program's exit code. I ran your program without the numbers that are bigger than 255 and it worked correctly.你的逻辑完全没有问题。当我将该代码输入
qq.s
并执行以下命令时:换句话说,正确的值正在加载到
ebx
中。There's nothing wrong with your logic at all. When I enter that code into
qq.s
and execute the following:In other words, the correct value is being loaded into
ebx
.两点(1)当调试并得到不合理的答案时,从测试数据中删除该值,因此在这种情况下从数据中删除 11 并看看会发生什么
(2)我刚刚检查了值 4356(10)并显示它以十六进制表示并得到 1104(16),所以我认为您的返回代码仅获取 16 位值的左字节 (4356)。
two points (1) when debugging and getting an unreasonable answer, remove that value from your test data, so in this case remove the 11 from your data and see what happens
(2) I just checked the value 4356(10) and displayed it in hex and got 1104(16), so I am thinking your return code is only getting the left byte of a 16 bit value (4356).