问题描述
我是汇编语言的新手.我正在尝试从以结尾的用户那里获取一串数字,或者该字符串的长度达到20.执行该程序时,它没有显示任何错误,但也没有显示任何输出字符串超过20个字符的限制时也不会终止.
I am newbie in assembly language. I am trying to get a string of numbers from user terminated by or the length of the string reaching 20. When I executed the program it didn't show any error, but neither did it show any output nor did it terminate when the string exceeded the 20 characters limit.
我的代码是:
.model small
.stack 100h
.data
var1 db 100 dup('$')
.code
main proc
mov ax, @data
mov dx, ax
mov si, offset var1
l1:
mov ah, 1
int 21h
cmp al,20
je programend
mov [si], al
inc si
jmp l1
programend:
mov dx,offset var1
mov ah,9
int 21h
mov ah, 4ch
int 21h
main endp
end main
推荐答案
mov ax, @data
mov dx, ax
您想在此处初始化 DS
段寄存器,但错误地编写了 DX
.老实的错字,但是您的代码将以这种方式破坏程序段前缀.
You want to initialize the DS
segment register here but have erroneously written DX
. Honest typo, but your code will have corrupted the Program Segment Prefix in this manner.
很明显,您需要循环执行此操作,并且您将需要进行2次测试才能确定何时停止!
It's clear that you need a loop to do this and that you will need 2 tests to decide about when to stop!
- 测试
AL
中的字符是否为13 - 测试一个计数器(例如
CX
)以查看其是否达到20
- test the character in
AL
to see if it is 13 - test a counter (e.g.
CX
) to see if it reached 20
xor cx, cx ; Empty counter
mov si, offset var1
TheLoop:
mov ah, 01h ; DOS.GetCharacter
int 21h ; -> AL
cmp al, 13
je programend
mov [si], al
inc si
inc cx
cmp cx, 20
jb TheLoop
programend:
但是等等,任务不是说它必须是一串数字吗?您需要确保输入确实是数字.
数字"0"表示至"9"ASCII码从48到57.
But wait, didn't the task say that it had to be a string of numbers? You need to make sure that the input is indeed a number.
Numbers "0" through "9" have ASCII codes 48 through 57.
xor cx, cx ; Empty counter
mov si, offset var1
TheLoop:
mov ah, 01h ; DOS.GetCharacter
int 21h ; -> AL
cmp al, 13
je programend
cmp al, 48
jb TheLoop ; Not a number
cmp al, 57
ja TheLoop ; Not a number
mov [si], al
inc si
inc cx
cmp cx, 20
jb TheLoop
programend:
无需使用单独的计数器,也无需使用汇编程序将字符转换为代码的能力:
Without using a separate counter and using the assembler's ability to translate characters into codes:
mov si, offset var1
TheLoop:
mov ah, 01h ; DOS.GetCharacter
int 21h ; -> AL
cmp al, 13
je programend
cmp al, "0"
jb TheLoop ; Not a number
cmp al, "9"
ja TheLoop ; Not a number
mov [si], al
inc si
cmp si, offset var1 + 20
jb TheLoop
programend:
这篇关于无法获得汇编语言代码的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!