Advertisement Jump to content
Sign in to follow this  
l jsym l

Problem with my Assembly Program

This topic is 2987 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I'm writing a program that deals with the stack. All I'm trying to do is call a Procedure (WriteColorChar) that simply calls another procedure (SetColor), which just sets the forecolor and backcolor of the program.


CR EQU 0Dh ; ASCII Carriage Return Character
LF EQU 0Ah ; ASCII Line Feed Character
EOS EQU 0h ; Zero-terminated


; parameters to be passed.
sFore DWORD white ; SetColor sFore (parameter)
sBack DWORD blue ; SetColor sBack (parameter)
wChar DWORD " " ; WriteColorChar wChar (parameter)

L1temp DWORD ? ; temp loop counter for L1
L2temp DWORD ? ; temp loop counter for L2
L3temp DWORD 1 ; temp loop counter for L3

main PROC

mov eax, sFore ; move sFore into eax reg. for color manipulation
mov ebx, sBack ; move sBack into ebx reg. for color manipulation
mov ecx, wChar ; move wChar into ecx reg. for WriteChar call

push eax ; push sFore onto the stack
push ebx ; push sBack onto the stack
push ecx
call WriteColorChar

mov edx, OFFSET all ; print a blank line
call WriteString ; call WriteString

main ENDP
SetColor PROC

; This procedure calls the SetTextColor procedure
; from the Irvine32 Library to set the color of the text

push ebp ; Save calling procedure base pointer
mov ebp, esp ; Set base pointer for this procedure

mov ecx, [ebp + 16] ; Retrieve colors
mov eax, [ebp + 12] ; from the stack

mov ebx, 16 ; foreColor + (backColor * 16 )
mul ebx ;
add ecx, eax ;

mov eax, ecx ; move ecx reg. into eax re.
call SetTextColor ; call SetTextColor to set the fore and back colors

mov eax, [ ebp + 8 ] ; retreive the char from the stack

pop ebp ; Restore base pointer for calling procedure

SetColor ENDP
WriteColorChar PROC
; This program calls the SetColor Procedure to set the color of the vertical bars
; that are to be printed in the testing code.



pop ebp ; pop the ebp from the stack
pop ecx ; pop the ecx reg. from the stack
pop ebx ; pop the ebx reg. from the stack
pop eax ; pop the eax reg. from the stack

push eax ; push sFore onto the stack
push ebx ; push sBack onto the stack
push ecx ; push wChar onto the stack

call SetColor ; call SetColor
call WriteChar ; call WriteChar

WriteColorChar ENDP
END main

The Problem that I am running into is that when calling the WriteColorChar Procedure, the program wont return into the main procedure.

I know that the SetColor procedure works and that it gets back into the WriteColorChar proc, however, cannot return to the main from there.

I was just wondering if anyone knew what I am doing wrong. I understand that it is probably just a stack variable I'm messing up by not popping it off or popping it off when not suppose.

If anyone has any idea it would be greatly appreciated....thanks!

Share this post

Link to post
Share on other sites
What calling convention are you using?
Looks strange with how you pass variables, especially all those pop at the beginning of WriteColorChar. And the functions you call that aren't in the code you posted, are those system functions or have you written them yourself too?

Share this post

Link to post
Share on other sites
all the calls im calling are included in a file called Irvine32..
I forgot to mention that and its my bad.

while popping off the stack in WriteColorChar im not really sure if im doing it right. I just did that because I figured it would be like passing a function parameters...

If you need any other information just let me know..

Share this post

Link to post
Share on other sites
Is this for a class?
There should be documentation for those functions, and also you should read up on how the stack works and calling conventions. If you look at the SetColor procedure you see that it starts with push ebp, mov ebp, esp and ends with pop ebp, ret, and reads two variables from the stack (ebp+12 and ebp+16). You probably want to write your WriteColorChar the same way.
As you have it now you don't reserve any stack space, and those registers you use aren't preserved.

The main thing you need to think of is where you pass data to a new function, and who is responsible for popping bytes pushed on the stack. If you skip the whole push/pop thing and just pass things in the registers it's easier, but if you call functions written by others, you must know their calling conventions, and make sure to save registers that aren't preserved when you call a function.

Share this post

Link to post
Share on other sites
Its for a class yes, however not graded. Our previous assignment was just the SetColor Proc.

I wanted to see if I could figure out how to call one procedure within another procedure and then I ran into this. I guess I just get confused why I cant just push the registers onto the stack and then call the SetColor Procedure.

Share this post

Link to post
Share on other sites
Try without the pops. Or at least without pop ebp, the others should probably be there..
Do you know how you're supposed to pass arguments to the functions in Irvine32?
It seems you're passing arguments in eax..

Share this post

Link to post
Share on other sites
Well not completely sure I guess. Its the authors library file so not sure..

I just tried commenting out everything in WriteColorChar and the program still crashed when trying to go back into the main procedure. So i know it has to be something with WriteColorChar that I'm missing but not sure what..

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!