flat assembler
Message board for the users of flat assembler.

Index > DOS > need help with io

Author
Thread Post new topic Reply to topic
geekbasic@gmx.com



Joined: 25 Oct 2022
Posts: 29
Location: Arizona
geekbasic@gmx.com 25 Oct 2022, 07:33
Hello, please excuse this mess of a code. I am trying to combine examples from around the web. Trying to learn assembly.

My goal with this code is to input a string, convert the string to a value in a data segment, do some math such as add 1 to the value in the segment, then print the value in the segment as text. I am doing this to have basic routines to program with. Can't do much without proper io.

I seem to be having an issue somewhere between getting the value from the input string. the code is not printing what I expect.

May someone please help fix my code? Or maybe share some input routine.
Essentially what I need is a function to input numbers like INPUT in BASIC.
Something just for numerical input.
Will be useful for simple math programs that need input.
Later I will worry about floating points, etc.

I hope I provided a clear indication of the help that I need. It's late and freezing. Can hardly type anymore. Will return later when thawed and rested. ;)

Code:
org 100h

call inputvalue

mov [num],ax
add [num],1
mov ax,[num]

call echovalue

xor ah,ah
int 16h
int 20h

inputvalue:

        call inputstring
        call getvalue

ret

getvalue:

        lea si,[inputbuffer]
        mov cx,4
        xor bx,bx

        .nextdigit:

                movzx ax,byte[si]
                inc si
                sub al,'0'
                imul bx,10
                add bx,ax

        loop .nextdigit

        mov ax,bx

ret

inputstring:

        mov dx,inputbuffer
        mov bx,dx
        mov byte[bx+1],0
        mov ah,0Ah
        int 21h
        call newline
        mov dx,inputbuffer
        mov bx,dx
        movzx bx,byte[bx+1]
        add bx,dx
        inc bx
        inc bx
        mov byte[bx],"$"

ret

echostring:

        mov ah,09h
        int 21h

ret

echovalue:

        xor cx,cx
        mov bx,10

        .valuestack:

                xor dx,dx
                div bx
                add dx,'0'
                push dx
                inc cx
                test ax,ax

        jnz .valuestack

        mov ah,02h

        .writedigit:

                pop dx
                int 21h

        loop .writedigit

ret

newline:

        mov dx,crlf
        call echostring

ret

crlf db 0Dh,0Ah,"$"

inputbuffer db '',"$"

num dw 0    
Post 25 Oct 2022, 07:33
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1475
Location: Toronto, Canada
AsmGuru62 25 Oct 2022, 19:25
DOS is so outdated. Why not Windows?
And why is it freezing in Arizona?

Anyhow, I fixed few lines for you (your code works for only positive values):
Code:
org 100h

mov dx, prompt
call echostring

call inputvalue

mov [num],ax
add [num],1

mov dx, result
call echostring

mov ax,[num]
call echovalue

xor ah,ah
int 16h
int 20h

inputvalue:

        call inputstring
        call getvalue

ret

getvalue:

        ;lea si,[inputbuffer]
        mov si,digits

        ;ag62: small issue here. CX=4 implies that you only working with 4 digit
        ;input, like "5216". In case you get more or less digits -- you will get wrong value in AX.
        ;You need to check the value loaded from [SI] and detect if it is a digit.
        ;If it is not a digit -- you need to stop the conversion. I will do a check and
        ;jump out of loop if the AL register is not a digit.

        mov cx,40   ; This will convert all input
        xor bx,bx

        .nextdigit:

                movzx ax,byte[si]
                inc si

                ; digit is a value between [0..9]

                cmp al,'0'
                jb .done
                cmp al,'9'
                ja .done

                sub al,'0'
                imul bx,10
                add bx,ax

        loop .nextdigit

.done:
        mov ax,bx

ret

inputstring:

        mov dx,inputbuffer
        ;mov bx,dx
        ;mov byte[bx+1],0   ;ag62: not needed now 'inputbuffer' already has what is needed
        mov ah,0Ah
        int 21h
        call newline

;ag62: not sure what this is...;
;after INT 21H returns -- user input is already located in 'digits' buffer
;        mov dx,inputbuffer
;        mov bx,dx
;        movzx bx,byte[bx+1]
;        add bx,dx
;        inc bx
;        inc bx
;        mov byte[bx],"$"

ret

echostring:

        mov ah,09h
        int 21h

ret

echovalue:

        xor cx,cx
        mov bx,10

        .valuestack:

                xor dx,dx
                div bx
                add dx,'0'
                push dx
                inc cx
                test ax,ax

        jnz .valuestack

        mov ah,02h

        .writedigit:

                pop dx
                int 21h

        loop .writedigit

ret

newline:

        mov dx,crlf
        call echostring

ret

prompt db ' PLEASE ENTER UP TO 5 DIGITS: $'
result db '           YOUR VALUE + 1 IS: $'

crlf db 0Dh,0Ah,"$"

;ag62:
;When using INT 21H AH=0Ah you need to specify how many characters user can enter.
;This needs to be a 1st byte in the buffer.
;The 2nd byte in the buffer is also reserved and usually zero.
;Actual user input starts after second byte.
;So, I am fixing your buffer here: I tell DOS that user can enter
;a maximum of 8 characters, probably, including the [ENTER] character at the end.
;Next byte is zero -- just ignore that for now.
;Immediately after these two service bytes is an area of 12 bytes called 'digits'.
;This is where your SI register supposed to point when you convert to 16-bit value.

inputbuffer db 8,0
digits      rb 12       ; rb --> means Reserve Bytes (12 of them)

num dw 0

    
Post 25 Oct 2022, 19:25
View user's profile Send private message Send e-mail Reply with quote
geekbasic@gmx.com



Joined: 25 Oct 2022
Posts: 29
Location: Arizona
geekbasic@gmx.com 26 Oct 2022, 07:19
First, I just want to say thank you for helping me out. It means more than I can express in words.
I feel welcome here at this forum that I just found. Reading posts here makes me feel like it's the golden age again.

I'm in the foothills of Yucca, Arizona which is higher in elevation. Also, living off grid with my office in a steel freight container.
The place is down in the 40s at night already now in October. It's 110 in the summer. I used to live in Helendale, CA where the weather ranged between 10 and 120. It's better here in Yucca.

Why DOS? I could say it's personal. It feels like cheating to skip to 32 and 64 bit programming before 8 and 16. I grew up with DOS programs and still use them. It's not like Windows 10 where everything is too bloated and complex to fully understand. There's more reasons than I could say off the top of my head. I also run tiny x86 thin clients with Windows 95 and DOS. These little x86 computers consume 5v and between 1 to 2 amps. With my limited solar setup, this allows me computing time well into the night without having to depend on Android.

Once I am comfortable with developing DOS programs in asm, then I will learn WIN asm.
For now, I have a goal to write a simple high level language compiler for DOS.
I am writing the compiler in QBasic (for simplicity, for now...).
My little project has forced me to learn asm more honestly.
I am able to see my weaknesses.

The code modification you posted here is not only helpful to me, but I think it serves as a great example for newbies.
Why do I not see more examples like it? How to declare "variables", perform arithmetic with them, print data, input data, loop, and execute conditions? The honest and simple basics. All packed in one example. That's powerful.

I spent a portion of today studying that code and your modifications. I think I understand it pretty well, there's still some things I am not sure of, but I am rolling with it and using the code. I have already added it to my project and it works like a charm. I think by exposing myself to this code and the development process, I will be slowly learning all of the details.

You mentioned positive values. I am planning on posting a new thread asking about how to use fpu commands and signed numbers. Is it difficult to have signed numbers and floating points at the same time?

Anyway, for now I have added your username to my program credits section for helping with this code.
Is that acceptable for you? I might make a thread for my program soon.
Post 26 Oct 2022, 07:19
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1475
Location: Toronto, Canada
AsmGuru62 26 Oct 2022, 12:13
Wow! Off grid sounds interesting (and strange).
Of course, you can ask anything about signed numbers and float points.
FPU is not super difficult, you can use (load, add, divide, etc.) integer values with FPU.

There is nice site describing the coding with FPU: http://www.ray.masmcode.com/tutorial/

It is old site, but all proven to be working.
Post 26 Oct 2022, 12:13
View user's profile Send private message Send e-mail Reply with quote
geekbasic@gmx.com



Joined: 25 Oct 2022
Posts: 29
Location: Arizona
geekbasic@gmx.com 07 Nov 2022, 01:02
Thanks for the link. It's way above my head, so I'm spending time trying to understand it. Are there any good simple examples? How would I make the example in this thread support floating points?

I am also hitting a brick wall on another issue. The expression parser. I have only written an expression parser for an interpreter which translates the infix to a postfix and evaluates it. I'm lost as to where and how to translate an infix to asm instructions as the postfix.

I'm also working on my interpreter project which is having it's own design challenges. For now, I have uploaded the projects to my site to share my progress and see if anyone has feedback. I'll post more threads about it.
Post 07 Nov 2022, 01:02
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.