Hi everyone! I'm having a bit of difficulty with making a program for a class.
We have to use Euclid's algorithm for find the HCF (Highest Common Factor) for two multi-digit numbers.
The program has to prompt the user for the two inputs, then waits till the user presses enter to proceed.
The code that I have is for 1-digit, and doesn't wait for an enter key.
Here is my code so far.
My teacher gave me a hint
Can anyone lead me in the right direction? I've looked in a few books but they are not really helping.
I'm using TASM that runs in ms-dos from windows to compile and run this.
Thanks a lot!
We have to use Euclid's algorithm for find the HCF (Highest Common Factor) for two multi-digit numbers.
The program has to prompt the user for the two inputs, then waits till the user presses enter to proceed.
The code that I have is for 1-digit, and doesn't wait for an enter key.
Here is my code so far.
.model small
.stack 200h
.data
welcome db 'Henry' 0842', 0Ah, 0Dh, 'Euclids Algorithm', 0Ah, 0Dh, '$'
prompt2 db 0Ah, 0Dh, 'Please type the first integer $'
prompt3 db 0Ah, 0Dh, 'Please type the second integer $'
prompt4 db 0Ah, 0Dh, 'The HCF is $'
goodbye db 0Ah, 0Dh, 'quiting now$'
.code
mov dx, @data
mov ds, dx
mov dx, offset welcome
mov ah, 9d
int 21h
;begin algorithm by reading in two 1-digit integers
;print the prompt first, using BIOS code '9d'
mov dx, offset prompt2
mov ah, 9d
int 21h
;now get the keystroke
mov ah, 1d ;BIOS code for read a keystroke
int 21h ;invoke BIOS, ASCII code will be in register 'al' after this command
;save the keystroke in register 'cl'
;after we get the integer value from the ASCII code
;by subtracting hex 30 from the ASCII code value
sub al, 30h
mov cl,al
;now read the second integer as above
;first print the prompt using BIOS code '1d'
mov dx, offset prompt3 ;set the offset for DS::DX
mov ah, 9d ;set BIOS code
int 21h ;invoke BIOS
mov ah, 1d ;BIOS code for read a keystroke
int 21h ;invoke BIOS, ASCII code will be in register 'al' after this command
sub ah, 30h
; we now have two integers, actual values not ASCII codes, one in 'cl' and the other in 'al'
repeat:
cmp cl, al ;compare the two integers
jz print_and_exit ;print the result and return control to operating system
;if here, the two numbers were **NOT** the same
;note that the flags are still set from the compare statement, but it's probably wise to do another
;one in case we change or edit the file.
cmp cl, al
jg greater ;test to see if cl > al, and if it is, jump to label 'greater'
;OK, so if we get to here, cl **must** be less than (<) al
sub al, cl ;diminish 'al' by 'cl'
jmp repeat ;go round the loop until the variables become equal
greater:
;if we get here, it was shown that cl > al, so diminish 'cl'
sub cl, al
jmp repeat ;go round the loop until the variables become equal
print_and_exit:
;there's only one way to get here and that's when the integers are equal
;so print and then we're done.
;first the prompt
mov dx, offset prompt4
mov ah, 9d ;BIOS code for print a string
int 21h
;last, the actual result
mov ah, 2d ;BIOS code for print to screen
;character's ASCII code must be in 'dl' for this to work
mov dl, cl ;starting to print 'cl's value
add dl, 30h ;convert value to ASCII code
int 21h
mov dx, offset goodbye
mov ah, 9d
int 21h
mov ah, 4ch ;BIOS code for exit/quit
int 21h
end
My teacher gave me a hint
When you have a multiple digit answer to the HCF algorithm, it is quite easy to extract and print the digits. Use DIV. Say your answer is 123. Divide by 100 and you'll get a quotient of 1 with remainder 23. That's your first digit. Take the 23 and divide by 10, you'll get a quotient of 2 remainder 3. That's your second and third digits extracted. Add 30 tpo convert to ASCII and print.
Can anyone lead me in the right direction? I've looked in a few books but they are not really helping.
I'm using TASM that runs in ms-dos from windows to compile and run this.
Thanks a lot!
Well you can take a look at http://www.asmcommunity.net/board/index.php?topic=24594.0