接受多个输入并打印它们

问题描述 投票:0回答:1

这是我的程序:

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

成功打印出姓名,但年龄打印不出来,因为输入有点困难。

我想输入姓名、年龄、国家,并使其输出可在每个代码中打印。

assembly input x86-16 emu8086
1个回答
0
投票

请阅读缓冲输入如何工作,以便您了解如何正确设置 DOS.BufferedInput 函数 0Ah。
接下来可能适用于您的情况(您自己的名字远超过 10 个字符):

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)

您说age的输入“有点困难”。嗯,我发现您混淆了 ArrArr2 缓冲区。数字 11 应该进入 arr2 缓冲区。此外,对于仅包含 10 个字节的缓冲区,值 11 是错误的!

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

不要做多余的工作。输入后的回车加换行根本不需要回车。光标已经放置在行的开头。只需要换行符,您应该使用 DL(而不是 DX):

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

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

输出循环在

loop
指令之前将一个空格字符冗余地移动到 DL 中。只需删除该行即可。


接下来的代码使用上面建议的缓冲区Arr1Arr2。如果这些缓冲区仅使用一次,则可以省略我用星号 (*) 标记的行。

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  dl, 10
mov  ah, 02h
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  dl, 10
mov  ah, 02h
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

在盲目复制/粘贴之前,请注意寻址数组的不同方式。您是否看到我没有使用 DOS 返回给我们的字符串长度?相反,我的循环基于 DOS 添加到输入缓冲区中的字符的强制回车符。

© www.soinside.com 2019 - 2024. All rights reserved.