tags:

views:

153

answers:

2

Hi, I am going through "Programming from ground up". Now I have the following code, which should give 2 as minimum answer, but I am getting 0 as answer when I do echo $?.


    .section .data
    data_items:
    .long 3,67,34,222,56,87,9,2,88,22,11,66,900,0
    .section .text
    .globl _start
    _start:
    movl $0,%edi
    movl data_items(,%edi,4),%eax
    movl %eax,%ebx
    start_loop:
     cmpl $0,%eax
     je loop_exit
 incl %edi
 movl data_items(,%edi,4),%eax
 cmpl %ebx,%eax
 jg start_loop
 movl %eax,%ebx
 jmp start_loop
loop_exit:
        movl $1,%eax
        int $0x80 
A: 

You're not printing the result. You need to debug. ebx will contain the answer after the loop has executed.

I know the int 0x80 means to call an external function, but I'm not sure what the details are there. Ok.. according to a nice page on interrupt 0x80 and system call numbers, the $1 is an exit code.

It dosn't seem like you're printing the result. Ok, the basic format of a print statement is this:

mov eax, <MEMORY POINTER TO STRING>
int 21h

You would need to convert your result into characters, put them in memory, and then pass in the memory location to the 'int 21h' call which will print them to the screen.

Try some of these examples and see if they work for you.

Can you debug the code to verify what's going on?

Kieveli
OP mentions `echo $?` which means he wants linux's, not DOS' API. Also, he doesn't print the value, but returns it as the exit status.
Adrian Panasiuk
You're right... Then it should be fine left in ebx. As the 2nd argument to the exit.
Kieveli
... or first argument as it were =)
Kieveli
+1  A: 

well, 0 is less than 2

Since you are JG'ing your going back to the loop if the value in eax is greater than the current ebx, also looks like the zero is used as an exit code in these lines

cmpl $0,%eax
je loop_exit

So in this case when you hit the zero in the list, it is effectively the lowest number AND the exit condition

curtisk
very true, I just realized while debugging
kost
The loop will exit without moving $0 into ebx. ebx will still contain the lowest value in the list
Kieveli
@ Kieveli: it will, at that point JG will not return to top of loop and the next line is movl %eax,%ebx, then it will go to top, see zero and exit
curtisk
got it !! moved these 2 lines cmpl $0,%eax je loop_exit ,after jg start_loop
kost
good deal! fun fun!
curtisk