Take several inputs and print 'em

39 Views Asked by At

This is my program:

data segment

str1 db "What is your name: $"   
str2 db "How old are you? $"
str3 db 13,10, "Name Output is: $"
str4 db 13,10, "Age Output is: $"

Arr db 10 dup('$')
Arr2 db 10 dup('$') 
ends

stack segment
     dw 128 dup(0)    
ends

code segment
start:

mov ax, @data
mov ds, ax   

lea dx, str1
mov ah, 09h
int 21h

mov ah, 10  ; string input sub-rountine
lea dx, Arr ; get offset address of array
mov arr,10
int 21h
 
mov dx,13
mov ah,2
int 21h  
mov dx,10
mov ah,2
int 21h
 
lea dx, str2
mov ah, 09h
int 21h 

mov ah, 10
lea dx, Arr2
mov arr, 11
int 21h


lea dx, str3
mov ah, 09h
int 21h 

mov ch, 0 
mov cl, Arr[1] ;cl = counter of character
mov si, 2
mov ah, 02h
output:
mov dl,Arr[si]
int 21h
inc si
mov dl,' '
loop output 

mov ax, 4c00h
int 21h

It is successful in printing out the name but the age cannot print out because its input is kinda hard.

I want to input the name, age, the country and make its output printable in every code.

1

There are 1 best solutions below

1
Sep Roland On

Please do read How buffered input works so you learn how to set up correctly for the DOS.BufferedInput function 0Ah.
Next could apply to your case (your own (full) name has well over 10 characters):

Arr1 db 30, 0, 30 dup(0)    ; This has room for 29 characters
Arr2 db 3, 0, 3 dup(0)      ; This has room for 2 decimal digits (ages up to 99)

You say that the input for the age is "kinda hard". Well I see that you have a mix-up between the Arr and Arr2 buffers. That number 11 should go to the Arr2 buffer. Moreover the value 11 is wrong for a buffer that only comprises 10 bytes!

mov ah, 10
lea dx, Arr2
mov arr, 11      <<<<< Should go to Arr2
int 21h

Don't do redundant work. The carriage return plus linefeed, that you now do following the input, does not need the carriage return at all. The cursor will already have been placed at the beginning of the line. Only the linefeed is necessary and you should use DL for it (not DX):

mov  dl, 10
mov  ah, 02h
int  21h

And you wouldn't even need to add these lines, if only you would store the matching control codes in your string definitions:

str1 db "What is your name? $"
str2 db 10, "How old are you? $"
str3 db 10, 10, "Name Output is: $"
str4 db 13, 10, "Age Output is: $"

mov dl,' '            <<<<< ???
loop output 

The output loop redundantly moves a space character into DL right before the loop instruction. Just remove that line.

Summary

Next code uses the above suggested Arr1 and Arr2 buffers, and the modification to the text strings. If these buffers are each used only once then the lines that I have marked with an asterisk (*) can be omited.

mov  dx, OFFSET str1       ; `lea dx, str1` has a 1 byte longer encoding, so prefer
mov  ah, 09h               ; to write `mov dx, offset str1` (less is generally better)
int  21h
mov  dx, OFFSET Arr1
mov  word ptr [Arr1], 30     (*)
mov  ah, 0Ah
int  21h
 
mov  dx, OFFSET str2
mov  ah, 09h
int  21h
mov  dx, OFFSET Arr2
mov  word ptr [Arr2], 3      (*)
mov  ah, 0Ah
int  21h

mov  dx, OFFSET str3
mov  ah, 09h
int  21h 
mov  si, 2
mov  ah, 02h
output1:
mov  dl, Arr1[si]
int  21h
inc  si
cmp  dl, 13
jne  output1

mov  dx, OFFSET str4
mov  ah, 09h
int  21h 
mov  si, OFFSET Arr2 + 2
mov  ah, 02h
output2:
mov  dl, [si]
int  21h
inc  si
cmp  dl, 13
jne  output2

Before you blindly copy/paste this, do observe the different ways of addressing the arrays. And did you notice that I did not use the length of the string as it was returned to us by DOS? Instead, I based my loops on the mandatory carriage return that DOS adds to our characters in the input buffer.