Reputation: 3981
Here is a simple program that should find the minimum of data_items. a 0 is used to terminate the block of memory. %ebx is tracking the current minimum and 0 should never get copied into it because of the je
instruction. And yet, this program is returning 0 to the OS and not 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
Edit: Correct code here:
.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
Upvotes: 0
Views: 57
Reputation: 179687
The logic isn't right.
In C code, your program looks like
edi = 0;
eax = data_items[edi];
ebx = eax;
while(eax != 0) {
eax = data_items[++edi];
if(eax < ebx) ebx = eax;
}
exit(ebx);
The problem is that you load eax
then immediately store it as the minimum. If eax = 0
, you store it as the minimum (because it is), and only then do you break.
Upvotes: 1