This is a program that finds the highest common factor of 2 different 3digit number.
If I put 200, 235,312 (low numbers) it works fine, but if I put 500, 550, 654 (i.e. bigger numbers) the program crashes after the second 3-digit number is entered. Can you help me find out what the problem is?
.model small
.stack 400h
.data
message db "hello. ", 0ah, 0dh, "$"
firstdigit db ?
seconddigit db ?
thirddigit db ?
number dw ?
newnumber db ?
anumber dw 0d
bnumber dw 0d
Firstn db 0ah, 0dh, "Enter first 3 digit number: ","$"
secondn db 0ah, 0dh, "Enter second 3 digit number: ","$"
messageB db 0ah, 0dh, "HCF of two number is: ","$"
linebreaker db 0ah, 0dh, ' ', 0ah, 0dh, '$'
.code
Start:
mov ax, @data ; establish access to the data segment
mov ds, ax ;
mov number, 0d
mov dx, offset message ; print the string "yob choi 0648293"
mov ah, 9h
int 21h
num:
mov dx, offset Firstn ; print the string "put 1st 3 digit"
mov ah, 9h
int 21h ;run
JMP FirstFirst ; jump to FirstFirst
FirstFirst:
;first digit
mov ah, 1d ;bios code for read a keystroke
int 21h ;call bios, it is understood that the ascii code will be returned in al
mov firstdigit, al ;may as well save a copy
sub al, 30h ;Convert code to an actual integer
cbw ;CONVERT BYTE TO WORD. This takes whatever number is in al and
;extends it to ax, doubling its size from 8 bits to 16 bits
;The first digit now occupies all of ax as an integer
mov cx, 100d ;This is so we can calculate 100*1st digit +10*2nd digit + 3rd digit
mul cx ;start to accumulate the 3 digit number in the variable imul cx
;it is understood that the other operand is ax
; the result will use both dx::ax
;dx will contain only leading zeros
add anumber, ax ;save
;Second Digit
mov ah, 1d ;bios code for read a keystroke
int 21h ;call bios, it is understood that the ascii code will be returned in al
mov seconddigit, al ;may as well save a copy
sub al, 30h ;Convert code to an actual integer
cbw ;CONVERT BYTE TO WORD. This takes whatever number is in al and
;extends it to ax, boubling its size from 8 bits to 16 bits
;The first digit now occupies all of ax as an integer
mov cx, 10d ;continue to accumulate the 3 digit number in the variable
mul cx ;it is understood that the other operand is ax, containing first digit
;the result will use both dx::ax
;dx will contain only leading zeros.
add anumber, ax ;save
;third Digit
mov ah, 1d ;samething as above
int 21h ;
mov thirddigit, al ;
sub al, 30h ;
cbw ;
add anumber, ax ;
jmp num2 ;go to checks
Num2:
mov dx, offset secondn ; print the string "put 2nd 3 digits"
mov ah, 9h
int 21h ;run
JMP SecondSecond
SecondSecond:
;first digit
mov ah, 1d ;bios code for read a keystroke
int 21h ;call bios, it is understood that the ascii code will be returned in al
mov firstdigit, al ;may as well save a copy
sub al, 30h ;Convert code to an actual integer
cbw ;CONVERT BYTE TO WORD. This takes whatever number is in al and
;extends it to ax, doubling its size from 8 bits to 16 bits
;The first digit now occupies all of ax as an integer
mov cx, 100d ;This is so we can calculate 100*1st digit +10*2nd digit + 3rd digit
mul cx ;start to accumulate the 3 digit number in the variable imul cx
;it is understood that the other operand is ax
; the result will use both dx::ax
;dx will contain only leading zeros
add bnumber, ax ;save
;Second Digit
mov ah, 1d ;bios code for read a keystroke
int 21h ;call bios, it is understood that the ascii code will be returned in al
mov seconddigit, al ;may as well save a copy
sub al, 30h ;Convert code to an actual integer
cbw ;CONVERT BYTE TO WORD. This takes whatever number is in al and
;extends it to ax, boubling its size from 8 bits to 16 bits
;The first digit now occupies all of ax as an integer
mov cx, 10d ;continue to accumulate the 3 digit number in the variable
mul cx ;it is understood that the other operand is ax, containing first digit
;the result will use both dx::ax
;dx will contain only leading zeros.
add bnumber, ax ;save
;third Digit
mov ah, 1d ;samething as above
int 21h ;
mov thirddigit, al ;
sub al, 30h ;
cbw ;
add bnumber, ax ;
jmp compare ;go to compare
compare:
CMP ax, anumber ;comparing numbB and Number
JA comp1 ;go to comp1 if anumber is bigger
CMP ax, anumber ;
JB comp2 ;go to comp2 if anumber is smaller
CMP ax, anumber ;
JE equal ;go to equal if two numbers are the same
JMP compare ;go to compare (avioding error)
comp1:
SUB ax, anumber; subtract smaller number from bigger number
JMP compare ;
comp2:
SUB anumber, ax; subtract smaller number from bigger number
JMP compare ;
equal:
mov ah, 9d ;make linkbreak after the 2nd 3 digit number
mov dx, offset linebreaker
int 21h
mov ah, 9d ;print "HCF of two number is:"
mov dx, offset messageB
int 21h
mov ax,anumber ;copying 2nd number into ax
add al,30h ; converting to ascii
mov newnumber,al ; copying from low part of register into newnumb
mov ah, 2d ;bios code for print a character
mov dl, newnumber ;we had saved the ascii code here
int 21h ;call to bios
JMP exit;
exit:
mov ah, 4ch
int 21h ;exit the program
End