oldmanshola
oldmanshola

Reputation: 21

Assembly program not printing read string

I wrote the following code that allows the user to input a number and have it printed back

.model tiny
.stack 100
.data
    userMsg db 13,10, 'Please enter a number: $'
    dispMsg db 13,10, 'You have entered: $'
    num dw dup 5(?)
.code
mov ax,@data
mov ds,ax

lea dx,userMsg
mov ah,9
int 21h

mov ah,0ah
lea dx,num
int 21h

lea dx,dispMsg
mov ah,9
int 21h

lea dx,num
mov ah,9
int 21h

mov ah,4ch
int 21h

However it does not print the user's number but shows the following error:

INT 21h, AH=09h
address: 07131
byte 24h not found after 2000 bytes.
; correct example of INT 21h/9h:
mov dx, offset msg
mov ah,9

What exactly am I doing wrong?

Upvotes: 1

Views: 258

Answers (1)

Sep Roland
Sep Roland

Reputation: 39676

num dw dup 5(?)

This is a peculiar syntax. Does this assemble at all?
The usual way to write this is num dw 5 dup (?), so putting the repetition count before the dup operator.

About the problem with printing the user's number for which you say in a comment:

Thanks I've added the $ and it's printing now

I seriously doubt that what gets printed is the inputted number and nothing else, because you're using both DOS function 09h and DOS function 0Ah wrongly!

The DOS.BufferedInput function 0Ah expects to get from you a pointer in DS:DX that points at a structure with a clearly defined layout.
The 1st byte must specify how big is the storage space that starts with the 3rd byte.
The 2nd byte is for DOS to inform you about how many bytes were inputted.
For more on this DOS function, read How buffered input works.

Next is an example that will allow inputting 5 characters. Why, will you ask, do I have to write 6 then? Well, DOS always appends a carriage return byte (13) to the inputted characters. Your count in the 1st byte must allow for this.

num     db      6, 0, 6 dup (0)

Since the actual characters start at num + 2, that will be the address that you need to pass to the DOS.PrintString function 09h.

byte 24h not found after 2000 bytes.

And then there's the small matter of $-terminating the characters. Simply replace the carriage return byte (13) by a $ character. The ASCII code for the $ character is 24h (36).

xor     bx, bx
mov     bl, [num+1]             ; The count obtained from DOS
mov     BYTE PTR [num+2+bx], '$'
mov     dx, OFFSET num+2
mov     ah, 09h
int     21h

before

num     6, 0, 0, 0, 0, 0, 0, 0,

input

2019

after

              '2' '0' '1' '9' CR
num     6, 4, 50, 48, 49, 57, 13, 0

printing

              '2' '0' '1' '9' '$'
num     6, 4, 50, 48, 49, 57, 36, 0
              ^
              DX

Upvotes: 3

Related Questions