[英]Accidental infinite loop in Assembly
所以我的任務是用星號(*)創建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
確定問題所在的好工作。 當遇到這個問題時(因為只有一個ECX寄存器),你需要使用顏色proc保存以前的值,使用它,然后恢復以前的值。 您可以使用push
和pop
說明執行此操作:
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的東西,除其他外,它指出哪些寄存器應該被你調用的其他函數保存和恢復。 這些都記錄為每個人都遵循的規則,以便可以在不同的編譯器和語言之間調用代碼而不覆蓋寄存器值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.