Enter larger_number
Enter smaller_number
While the larger_number is not equal to the smaller_number
If larger_number < smaller_number
Exchange values of larger_number and smaller_number
EndIf
Replace larger_number value with value of the larger_number
minus the smaller number
Loop back to while
Output larger_number
Stop
; GetNum - input a maximum of 4 decimal digits and return the
; unsigned binary form of this number in AX;
; Return with the Carry flag "on" if the input is invalid
;
; note that this particular version of the GetNum "proc" is flagged as "near"
; indicating that it will be contained in the same segment as the
; statement that calls it.
getnum proc near
mov num,0
mov bh,'9' ;setup for valid range testing
mov bl,'0'
mov cl,4 ;maximum number of digits acceptable
get_loop:
mov ah,01 ;get key (with echo)
int 21h
cmp al,0Dh ;exit loop on carriage return
je get_exit
cmp bh,al ;check that it's < '9'
jc bad_digit ;error if its not
sub al,bl
jc bad_digit ;error if its < '0'
mov ah,0
push ax ;save digit value for later addition
mov ax,num
mov dx,10 ;decimal based system
imul dx ;multiply ax by dx results in dx|ax
mov num,ax ;value of prior digits times 10
pop ax ;add this digit's value
add num,ax
sub cl,1
jnc get_loop
bad_digit: ;-- if we get here something is wrong
;-- by careful coding we have ensured that
;-- the carry flag will be set
;-- this is the "error" return indicator
get_exit:
mov ax,num
ret
;--- local data ---
num dw ?
getnum endp
while: mov ax,large ;Loop until large and small are equal
cmp ax,small
jz done
jmp while ;loop back
done:
mov ax,large
cmp ax,small
jnc endif ;if large >= small skip over conditional code
;otherwise, large < small
;-- insert conditional code here
endif: push large
push small
pop large
pop small
mov ax,large ;reduce large by small amount
sub ax,small
mov large,ax
; ShowNum - Display the unsigned binary value in AL as a
; decimal value
showNum proc near
mov bx,10 ;divide by 10 to convert to decimal
mov cl,0 ;count of number of digits produced
div_loop:
mov dx,0 ;setup to divide dx|ax by bx
idiv bx ;unsigned division:
;quotient in ax, remainder in dx
push dx ;save the digit (remainder)
inc cl ;keep track of number of digits
cmp ax,0 ;until nothing left to divide
jnz div_loop; loop back
mov dx,offset newline ;ensure display is on a newline
mov ah,09
int 21h
show_digit:
pop dx ;retrieve digit value from LIFO stack
add dl,30h ;convert digit to ASCII code
mov ah,02 ;display character
int 21h
dec cl ;loop until all digits shown
jnz show_digit
ret
;-- local data --
newline db 0Dh,0Ah,"$"
showNum endp
mov ah,4Ch ;terminate with code
int 21h