Good day,
I have been trying to write a program that does the following:
1. Accept two ASCII numbers from a user (I haven't bothered trying to check the values yet)
2. Convert those ASCII numbers to decimal values.
3. Find the greatest common divisor from those two numbers
4. Display the result
I feel as though I've successfully done steps 1-3, although I'm not entirely sure. Here is the code:
bits 16
org 0x100;
jmp main ;
number1_str: db 3,0,0,0,0,'$'
number2_str: db 3,0,0,0,0,'$'
num1_hex: dw 2
num2_hex: dw 2
prompt1: db 'Please enter first number (from 1 to 50): ','$'
prompt2: db 'Please enter second number (from 1 to 50): ','$'
cr_lf:
db 13,10,'$' ; carriage return and line feed
;
;Displays a string in dx
;
disp_str:
mov ah,09 ;
int 0x21 ;
ret ;
;
; Converts an ASCII string of digits to a decimal number, and puts the result
; in ax, and passes the address to dx. If an error occurs, AL is set to 'E'.
;
str_to_num:
xor ax,ax ; initial value of AX = 0
xor bh,bh ; BH = 0
mov cx,10 ; To build integer in AX (multiply by 10)
mov si,dx ; DX points to start of input buffer
call next_char ;
ret ;
next_char:
mov bl,[si] ; move contents of memory pointed to by SI to BL
cmp bl,0x0D ; Is it a carriage return?
je finis ; Yes, we are done
cmp bl,0x39 ; ASCII for the character '9' is 39h
jg error ; > '9', invalid character
sub bl,0x30 ; Convert to numeric value (ASCII '0' - 30h)
jl error ; < 0, invalid character
imul cx ; DX:AX = AX * 10 (32-bit result)
add ax,bx ; add next digit
inc si ; pointer to next char
jmp next_char ; repeat for next character
ret ;
error:
mov al,'E' ; Flag an error
finis:
ret ; return to calling program
;
;Calculates the greatest common divisor from the values in
;ax and bx. The GCD will be stored in ax.
;
GCD:
idiv bx ; remainder (7) in DX, quotient (1) in ax
mov ax,bx ; move bx into ax
mov bx,dx ; move dx into bx
cmp bx, 0x0 ; Is y = 0?
jg yIsZero ; Y is zero, return to call from main
call GCD ; loop again if y isn't zero
;
; Displays result in ax and terminates program
;
yIsZero:
xor dx,dx ; set dx to 0
add ax,0x30 ; convert remainder to ascii, store in ax
mov dx,ax ;
int 0x21 ;
int 0x20 ; terminate program
main:
mov dx,prompt1 ; move prompt1 to dx
call disp_str ; display prompt1
;get number1_str
xor dx,dx ; set dx to 0
mov ah,0x0a ; accept string from user
mov dx,number1_str; address for string
int 0x21 ;
call str_to_num ;
mov [num1_hex],ax ; put value of ax into contents of memory address at
; num1_hex
mov ah,09 ; display string
mov dx,cr_lf ; display carriage return and line feed
int 0x21 ;
mov dx,prompt2 ; move prompt2 to dx
call disp_str ; display prompt2
;get number2
xor dx,dx ; set dx to 0
mov ah,0x0a ; accept string from user
mov dx,number2_str; address for string
int 0x21 ;
call str_to_num ;
mov [num2_hex],ax ; put value of ax into contents of memory address at
; num2_hex
mov ah,09 ; display string
mov dx,cr_lf ; display carriage return and line feed
int 0x21 ;
; Find the greatest common divisor
xor dx,dx ; set dx to 0
mov ax,num1_hex ; move value in num1_hex to ax
mov bx,num2_hex ; move value in num2_hex to bx
call GCD ; find greatest common divisor from values in ax and bx
I'm expecting y_is_zero to display a result, but all it does is wait for entry when I run the code above. If I press any key, the program just ends. I
Any help would be greatly appreciated.
- JS