Click to See Complete Forum and Search --> : How do i read a line of text from the cmd prompt


ne0n82
November 2nd, 2008, 05:21 PM
Hello, im trying to read a line of text using the DOS interupt 21h, function code 0Ah and im having tons of problems, my level of assembly knowledge is limited im taking a beginner course on it in college at the moment.

i would be very grateful if someone could post a full example (including) creating the inital buffer and then filling the buffer, ill post the code im having trouble with now

BTW: this code is old because the book we are using is an old book so we are using MASM 615.


;***********************************
;* File: readline.asm
;* Author: James Martinez
;***********************************
include c:\masm615\programs\pcmac.inc

.model small
.386
.stack 500h

.data
;; Defines
cr equ 13
lf equ 10

;; Global Data
string DB 256 DUP(?) ;; This is my buffer to read into (i dont know if this is the right way to declare a buffer)
requestText DB 'Enter a line of text: ', '$'
newline DB cr, lf, '$'
.code

_start proc
_Begin

_PutStr requestText

;; from what i read about the dos call the first element in the buffer has to be the size of the string to read in
mov [string], 250

;; my attempt at using the dos call but it doesnt work
mov ax, @data
mov ds, ax
mov dx, OFFSET string
mov ah, 0Ah
int 21h

;; Try to just echo back what the user inputs
_PutStr string

_Exit 0
_start endp

end _start



again if you could please show me how to get this working it would be a lifesaver, THANKS! =)

bitshifter420
November 5th, 2008, 01:07 AM
http://www.ctyme.com/intr/int-21.htm

BTW, whats this do?mov ax, @data

iviggers
November 5th, 2008, 10:12 AM
mov ax,@data defines AX with the value of data segment. This is required exe files if you want access to variables in that segment.

S_M_A
November 5th, 2008, 05:39 PM
My memories of both DOS and assembly are very faint but shouldn't ds be loaded before the mov [string], 250 instruction?

iviggers
November 6th, 2008, 10:38 AM
Hi SMA:
You're right.

I just focused on bitshifter420's question (didn't pay much atention to the code), but your remark is correct.

ds should be defined before giving values to any of variables declared inside it. Otherwise service 0Ah of int 21h won't even request input.

dnapcex
December 6th, 2008, 03:54 AM
I hope following information can help you.

INT 21,A - Buffered Keyboard Input
AH = 0A
DS:DX = pointer to input buffer of the format:

| max | count | BUFFER (N bytes)
| | `------ input buffer
| `------------ number of characters returned (byte)
`-------------- maximum number of characters to read (byte)


returns nothing

- since strings can be pre-loaded, it is recommended that the
default string be terminated with a CR
- N bytes of data are read from STDIN into buffer+2
- max buffer size is 255, minimum buffer size is 1 byte
- chars up to and including a CR are placed into the buffer
beginning at byte 2; Byte 1 returns the number of chars
placed into the buffer (extended codes take 2 characters)
- DOS editing keys are active during this call