Thursday, February 10, 2011

GCD



%OUT-------------------------------------------------------------------------
%OUT- Computes GCD of a given set of Integers          
%OUT- output: GCD of the set of numbers   
%out – author: Ayon and Ariyam                                            
%OUT-------------------------------------------------------------------------

if1
include macrolib.lby         ;contains common macros
endif

.model small
.stack
.data
array db 10 dup(?)                                                ;the array to store values
count dw 1 dup(?)                                                ;array length
i_msg db "How many elements? ","$"
str_msg db "Input 6 two-digit nos. ","$"
msg db " Enter No. ","$"
out_msg db " GCD of the Nos. is ","$"
.code

;---------------------------------------------------
; Computes the gcd of two numbers
; input: nos. are in BL & BH
; ouput: gcd will be in BL
; regs. affected: BX & AX
;---------------------------------------------------

gcd proc                  ;supply the two nos. in BL & BH
            pushf
            mov ax,0000h
            cmp bl,bh
            jb ok               ;(BL)<(BH), no xchg needed
            xchg bl,bh    ;else exchange
ok:       mov al,bh
lp:        div bl
            cmp ah,00h ;check if remainder zero
            je done
;remainder not zero
            mov al,bl
            mov bl,ah     ;final result will be in BL
            mov ah,00h
            jmp lp
done:
            popf
            ret
gcd endp    
           
main proc

            mov ax,@data
            mov ds,ax

;accept data
            showmsg i_msg
            input                                      ;call input macro to take no. of values
            lea bx,count
            mov [bx],al                          ;store the count value
            input_array array,count,msg
            mov si,00h
            mov cx,count
            dec cx
            mov bl,array[si]

lp1:
            inc si
            mov bh,array[si]
            call gcd
            loop lp1
            mov bh,bl
            showmsg out_msg
            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.