繁体   English   中英

简单的组装算法来找到最大的数字-为什么返回错误的数字?

[英]Simple assembly algorithm to find the largest number - Why is it returning the wrong number?

我正在研究一个简单的例程,该例程将遍历数字列表并返回最大值。 它总是返回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

基于Unix的操作系统仅支持8位返回值(即0-255)。

因此,您的程序确实找到了最大值,并将其存储在%ebx ,但是您不能将其作为程序的退出代码返回。 我运行的程序没有大于255的数字,并且可以正常工作。

您的逻辑没有任何问题。 当我将代码输入qq.s并执行以下命令时:

pax$ as -o qq.o qq.s

pax$ ld -o qq qq.o

pax$ gdb qq
GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
... blah blah blah ...
Reading symbols from /home/pax/qq...(no debugging symbols found)...done.

(gdb) break loop_exit
Breakpoint 1 at 0x8048097

(gdb) run
Starting program: /home/pax/qq 
Breakpoint 1, 0x08048097 in loop_exit ()

(gdb) info reg ebx
ebx            0x1104   4356

(gdb) _

换句话说,正确的值被加载到ebx

两点(1),当调试并获得不合理的答案时,请从测试数据中删除该值,因此在这种情况下,请从数据中删除11,然后看看会发生什么

(2)我只是检查了值4356(10)并以十六进制显示它并得到了1104(16),所以我认为您的返回码仅获取16位值的左字节(4356)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM