Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code Modification

Status
Not open for further replies.

Cuso

MIS
Jul 11, 2005
8
0
0
US
As anyone can see I'm a newbe in assembly. I'm just trying to learn a bit and practicing. My problem right now is that I just created a program that allows me to enter any number from 1 to 5 and once I enter it the program will display the number I entered but instead of a number it will say the word. For example if I enter 5 it will the number you entered is "five". I know how to display it in number but I'm having a hard time trying to display it in words. Any help will be appreciated. My code is the following:

-------begin code---------
.model small
.stack 100h
.data

Number db ?
message1 db "Enter a number from 1 to 5:$ "
message2 db " The word is [$ "
message3 db " Enter another number from 1 to 5:$ "
message4 db " The word is [$ "

.code
main proc
mov ax,@data
mov ds,ax

mov dx,offset message1
mov ah,9
int 21h

mov ah,1
int 21h
mov number,al

mov dx,offset message2
mov ah,9
int 21h

mov ah,2
mov dl,number
int 21h
mov ah,2
mov dl,']'
int 21h

mov dx,offset message3
mov ah,9
int 21h

mov ah,1
int 21h
mov number,al

mov dx,offset message2
mov ah,9
int 21h

mov ah,2
mov dl,number
int 21h
mov ah,2
mov dl,']'
int 21h

mov ax,4c00h
int 21h
main endp
end main

--------- end of code ------------
 
Please use the [code][/code] tags when posting code.

--
 
As far as i can see the code just displays the number, you haven't made an effort yet to display it in words.

maybe you can make an array of strings which contains the numbers in words.
Next you convert the ASCII character gotten from the user to decimal, and use that as an index into the array.
 
This my modified code but by some reason when I enter the number it writes at the screen all the messages at the same time. For example if I enter 1 it writes:

The number is one
The number is two
The number is three
The number is four
The number is five

What am I doing wrong? Any help will be appreciated.

Code:
.model small
.stack 100h
.data
    
Number     db ?
message    db "Enter a number from 1 to 5: $ "
one        db 0Ah,"The number is one $",0
two        db 0Ah,"The number is two $",0
three      db 0Ah,"The number is three $",0
four       db 0Ah,"The number is four $",0
five       db 0Ah,"The number is five $",0      

.code
main proc
    mov ax,@data
    mov ds,ax
    

    mov dx,offset message
    mov ah,9
    int 21h


    mov ah,1
    int 21h
    mov number,al
       
 
    cmp number,1
    je L1

    cmp number,2
    je L2

    cmp number,3
    je L3

    cmp number,4
    je L4
 
    cmp number,5
    je L5

L1:
    mov dx,offset one
    mov ah,9
    int 21h

L2:
    mov dx,offset two
    mov ah,9
    int 21h

L3:
 
    mov dx,offset three
    mov ah,9
    int 21h

L4:

    mov dx,offset four
    mov ah,9
    int 21h

L5:

    mov dx,offset five
    mov ah,9
    int 21h
           
    mov ax,4c00h
    int 21h

main endp
end main
 
After printing each message your code drops straight into the next message block.

Code:
L1:
    mov dx,offset one
    mov ah,9
    int 21h
    [b]jmp short alldone[/b]
L2:
    mov dx,offset two
    mov ah,9
    int 21h
    [b]jmp short alldone[/b]
L3:
 
    mov dx,offset three
    mov ah,9
    int 21h
    [b]jmp short alldone[/b]
L4:

    mov dx,offset four
    mov ah,9
    int 21h
    [b]jmp short alldone[/b]
L5:

    mov dx,offset five
    mov ah,9
    int 21h
[b]alldone:           [/b]
    mov ax,4c00h
    int 21h


Hope this helps.

[vampire][bat]
 
That worked but now instead of appear all the messages. No matter which number I choose it will always answer me "The number is one". =/
 
this is because there was no match. it drops down to the L1 label, executes the code there and jumps to alldone.

the character you input is ASCII character '1', the values you are checking for are all numeric values,
which have completely different values for the computer "internally".

replace

cmp number,1

with

cmp number,'1'

do this with all the other numbers as well, and yer prog should work.
 
or

mov ah,1
int 21h
and al, 0fh
mov number,al

and leave your cmp instructions unchanged

you also need:

cmp number,5
je L5
jmp short alldone

to handle invalid inputs

Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top