2

I am new to assembly language. so started writing small programs. I have written a basic loop program to print "*" pyramid. but the program goes into infinite loop. I am pasting the code below. can someone please help? start:

   mov ecx,2
   invoke StdOut, addr startProg

label_1:

   .while ecx > 0

   push ecx
       pop aInt

     .while aInt > 0
       invoke StdOut, addr star
       sub aInt, 1
     .endw

        dec ecx
    .endw

     ;invoke StdOut, addr newline


   jmp out_
out_:
   invoke ExitProcess, 0  

end start

1
  • Your going to need a bit more than that for a pyramid. Here - dreamincode.net/forums/topic/… I describe in steps how to create a pyramid. Commented Jan 28, 2013 at 0:31

3 Answers 3

3

Invoke calls the method via the __stdcall calling convention. Part of that convention is that EAX, ECX and EDX are not preserved over that call. This is why your ECX and EAX registers are not decrementing and causing the loop to stop.

Sign up to request clarification or add additional context in comments.

2 Comments

you mean If I use local variables instead of these registers. shall that work?
I was SO frustrated with these infinite loop i was getting all the time, i was about pull ever hair in my head out! And the answer was this simple... Iv made some nice p&p functions now THANKS +1
1

Like @SecurityMatt stated, the reason you get trapped in a infinite loop is because the value of ecx is modified in your call to StdOut.
You can avoid this by preserving your registers using push and then restoring them using pop:

.while ecx > 0
   push ecx
   pop aInt
   ; preserve state of `ecx`
   push ecx
   .while aInt > 0
     invoke StdOut, addr star
     sub aInt, 1
   .endw
   ; restore ecx
   pop ecx
   dec ecx
.endw

You could also use pushad and popad in order to push/pop all general-purpose register values on and off the stack.

; push general-purpose registers values onto stack
pushad
invoke StdOut, addr star
; restore general-purpose registers
popad

Comments

0

You are probably confusing assembly instructions with macros. The .while is not an assembly instruction, it is a macro. Same for all directives that begin with '.'

1 Comment

If only assembly was that easy :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.