1 ; Chapter 3 "Maximum"
     
2 ; 
     
3 ; Finds the largest value in a series of 4-byte values.
     
4 ; Demonstrates x86 memory access via: address + index * multiplier.
     
5 ;
     
6 ;    $ ./go.sh ch03.2_maximum
     
7 ;    Exited with code: 222
     
8 ;
     
9 ; Note: My version here differs from the book's. I don't preload
    
10 ;       the first value into eax and I check for the sentinel
    
11 ;       later in the loop. Savings: 1 instruction. :-)
    
12 
    13 section .data
    
14 
    15 ; "double" sized items (32 bits) with zero sentinel at the end
    
16 data_items: dd 3, 67, 34, 222, 45, 75, 54, 0
    
17 
    18 section .text
    
19 
    20 global _start
    
21 
    22 _start:
    
23     mov edi, 0 ; start our data "index" at 0
    
24     mov ebx, 0 ; ebx will hold the largest value found
    
25 
    26 start_loop:
    
27     mov eax, [data_items + edi * 4] ; get item by index (* 4 bytes)
    
28     cmp eax, 0       ; have we hit the sentinel?
    
29     je loop_exit     ; ...yes, we're done with the list
    
30     inc edi          ; move index to next value to be checked.
    
31     cmp eax, ebx     ; Is this value bigger?
    
32     jle start_loop   ; ...no. Try the next value.
    
33     mov ebx, eax     ; ...yes. Store it...
    
34     jmp start_loop   ; ...and check next value
    
35 
    36 loop_exit:     ; ebx contains largest value, so that's our exit status
    
37     mov eax, 1 ; linux syscall 'exit'
    
38     int 0x80   ; interrupt to call linux kernel