So my assignment is to create V made out of asterisk(*) with each * having a random foreground and background color. Here is my code... I put a few breaks in and traced the program and somewhat figured out what the issue it. When I run it, it becomes an infinite loop because the backslash PROC
calls the color Procedure which overwrites the loop counter(ECX
register) and also overwrites the DH
/DL
registers used to move the cursors location. I am a beginner to assembly and could use a few hints or tips to avoid these problems in the future and fix it. Any help is appreciated, thanks in advance!
Assignment Guidelines - 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
Good job on determining what the problem is. When faced with this problem (since there is only one ECX register), you need the color proc to save the previous value, use it, then restore the previous value. You can do this with the push
and pop
instructions:
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
I have marked the added code with *****
.
For a given platform and operating system, there is something called an ABI which, among other things, states which registers are expected to be saved and restored by other functions you call. These are written down as rules that everybody follows so that code can be called between different compilers and languages without overwriting register values.