【发布时间】:2016-03-09 17:50:44
【问题描述】:
我真的是低级编程的新手,使用 16 位和 4 个寄存器,但我正在尝试编写一个程序来检查是否从键盘输入了一个字符串,并以句号 (.) , 是一个回文。如果是则输出'y'到vdu,否则输出'n'。 但是,我遇到了问题,它似乎只输出'y',无论它是否是回文
mov bl,70 ; Memory Start Address
mov dl,0 ; how many characters make up the string
loop:
in 00 ;read input from the keyboard
cmp al, 2E ;check to see if the input is a fullstop
jz palin ;jump to see if the input is a palindrome
mov [bl], al ;save the input in memory address
inc bl ;goto next memory addr in bl
inc dl ;increment dl by 1 to the length of the string
push al
jmp loop
palin:
cmp dl,0 ;check if it has gone through the whole string
jz ispalin ;jump it has then the string is a palindrome
mov bl, 70 ;bring back the first input character
mov dl,[bl]
pop cl ;put the last input character
cmp cl,dl ;check if these two values are the same
jnz notpalin ;if they are not then jump to notpalin
inc bl ;go to the next input addr
dec dl ;take away 1 from the length of the string
jmp palin ;jump pack to the start of palin
notpalin:
mov dl,c0
mov cl,6E
mov [dl],cl ;print the character 'n' to the vdu
ispalin:
mov dl,c0
mov cl,79
mov [dl],cl ;print the character 'y' to the vdu
end
【问题讨论】:
-
16 位 x86 有 8 个寄存器,而不是 4 个。(其中 7 个或多或少是通用的)。 16bit 也比 32bit 复杂,所以比较难学。请参阅x86 tag wiki。
-
palin:之后的mov bl, 70需要退出循环,否则您将一直检查第一个字符。 -
当你说提升出循环时,我应该把它放在哪里?顺便谢谢你
标签: function assembly palindrome low-level 16-bit