这是一个应该找到最少data_items的简单程序。 a 0用于终止内存块。 %ebx正在跟踪当前最小值,因为je
指令,0永远不会被复制到其中。然而,这个程序返回0到操作系统,而不是3。
.section .data
data_items:
.long 3,67,34,222,45,75,54,34,44,222,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 #first item, so its smalles, ebx tracks smallest
start_loop:
cmpl $0, %eax #check if we hit the end
je loop_exit
incl %edi
movl data_items(,%edi,4), %eax
cmpl %ebx, %eax
jge start_loop
movl %eax, %ebx
jmp start_loop
loop_exit:
movl $1, %eax
int $0x80
编辑:在此处更正代码:
.section .data
data_items:
.long 3,67,34,222,45,75,54,34,44,222,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 #first item, so its biggest, ebx tracks biggest
start_loop:
incl %edi
movl data_items(,%edi,4), %eax
cmpl $0, %eax
je loop_exit
cmpl %ebx, %eax
jge start_loop
movl %eax, %ebx
jmp start_loop
loop_exit:
movl $1, %eax
int $0x80
答案 0 :(得分:1)
逻辑不对。
在C代码中,您的程序看起来像
edi = 0;
eax = data_items[edi];
ebx = eax;
while(eax != 0) {
eax = data_items[++edi];
if(eax < ebx) ebx = eax;
}
exit(ebx);
问题是您加载eax
然后立即将其存储为最小值。如果eax = 0
,则将其存储为最小值(因为它是),然后才会中断。