2014-04-17 108 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