【发布时间】:2013-10-15 06:43:20
【问题描述】:
代码编译得很好 [NASM]
但是一旦我输入我的第一个值,它就会崩溃
我不知道怎么回事,目标是输入一个字符串并输出字符串的反转,如果用户说是('Y'或'y'),则一直循环重复
**.DATA
; Initialized Data Definitions
strlength EQU 40
PromptStr dd "Please input a string (must be less than 40 characters long): ", 0
OutputStr dd "The reverse string is: ", 0
AgainStr dd "Would you like ot try again? ('Y' or 'y' for yes): ", 0
.UDATA
; Uninitialized Data Definitions
string resb strlength
.CODE
; Program Code
.STARTUP
nwln ; start output at a new line
PutStr PromptStr
nwln
while:
GetStr string
mov EBX, string
loop_in:
push dword[EBX]
add EBX, 4
cmp dword[EBX], 0
jnz loop_in
loop_out:
XOR EBX, EBX
pop EBX
PutCh [EBX]
cmp dword[EBX], 0
jnz loop_out
nwln
PutStr AgainStr
GetStr EBX
mov AL, [EBX]
cmp AL, 'Y'
jz while
cmp AL, 'y'
jz while
Pause
.EXIT**
我把第一个循环改成
loop_in:
mov AL, [EBX]
push byte[AL]
add EBX, 4
cmp byte[AL], 0
jnz loop_in
我得到这个错误“错误:无效的有效地址”
当我改为“字节”时
loop_in:
push byte[EBX]
add EBX, 4
cmp byte[EBX], 0
jnz loop_in
我得到“错误:操作码和操作数的无效组合”
对于 {add EBX, 4} 行
所以我改变了
loop_in:
push EBX
inc EBX
cmp byte[EBX], 0
jnz loop_in
loop_out:
XOR EBX, EBX
pop EBX
PutCh [EBX]
cmp byte[EBX], 0
jnz loop_out
现在它可以编译了,我已经走到这一步了
Please input a string (must be less than 40 characters long):
asdf
fdsaêë
在它崩溃到 Windows 之前
【问题讨论】:
-
使用调试器,通过单步执行机器指令。
PutStr不是汇编指令。 -
然后显示宏文件或至少链接到它。
-
为什么要对字符使用
dword操作? ASCII 字符通常存储为字节。 -
会不会出错?
-
我尝试更改它并且我得到“错误:操作码和操作数的无效组合”