Author Topic: converting a string into a number  (Read 16547 times)

Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
converting a string into a number
« on: July 20, 2011, 01:20:40 PM »
hey yall
i cant seem to get this converting a string to go into the single digit ,,i mean instead of pick a number (1) (2) etc i have to use higher numbers like (71) (72) etc ..
is there a way to do this without converting it ?
thanks

Code: [Select]
[ORG 0x100]

        mov     ah, 9           ; Print "something"
        mov     dx, strchoose   ;
        int     0x21            ;

MainLoop:
        mov     ah, 9           ; Print "something"
        mov     dx, strinput    ;
        int     0x21            ;           
     
        mov     ah, 0xA         ; Input String
        mov     dx, buf         ;
        int     0x21            ;

        call    StrToDec        ; This function Converts the input string into a number, AL=Result

        cmp     al, 71          ; Compare AL with the number you choose
        je     Lblwinxp
        cmp     al, 72
        je     Lbldos
        cmp     al, 73         
        je     Lblrestore
        cmp     al, 74     
        je     Lblreboot

        jmp    MainLoop
Lblwinxp:
        mov     ah, 9           ;
        mov     dx, strone      ;
        int     0x21            ;
        jmp     MainLoop        ;

Lbldos:
        mov     ah, 9           ;
        mov     dx, strtwo      ;
        int     0x21            ;
        jmp     MainLoop        ;


Lblrestore:
        mov     ah, 9           ;
        mov     dx, strthree    ;
        int     0x21            ;
        jmp     MainLoop        ;


Lblreboot:       
        mov     ah, 9
        mov     dx, strfour     ;         
        int     0x21            ;

        mov     ax, 0x4C00      ; Terminate program
        int     0x21            ;


strchoose       db           10,13
                db           ' PLEASE CHOOSE ONE',10,13
                db           '(71)WINDOWS XP',10,13
                db           '(72)DOS',10,13
                db           '(73)RESTORE WINDOWS XP',10,13   
                db           '(74)REBOOT $',10,13
strinput        db        10,13,'TYPE THE NUMBER: $'





strone           db     13,10,'        71 $'
strtwo           db     13,10,'        72 $'
strthree         db     13,10,'        73 $'
strfour          db     13,10,'        74 $'


; The keyboard input buffer
buf:
max     db      3
count   db      0
inpdata db      0,0,0


; Convert the string inpdata to a number and store the result in AL
; Input parameters:  None
; Return value: AL = Number
StrToDec:

        mov     dl, [inpdata]   ; Get the first character and
        sub     dl, '0'         ; convert it into a value

        mov     al, dl          ; AL = 10 * value
        mov     bl, 10          ;
        mul     bl              ;

        mov     dl, [inpdata+1] ; Get the second character and
        sub     dl, '0'         ; convert it into a value

        add     al, dl          ; AL = AL + value

        retn





Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: converting a string into a number
« Reply #1 on: July 20, 2011, 06:35:05 PM »
What are you actually trying to do, convert a string to a number, or select a "one key" menu item?

You've got the algorithm approximately correct to convert a string to a number, but you want to convert the number of characters actually entered, not unconditionally two.

If you just want to select a menu item, junk the "buffered input" routine, and use one of the "input one key" routines... int 21h/2, 21h/7, or 21h/8, IIRC, or int 16h/10h, and just compare.

If you want to keep the same code you've got, just check "count" before you attempt to convert the second character.

Best,
Frank


Offline flyhigh427

  • Jr. Member
  • *
  • Posts: 60
Re: converting a string into a number
« Reply #2 on: July 21, 2011, 01:09:06 AM »
the answer was very simple ..dah   put the number in '1' instead of  1

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: converting a string into a number
« Reply #3 on: July 21, 2011, 04:22:10 AM »
the answer was very simple ..dah   put the number in '1' instead of  1

'1' is the equivalent of hexadecimal 0x31, in which is the ASCII [display] code for that particular number.

So yes, unless you plan on doing bin2ascii/ascii2bin conversions, handling direct ASCII codes in such a simplistic manner is sufficient, and probably most efficient.