2010-09-27 124 views
3

以下程序的要點是用每種背景和前景色的組合打印出字母「c」。Irvine's WriteString的奇怪輸出

在我使用的顏色庫中定義0-15和下面的代碼:

mov eax,FOREGROUND + (BACKGROUND * 16) 
call SetTextColor 

這裏是我的代碼:

INCLUDE Irvine32.inc 
.data 

character BYTE "c" 
count DWORD ? 
background DWORD 0 

.code 
main PROC 
    call Clrscr 

    mov ecx, 15        ; our main counter 0-15 colors 

L1:  
    mov count, ecx       ; store our outer loop counter 
    mov ecx, 15        ; set out inner loop counter 
L2:  
    ; since our color is defined like so... mov eax,FOREGROUND + (BACKGROUND * 16) 
    mov eax, count       ; setup our foreground color 
    add eax, background      ; setup our background color 
    call SetTextColor 

    ; instead of multiplying each background color by 16, we are going to 
    ; add 16 each time. 
    add background, 16      

    ; print the character 
    mov edx, OFFSET character 
    call WriteString 
    loop L2 

    mov ecx, count       ; reset our outside loop 
    loop L1 

    call Crlf 
    exit 
main ENDP 

END main 

現在,我使用Windows 7,上面的代碼「有效」,但由於某種原因,它到了某個程度,程序停止,計算機開始發出嗶嗶聲。此外,在程序的某一點,它開始打印隨機字符以字母C ..這裏是我的輸出:

c♀c♀c♀c♀c♀c♀c♀c♀c♀c♀c♀c♀c♀c♀c♀c♂c♂c♂c♂c♂c♂c♂c♂c♂c♂c♂c♂c♂c♂c♂c 
c 
c 
c 
c 
c 
c 
c 
c 
c 
c 
c 
c 
c 
c 
c  c  c  c  c  c  c  c  c  c 
c  c  c  c  c  cccccccccccccccc♠c♠c♠c♠c♠c♠c♠c♠c♠c♠c♠c♠c 
♠c♠c♠c♣c♣c♣c♣c♣c♣c♣c♣c♣c♣c♣c♣c♣c♣c♣c♦c♦c♦c♦c♦c♦c♦c♦c♦c♦c♦c♦c♦c♦c♦c♥c♥c♥c♥c♥c♥c♥c 
♥c♥c♥c♥c♥c♥c♥c♥c☻c☻c☻c☻c☻c☻c☻c☻c☻c☻c☻c☻c☻c☻c☻c☺c☺c☺c☺c☺c☺c☺c☺c☺c☺c☺c☺c☺c☺c☺ 
Press any key to continue . . . 

誰能告訴我爲什麼發生這種情況?

+1

我沒有看到一個'.model'指令,你也不會遞增/遞減循環的任何計數器。你有沒有遺漏代碼示例? – slugster 2010-09-27 03:33:25

+0

@slugster:循環指令遞減[E] CX,將結果與0進行比較,如果它不爲零,則跳轉到指定的標籤。據猜測,他使用的包含文件設置了內存模型。 – 2010-09-27 03:40:39

+0

是的,這是正確的。 irvine圖書館爲我們貶低它。它是計算機體系結構課程,而不是MASM課程。 – 2010-09-27 15:00:55

回答

2
character BYTE "c" 

應該是:

character BYTE "c",0dh,0ah,0 
0

是什麼WriteString辦?如果該函數打印一個字符串,可能需要用$(如果是DOS程序09函數Int21h)結束「字符BYTE」c「」

3

Irvine的WriteString需要一個「以null結尾的字符串」。有些人可以將幫助下載爲CHM文件here (IrvineLibHelp.exe)

說「EDX =指向字符串」有點草率。 EDX只是指向一個標籤可識別的內存地址(這裏是「字符」)。 WriteString將從該位置獲取字節的字節,並將其寫入字符或控制指令,而不管其實際類型或意圖如何,直到遇到值爲0的字節爲止。MASM沒有指令來定義具有最後0的字符串,因此它必須手動添加:

character BYTE "c", 0 

打印字符的另一種方法是使用WriteChar

... 
; print the character 
mov al, character 
call WriteChar 
loop L2 

mov ecx, count       ; reset our outside loop 
loop L1 
...