装配 - 循环不按预期工作

时间:2014-09-30 00:51:29

标签: assembly

我写了一个简单的程序,它找到了一个集合中的最小数字。我使用0作为哨兵来发出程序退出的信号。我的程序总是选择0,但每次在执行后键入echo $?时都是最小值。

  1 .section .data
  2 
  3 data_items:
  4         .long 3,67,34,222,45,75,54,34,44,33,22,11,66,0
  5 
  6 .section .text
  7 
  8 .globl _start
  9 
 10 _start:
 11         movl $0, %edi
 12         movl data_items(,%edi, 4), %eax
 13 
 14         movl %eax, %ebx
 15 
 16 start_loop:
 17         cmpl $0, %eax
 18         je loop_exit
 19 
 20         incl %edi
 21         movl data_items(, %edi, 4), %eax
 22 
 23         cmpl %ebx, %eax
 24         jl start_loop
 25 
 26         movl %eax, %ebx
 27         jmp start_loop
 28 
 29 loop_exit:
 30         movl $1, %eax
 31         int $0x80

此处,最低值应为3,因为0仅用作哨兵。我该如何解决这个错误?

1 个答案:

答案 0 :(得分:2)

  1 .section .data
  2 
  3 data_items:
  4         .long 3,67,34,222,45,75,54,34,44,33,22,11,66,0
  5 
  6 .section .text
  7 
  8 .globl _start
  9 
 10 _start:
 11         movl $0, %edi
 12         movl data_items(,%edi, 4), %eax
 13 
 14         movl %eax, %ebx
 15 
 16 start_loop:
 17         incl %edi
 18         movl data_items(, %edi, 4), %eax
 19 
 20         cmpl $0, %eax
 21         je loop_exit
 22 
 23         cmpl %ebx, %eax
 24         jge start_loop
 25 
 26         movl %eax, %ebx
 27         jmp start_loop
 28 
 29 loop_exit:
 30         movl $1, %eax
 31         int $0x80

我在增量后将比较值移至0并将jl更改为jge