flat assembler
Message board for the users of flat assembler.

Index > DOS > User input?

Author
Thread Post new topic Reply to topic
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
I spent the last few hours trying to figure out how to get user's input. But I was unsuccessful. So I took a look at some code from "console fun" by Bitshifter. The coding was brilliant (from my perspective). I tried to duplicate the code in my way, but it didn't work. Please tell the errors in the code:
Let's say the user enters: "MeshNix"
The program would output: "Hi MeshNix!!!" the go on a new line, then prompt again.
The virtual keys such as backspace don't work.
And also when the enter key is pressed, the prompt comes back on the same line instead of a newline
Code:
org 100h

jmp Start

buffer   rb 16
length   db 0
maxlen   db 15
startmsg db 'Enter something here: ',0
nextmsg1 db 'Hi ',0
nextmsg2 db '!!!',0
newline  db 13,10,0

Start:
    ;set to graphic mode 3
    mov al,03h
    mov ah,0
    int 10h
   .reset:
     ;reset prompt buffer
     mov [buffer],0     ;set buffer to 0
     mov [length],0     ;set length to 0
     mov di,buffer

     ;print prompt
     mov si, startmsg
     call PrintString
    .input:
     ;wait for keystroke
     xor ax,ax
     int 16h

     ;handle special keys
     cmp al,1Bh    ;VK_ESCAPE
     je .vk_escape
     cmp al,08H    ;VK_BACK
     je .vk_back
     cmp al,1DH    ;VK_RETURN
     je .vk_return

     ;is buffer full
     mov ah,[length]
     cmp ah,[maxlen]
     je .input

     ;append character to buffer
     mov byte[di],al
     inc di
     mov byte[di],0
     inc [length]

     ;print character
     call PrintChar
     jmp .input

    .vk_escape:
     ;exit program
     ret

    .vk_back:
     ;remove character from buffer
     dec di          ;decrease buffer pointer
     mov byte[di],0  ;insert NULL terminator
     dec [length]    ;decrement rompt length
     jmp .input

    .vk_return:
     ;check form empty command buffer
     cmp [length],0
     je .input
     call ProcessInput
     jmp .reset

;Call procedures
     PrintString:
     jmp GetChar     ;skip echo - no character in al as yet
       EchoChar:
       mov ah,0Eh
       int 10h
     ;Get character from string
       GetChar:
       mov al,[si]   ;extract character from string
       inc si        ;step up on stack (skip last byte)
       or al,al      ;is string complete printer (is al =0)
       jz .finish    ;if so end procedure
       jmp EchoChar  ;else repeat process
    ;return to call address
    .finish:
     ret

    ;print character
     PrintChar:
     mov ah,0Eh
     int 10h
     ret             ;return to call address
    ;process input from user
     ProcessInput:

     inc si
     ;print nextmsg1
     mov si,nextmsg1
     call PrintString
     inc si
     ;print data in buffer
     mov si,buffer
     call PrintString
     inc si
     ;print nextmsg2
     mov si,nextmsg2
     call PrintString
     inc si
     ret
    


Help me please!!! Very Happy

_________________
meshnix
Post 23 Feb 2010, 16:06
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
Hi MeshNix,

The INT 21h / AH=0Ah interrupt provides a simple way to read user input. Idea
See the last post by daluca here
Post 23 Feb 2010, 18:31
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
This may be the problem:
Code:
cmp al,1DH    ;VK_RETURN    

It should be
Code:
cmp al,0DH   ;VK_RETURN    
Post 23 Feb 2010, 19:41
View user's profile Send private message Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
Thanks, a lot guys.
Bitshifter, I change the virtual key value to 0Dh instead of 1Dh and it worked, but it still prints the text on the the same line. What is the problem?

Picnic, I checked the post and i'm kinda studying it to see how it worked, but I don't think that's the kind of code I want. I like Bitshifter's code because it was (what I would call) raw.

Thanks again guys
Help me if you could...

_________________
meshnix
Post 23 Feb 2010, 19:59
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
You need to specify newlines...
Change your code to this and its fixed!
Code:
nextmsg1 db 13,10,'Hi ',0
nextmsg2 db '!!!',13,10,0    
Post 23 Feb 2010, 20:09
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
MeshNix wrote:
I like Bitshifter's code because it was (what I would call) raw.
I welcome your choice, just highlight this simple mechanism. Smile
Post 23 Feb 2010, 20:43
View user's profile Send private message Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
Thanks a lot
...
Post 24 Feb 2010, 23:05
View user's profile Send private message 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.

Powered by rwasa.