2017-02-21 58 views
3

所以我的任務是創建V由星號(*)製成,每個*有一個隨機的前景色和背景色。這裏是我的代碼...我放了幾個休息時間,並追溯了程序,並有點想出了它的問題。當我運行它時,它變成一個無限循環,因爲反斜槓PROC調用覆蓋循環計數器(ECX寄存器)的顏色過程,並且還覆蓋用於移動光標位置的寄存器DH/DL。我是一名初學者,可以使用一些提示或技巧來避免將來出現這些問題並加以解決。任何幫助表示感謝,提前致謝!裝配中的意外死循環

分配指導方針 - https://docs.google.com/document/d/1iPqfTd0qNOQo_xubVvsZLqfeNDog8mK6kzGGrR6s-OY/edit?usp=sharing

; main.asm - Assembly language source file 
; Author:  Dekota Brown 
; Date:    2/21/2017 
; Description: Colorful V-Pattern 

INCLUDE Irvine32.inc     ; Irvine's assembly library 
ExitProcess PROTO,dwExitCode:DWORD  ; MS Windows ExitProcess function 

.data 
    nullVar DWORD ? 
    msgEnd BYTE "Is the program running as you thought?",0 
    msgEndCaption BYTE "Program Exit...",0 
    symbol BYTE '*',0 
.code 
main PROC        ; main procedure, entry point 

mov EAX, nullVar 
mov EBX, nullVar 
mov ECX, nullVar 
mov EDX, nullVar 

call backslash 

mov EDX,OFFSET msgEnd 
mov EBX,OFFSET msgEndCaption 
call MsgBoxAsk 


mov EAX,07 
call SetTextColor 
call CrLf 
call WaitMsg 

INVOKE ExitProcess,0    ; end the program 

main ENDP 

color PROC 

    call Randomize ; Seed the RNG 
    mov ECX,20 ; Set up loop counter 
L1: 
    mov EAX, 256 
    call RandomRange 
    call SetTextColor 
    mov EDX,OFFSET symbol 
    call WriteString 
loop L1 

    ret 
color ENDP 

backslash PROC 

    mov dl, 2 ; Row 2 
    mov dh, 4 ; Column 4 
    mov ECX,20 ; Sets up loop counter 
L2: 
    call color 
    call CrLf 
    add dh,1 ; Increments column or shifts right by 1 position 
loop L2 

    ret 
backslash ENDP 

forwardslash PROC 

    ret 
forwardslash ENDP 

END 

回答

3

上確定的問題是什麼好工作。當面對這個問題時(因爲只有一個ECX寄存器),你需要顏色過程來保存以前的值,使用它,然後恢復以前的值。你可以用pushpop說明這樣做:

color PROC 
    push ecx ; ***** save previous value 
    call Randomize ; Seed the RNG 
    mov ECX,20 ; Set up loop counter 
L1: 
    mov EAX, 256 
    call RandomRange 
    call SetTextColor 
    mov EDX,OFFSET symbol 
    call WriteString 
loop L1 
    pop ecx ; ***** restore previous value 
    ret 
color ENDP 

我與*****標明添加的代碼。

對於一個給定的平臺和操作系統,有一個叫做ABI的東西,其中包括哪些寄存器可以被其他調用的函數保存和恢復。這些都是作爲每個人遵循的規則寫下來的,以便可以在不覆蓋寄存器值的情況下在不同編譯器和語言之間調用代碼。