Hello, I am trying to write a program that reads in a string of numbers. I am only allowed to use ReadChar (not ReadString, ReadInt, ReadBin, etc). The program reads in this string of numbers (2-16) which is a base number. Then the user enters another string of numbers. I have to call the same procedure for both. The program converts this 2nd string from the indicated base to base 2, 8, 10, and 16. Right now I am having problems just entering the string of numbers and reading them back as numbers and not ASCII characters. Can someone please point me in the right direction to get started on this first part? Below is the code I have so far. Thank you.

INCLUDE Irvine32.inc
promptBase BYTE "What base (2-16 or 0 to quit)?: ",0
promptVal BYTE "Number please: ",0
outputErrorMsg BYTE "Invalid entry. Try again.",13,10,0
outputBase2 BYTE "Base 2: ",13,10,0
outputBase8 BYTE "Base 8: ",13,10,0
outputBase10 BYTE "Base 10: ",13,10,0
outputBase16 BYTE "Base 16: ",13,10,0
base BYTE 2 DUP(?)  ; to hold base value
basesize = ($ - base)

main PROC
    call Clrscr

        mov edx, OFFSET promptBase      ; "What base (2-16 or 0 to quit)?: "
        call WriteString                ; print prompt to screen

        call ReadInteger                

        cmp esi, 1          ; if esi = 1, then base value is one digit
        je Base1            ; so jump to Base1 Instructions
        jmp Base2           ; otherwise jump to Base2 Instructions

            cmp base[esi], 0    ; if user entered 0
            je Quit             ; then jump to Quit

            cmp base[esi], 1    ; if value is 1, base value is invalid
            je Error            ; jump to error message

        cmp base[esi], 7        ; if 2nd value of base is 7 or greater, then base is greater than 16 and invalid
        jge Error               ; display error msg

        jmp RunProgram          ; otherwise run program

            mov edx, OFFSET outputErrorMsg     ; "Invalid entry. Try again."
            call WriteString
            mov eax, 0
            jmp L1

        ;call ReadInteger               ; pass value entered to ReadInteger Procedure
        ;call Crlf
        ;jmp L1

        call DumpRegs

main ENDP

ReadInteger PROC 
; Description: Reads in a string of numbers and stores them in 
; string array called base.

    mov edx, OFFSET base
    mov ecx, basesize

        call ReadChar
        call WriteChar
        mov base[esi], al     ; save character into base, position 1
        inc esi               ; increment pointer
        cmp al, 0dh           ; if "enter" key pressed
        je Return             ; then jump to Return
        jmp L1                ; otherwise repeat loop

ReadInteger ENDP
main ENDP

Edited by teppuus

4 Years
Discussion Span
Last Post by DarkLightning7

to read a string without readstring simply use readchar inside of a loop.
0. create a pointer to the end of the stack, (first available location in mem) this will be the begining of the string.
1. read the char
2. check if the char is /n
3. if it is /n goto setp 6
4. if it is any other character place it on the stack in main mem
5. goto step 1
6. terminate the stored string with your chosen char standard in c is null, /0

now that we have a string of numbers we can read them starting at the pointer you set earlier.
read the first char to a registry then convert it to its literal value by subtracting 48.
you now have a number stored in your chosen register.


Thank you for the tip...I will try to work on this project more tomorrow night and post the progress.


you could convert the characters to numbers as you read them then store it on the stack as described above. unless you can operate on the numbers right away then don't store it on the stack saves a lot of execution time if you dont have to dump you data to the stack.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.