HI all
i'm new to this site, but i'm hoping someone can help. I have a program to write to input 6 numbers as ascii characters add them, divide by six and output the result. I have functions that convert to numeric and from numeric back to string, but when i ask for a keyboard input, i get a divide overflow.I know it must be in the num_to_str where i have idiv bx, but i cannot figure out why. WHen i don't ask for an input and start with '5' as input, i get the answer 9 on the screen no matter what. below is the code. thank you for any help you can provide. my email address is
niri@ananzi.co.zaThanks and kind regards,
NIRI
-----------------------------------------------------------------
bits 16
org 0x100
jmp main
init_mess:
db 'Please enter the numbers you selected : ',0ah,0dh,'$'
input_buf: db 2
outputbuf: db ' ','$'
blank: db ' ','$'
next_mess:
db 'Number ','$'
store:
db 'Stored value right now is',0ah,0dh,'$'
input:
db '2','$'
line_ret:
db 0ah,0dh,'$'
display_number:
mov dx,next_mess
mov ah,09
int 21h
ret
display_lineret:
mov dx,line_ret
mov ah,09 ; Service - display of string
int 21h
ret
display_colon:
mov ah,02
mov dl,3ah
int 21h
ret
display_n1:
mov ah,02
mov dl,31h
int 21h
ret
display_n2:
mov ah,02
mov dl,32h
int 21h
ret
display_n3:
mov ah,02
mov dl,33h
int 21h
ret
display_dx:
mov ah,09 ; Service - display of string
int 21h
ret
read_string:
mov ah,0ah
mov dx,input_buf
int 21h
ret
;{----------------------------- CONVERT STRING TO NUMBER ----------------------------
; INPUT = DX
; OUTPUT = AX
str_to_num:
xor ax,ax ; Initial value of AX = 0
xor bh,bh ; Initial value of BH = 0
mov cx,10 ; To build integer in ax(multiply by 10)
mov si,dx ; DX point to the start of input buffer
next_char:
mov bl,[si] ; Move contents of memory pointed to by Si to BL
cmp bl,0Dh ; Is it a carriage return?
je finis ; Yes, we are done
cmp bl,39h ; Compare for '9'
jg error ; Is greater than 9 - invalid
sub bl,30h ; Convert to numeric value (ASCII '0' = 30h)
jl error ; <0, invalid character
imul cx ; DX:AX = AX * 10
add ax,bx ; Add next digit
inc si ; Pointer to next character
jmp next_char ; Repeat for next character
error:
mov al,'E' ; Error flag
finis:
ret ; Return to calling program
;{------------------------------ CONVERT NUMBER TO STRING --------------------------
; INPUT = AX
; OUTPUT = DX
num_to_str:
mov di,outputbuf ; Address of output buffer
;add di,1 ; Point to end of output buffer [total buffer - 1]
mov bx,10 ; We divide by 10
loopnts:
idiv bx ; Divide DX:AX by BX, result in Ax and remainder in DX
add dx,30h ; Convert remainder to ASCII
mov [di],dl ; Move digit into output string
cmp ax,0 ; Is the result 0
je finish ; Yes then we have finished
dec di ; Pointer one back
xor dx,dx ; Clear DX
jmp loopnts ; Repeat loop
finish:
mov dx,outputbuf ; DX = address of output buffer
mov ah,09 ; Display funtions call
int 21h ; DOS system call
ret
; {---------------------------- MAIN PROGRAM ----------------------------------------
main:
mov dx,input_buf
call read_string
mov ah,0
mov dx,ax
call str_to_num
call num_to_str
int 20h ; Terminate the