News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Decimal input/output problem

Started by DViz, September 22, 2009, 03:21:06 PM

Previous topic - Next topic

DViz

Hey all. I'm trying to write this 16-bit DOS assembly program... the assignment is to have the user input a decimal number, store it, then output it. However, when I actually run the program to do so, it seems to add some numbers by 7, and then just adds garbage to the end of it. I know I'm probably shifting the numbers wrong, but I can't for the life of me figure out where I'm messing up, or what the exact correct value of the shifting should be. All help would be greatly appreciated.


;Assembly - Project 2

INCLUDE Irvine16.inc

.data
prompt db "Please enter a number.", "$"
prompt1 db 0ah, 0dh, "Now, please enter another one.", "$"
prompt2 db 0ah, 0dh, "Kay! Here's your first number...", "$"
prompt3 db 0ah, 0dh, "...and here's your second one.", "$"
prime1 db 0ah,0dh, "ding ding... this is a prime number!!"
error1 db 0ah, 0dh, "Letters? Ha. HA! Hexidecimals are SO last project. Try again.", "$"
error2 db 0ah, 0dh, "I have no idea what you just entered, but it was AWESOME. And by AWESOME, I mean WRONG.", "$"

input byte 20 dup('$')

number1 word 0

.code
main PROC
mov ax,@data ;begin input from keyboard.
mov ds,ax                      ;set DS to point to the data segment
xor dx,dx
xor si,si ;initializing the index register here.
xor bl,bl

mov ah,9                       ;begins the DOS print string function.
mov dx,offset prompt      ;points to the input prompt...
int 21h ;prints the prompt.

start:
mov ah,1   
xor bx,bx

top: int 21h
Mov input[bx],al
cmp al,0dh
Je alldone
Inc bx
Jmp top
alldone:

redoloop: mov bl, input[si]
inc si
cmp bl,'$' ;error checking! i like me some extra credit...
je loopfinished
cmp bl,'A'
je lettererror
cmp bl,'B'
je lettererror
cmp bl,'C'
je lettererror
cmp bl,'D'
je lettererror
cmp bl,'E'
je lettererror
cmp bl,'F'
je lettererror
cmp bl, 58
jae invaliderror
sub bl, 48 ; <<==== Should this be BX instead of BL? are the two linked?
add number1, bx ; <<<==== keep an eye on this... should this be "add" or "mov?"
shl number1,4
loop redoloop
loopfinished:

mov cx,number1
xor si,si


printloop:    
cmp si, 4
jge nothingwentwrong
inc si
mov dx, cx
shr dx, 12           ;is this where my problem is?
shl cx, 4
add dx, 48

mov ah, 2
int 21h
jmp printloop



;------------------THIS IS THE END OF THE MAIN PROGRAM. JUMP PROMPTS WILL GO HERE.-----------------
jmp nothingwentwrong
lettererror:
mov ah,9                       ;begins the DOS print string function.
mov dx,offset error1      ;points to the input prompt...
int 21h ;prints the prompt.
jmp nothingwentwrong

invaliderror:
mov ah,9                       ;begins the DOS print string function.
mov dx,offset error2      ;points to the input prompt...
int 21h ;prints the prompt.

nothingwentwrong:
.EXIT
main ENDP
END main


This is my output:

C:\MASM611\BIN>decinout
52=0se enter a number.45
C:\MASM611\BIN>

FORTRANS

HI

   You have:

Quoteloop redoloop

But I do not see where you put a value into CX, so you could
be looping more than you want to.

Steve

dedndave

you should not assume that registers are preserved across DOS function calls
some are - some aren't

mov ds,ax                      ;set DS to point to the data segment
xor dx,dx
xor si,si ;initializing the index register here.
xor bl,bl

mov ah,9                       ;begins the DOS print string function.
mov dx,offset prompt      ;points to the input prompt...
int 21h ;prints the prompt.

change the order...

mov ds,ax                      ;set DS to point to the data segment

mov ah,9                       ;begins the DOS print string function.
mov dx,offset prompt      ;points to the input prompt...
int 21h ;prints the prompt.

xor dx,dx
xor si,si ;initializing the index register here.
xor bl,bl

and again here...

start:
xor bx,bx
top:
mov ah,1   
push bx
int 21h
pop bx
Mov input[bx],al
cmp al,0dh
Je alldone
Inc bx
Jmp top

i am assuming that converting the input value to a binary number is part of the assignment
otherwise, you could just echo what was entered and not convert it at all
Steve is right, of course, but no LOOP instruction should be required
you just want to terminate input when you see a carriage return

i dunno what this is all about - lol

printloop:   
cmp si, 4
jge nothingwentwrong
inc si
mov dx, cx
shr dx, 12           ;is this where my problem is?
shl cx, 4
add dx, 48

mov ah, 2
int 21h
jmp printloop

you have converted the value to binary
now you need to convert it back to decimal
there are a number of ways to do it, but the simplest to explain is to reverse what you do for conversion to binary
that means divide the value by ten to extract digits
they will come off in reverse order, so extract all the digits into a string
then, display the string all at once

as for "error detection" and "extra credit"
i would just ignore all non-numeric input - don't display it - don't use it

as postmaster K (MIIB) would say, "Go home and do it again"