# ECE511 
# HW2
# Programmer: Chia-hung Huang
# G#: 00419075


# This program is to find the max & min values from a set a data_item

# Variables: The registers have the following uses:
# %edi index of the data_item
# %ebx the largest value position
# %eax the smallest value position
# %ecx current data position

# The following memory location are used:
# data - contains the data. A 0 is used to terminate the data_item
#  

.section .data
 
     data_item:
           .long 27,2,3,1,6,6,7,8,9,122,1,0
          
.section .text

.globl _start
     _start:
          movl $0, %edi                                      # move 0 to index register
          movl data_item(,%edi,4), %ebx                      # load first number to %ebx
          incl %edi                                          # load next value
          movl data_item(,%edi,4), %eax                      # load 2nd number to %eax
          cmpl %ebx, %eax                                    # compare %ebx to %eax
          jle decide_branch                                  # jump to decide_branch if %ebx >= %eax
          movl %ebx, %eax                                    #   
          movl %eax, %ebx                                    # to make sure that %ebx holds the largest value
               decide_branch:                                # & %eax holds the smallest value
                    movl %ebx, %ebx                          #  
                    movl %eax, %eax                          #
          start_loop:                                        # start loop 
               incl %edi                                     # load the next value
               movl data_item(,%edi,4), %ecx                 # load the next value to %ecx
               cmpl $0, %ecx                                 # 
               je loop_end                                   # check if we hit the end 
               
               cmpl %ecx, %ebx                               #
               jl max_branch                                 # if ecx >ebx, jump to max_branch
               cmpl %ecx, %eax                               # if not compare %ecx to %eax
               jg min_branch                                 # if ecx < eax, jump to min_branch
               jmp start_loop
                    
                    max_branch:
                         movl %ecx, %ebx                     # load the largest number to %ebx
                         jmp start_loop
                            
                    min_branch:
                         movl %ecx, %eax                     # load the smallest value to %eax
                         jmp start_loop
                         
                         
          loop_end: 
                   
               movl $1, %eax                                 # loading 1 to %eax is exit() system call 
               int $0x80
