Thursday, February 10, 2011

Second Maximum & Second Minimum



%OUT---------------------------------------------------------------------------------------------------------
%OUT- Find second maximum & second minimum from an array accepted from user                             
%out- input: Each term of the array is accepted from the user                                                 
                     ; Size of the array is mentioned in the program itself
%out- output: Print second maximum & second minimum                                 
%out – author: Ayon and Ariyam                               
%OUT---------------------------------------------------------------------------------------------------------

if1
include macrolib.lby
endif

.model small
.stack
.data
array_size equ 6                              ;assuming array_size to be equal to 6
array db array_size dup(?)          ;reserving bytes = array_size for the array, uninitialised
ip db "Enter terms of the array: ","$"    
;a request to prompt the user for entering terms of the array
op1 db "Second Maximum: ","$"                                    ;output message
op2 db "Second Minimum: ","$"                                     ;output message  
heading db "** SECOND MAX - SECOND MIN **","$"           ;a heading

.code


main proc
            clrscrn                                                                        ;clearing the screen
            mov dx,0105h
            cursor                                                                         ;setting up the cursor
            showmsg heading                                                 ;displaying heading
           
            mov dx,0303h                                                         ;pointing to next line
            cursor                                                                         ;setting up the cursor
            input_array array,array_size,ip                           ;accept array from user
           
            mov dx,0503h                                                         ;advancing to next line
            cursor                                                                                    

           

            mov dh,00h                         ;dh will store first maximum
            mov dl,00h                           ;dl will store second maximum
            mov cx,array_size              ;loading cx with number of times iteration to take place
            mov si,00h                            ;setting up the index to point to the array element
           
outer:             cmp array[si],dl
                        jbe done
                        cmp array[si],dh
                        jae replace
                        mov dl,array[si]
                        jmp done

replace:        mov dl,dh
                        mov dh,array[si]
done:             inc si
                        loop outer
                       
            mov bh,dl                             ;storing 2nd maximum in BH to call 'output' macro
            showmsg op1
            output
                       
            mov dx,0703h                     ;advancing to next line
            cursor                                                                                    

            mov dh,0FFh                       ;dh will store first minimum
            mov dl,0FFh                         ;dl will store second minimum
            mov cx,array_size              ;loading cx with number of times iteration to take place
            mov si,00h                            ;setting up the index to point to the array element
           
outer1:          cmp array[si],dl
                        jae done1
                        cmp array[si],dh
                        jbe replace1
                        mov dl,array[si]
                        jmp done1

replace1:      mov dl,dh
                        mov dh,array[si]
done1:                       inc si
                        loop outer1
                       
            mov bh,dl                                         ;storing 2nd maximum in BH to call 'output' macro
            showmsg op2
            output

            exit
main endp
end main

No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.