2014-04-17 38 views
0

我正在练习汇编语言,我想要做的是让它打印出每输入一个字符我输入的每个字符。问题是它只打印第一个和第六个字符。有什么我做错了吗?打印每五个字符

include irvine32.inc 
Title characters 
.data 
fort db "Enter here:",0 
.code 
main proc 
mov ecx,10 
mov edx, offset fort 
mov eax,0 
call crlf 
call writestring 
call crlf 
call crlf 
call readstring 
call crlf 
call crlf 
L1: 
mov al, [edx] 
add dx,5 
call writechar 
call crlf 
loop L1 
exit 
main endp 
end main 

回答

0

我的猜测是指令add dx,5应该是add edx,5。参考'dx'强制16位寄存器宽度,所以添加后的dx值将超出0xFFFFF。不是你想要的。根据edx中的初始指针值,这个滚动错误可能会很快发生。

0

1)主要问题叫做"Off by one error"。第一个字符位于索引位置0.索引位置5(0 + 5)是第六个字符。下一个索引位置是第十一个字符所在的10(5 + 5)。我想,你想显示索引4,9,14 ...,所以首先将EDX增加4,然后重复添加5。

2)欧文的ReadString写入最大10(ECX)字符[EDX]fort: “在这里输入:\ 0”)。没有反应,如果输入大于ECX则允许,写入的字符串只是被裁剪。输入“123456789”后,fort的内存看起来像“123456789 \ 0:\ 0”。只有一个“第五”字符,第十个字符是字符串终止的空字符。我建议为输入定义一个单独的变量,并有更多的空间。

3)LOOP也适用于ECX,将其在程序开始时设定为10并意外没有改变由函数WriteStringReadStringCrlf。因此,循环将重复10次,使存储器指针EDX增加5倍。它将读取远远超出由ReadString填充的空间的存储器。我建议创建一个无限循环(JMP而不是LOOP),并根据字符串长度设置单独的中断条件。

摘要:

include irvine32.inc 

.data 
fort db "Enter here:",0 
entered db 100 DUP (0)   ; Reserve space for 100 bytes and fill them with 0 
terminating dd OFFSET entered ; Pointer to the terminating null of the string 

.code 
main proc 
    mov edx, offset fort 
    call crlf 
    call WriteString 
    call Crlf 
    call Crlf 

    mov ecx,100     ; Maximal count of characters for ReadString 
    mov edx, offset entered  ; Pointer to string 
    call ReadString    ; Returnsin `EAX` the size of the input 
    add terminating, eax  ; Pointer to the terminating null of the input 
    call Crlf 
    call Crlf 


    mov edx, offset entered+4 ; Pointer to the fifth character of the string 
    L1: 
    cmp edx, terminating  ; Does it point beyond the string? 
    jae J1      ; Yes -> break the loop. 
    mov al, [edx] 
    add edx,5 
    call WriteChar 
    call Crlf 
    jmp L1      ; Endless loop 
    J1: 

    exit 
main endp 
end main