1

What I want to do is to read a string from the keyboard and output that same string. However, using the code below in TASM, I get only gibberish:

UPDATED

DATA SEGMENT PARA PUBLIC 'DATA'
MSG DB 10,0,80 dup(?) ; variable to hold string
DATA ENDS
CODE SEGMENT PARA PUBLIC 'CODE'
START PROC FAR
ASSUME CS:CODE, DS:DATA
PUSH DS
XOR AX, AX
PUSH AX
MOV AX,DATA
MOV DS, AX
MOV AH, 0AH
MOV DX, OFFSET MSG
INT 21H ; read string
MOV AH, 09H
INT 21H ;output string
RET
START ENDP
CODE ENDS
END START

Now I get the chance to enter input but the result is gibberish. Where am I wrong?

Vlad Silviu Farcas
  • 177
  • 1
  • 3
  • 15
  • You may wish to see this SO answer: http://stackoverflow.com/a/13206675/3857942 that is for a similar question. – Michael Petch Jan 03 '16 at 20:32
  • 1
    Did you read the description for the [description for the DOS `int 21h` command `0Ah`](http://spike.scu.edu.au/~barry/interrupts.html#ah0a)? According to that, the first byte of that buffer should contain the max buffer length, and the next two bytes pertain to number of characters read. If you are outputting that as a string, it's going to be jibberish. And it appears you only have set aside one byte for your `MSG` buffer: `MSG DB ''`). That's going to be trouble. – lurker Jan 04 '16 at 17:32
  • @lurker, thanks for your advice, I modified the MSG buffer: 'MSG DB 10,0,80 dup(?)' but still I don't get my string. – Vlad Silviu Farcas Jan 05 '16 at 13:49
  • 1
    Did you read the documentation for command `0Ah`? Your new `MSG` definition says you plan to read no more than 10 characters. And then after the read, did you check the 3rd byte to determine how many bytes were actually read? Then when you print the buffer, you can't print those first 3 bytes as your code currently shows. – lurker Jan 05 '16 at 14:12
  • 1
    Subfunction 09H requires a `$` character to terminate the string, not a null. Without that `$`, it will simply write the random contents of uninitialised memory until it reaches a `$` - and it won't output the `$` iy finds, either. – Magoo Jan 05 '16 at 14:24

1 Answers1

1

Thanks to all comments, I managed to write the program which reads and displays a string:

DATA SEGMENT PARA PUBLIC 'DATA'
MAXLEN DB 20
LEN DB 0
MSG DB 20 DUP(?)
DATA ENDS
CODE SEGMENT PARA PUBLIC 'CODE'
START PROC FAR
ASSUME CS:CODE, DS:DATA
PUSH DS
XOR AX, AX
PUSH AX
MOV AX,DATA
MOV DS, AX
MOV AH, 0AH
MOV DX, OFFSET MAXLEN
INT 21H
MOV DL, 10
MOV AH, 02H
INT 21H ;NEW LINE FEED
MOV AL, LEN
CBW ; EXTEND AL TO AX
MOV SI, AX
MOV MSG+SI, '$'
MOV AH, 09H
MOV DX, OFFSET MSG
INT 21H
RET
START ENDP
CODE ENDS
END START
Vlad Silviu Farcas
  • 177
  • 1
  • 3
  • 15