3

下面程序的重点是打印出字母“c”,并结合了每个背景和前景色。

在我使用的库中,颜色定义为 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 . . .

谁能告诉我为什么会这样?

4

3 回答 3

3

Irvine 的WriteString需要一个“以空字符结尾的字符串”。有些人可以在此处 (IrvineLibHelp.exe)将帮助下载为 CHM 文件。

说“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
...
于 2015-12-15T17:33:20.547 回答
2
character BYTE "c"

应该:

character BYTE "c",0dh,0ah,0
于 2013-03-25T20:06:38.890 回答
0

WriteString 是做什么的?如果函数打印一个字符串,你可能需要用 $ 结束 "character BYTE "c" " (如果是 DOS 程序。09 function Int21h)

于 2013-09-01T12:38:17.703 回答