0

I am very new to assembly and I just picked up some online tutorials about it so I'm trying to practice.

Here's the snippet of a code which makes a textbox appear:


segment .data 
 TITLE db 'Message',0 
 TEXT dd 'This is a message',0 
segment .text 
global  start 
extern MessageBoxA 
extern ExitProcess 
start: 
      push 0 
      push TITLE 
      push TEXT
      push 0 
      call MessageBoxA 
      push 0 
      call ExitProcess 

I found some online tutorials how to make a for loop, so I made this:


segment .data 
 TITLE db 'Message',0 
 TEXT dd 'This is a message',0 
segment .text 
global  start 
extern MessageBoxA 
extern ExitProcess 
start: 
             mov ecx,5
             loop1:
             push 0 
             push TITLE 
             push TEXT
             push 0 
             call MessageBoxA 
             push 0
             dec ecx
             jnz loop1
             call ExitProcess

I tried to implement a simple for loop which would make the textbox appear five times following this tutorial on stackoverflow

However what this does is it makes it re-appear every time I press "OK" which leads me to think I may have made an infinite loop. My question is, how do I modify this so it is correct?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
l0ner9
  • 263
  • 1
  • 7
  • 3
    MessageBox is allowed to modify ECX, you must therefore preserve it: https://arvindsraj.wordpress.com/2013/01/12/x86-registers-register-conventions-and-calling-conventions – Hans Passant Oct 27 '21 at 13:05
  • @HansPassant Thank you for your fast answer. Do you perhaps have an idea how I could do that since I'm very new to assembly? – l0ner9 Oct 27 '21 at 13:10
  • 1
    PUSH ECX right after the loop1: label, POP ECX after the call. No real idea why there's a stray push 0 there, delete it. – Hans Passant Oct 27 '21 at 13:12
  • @HansPassant Thank you! It seems I need to study more about PUSH and POP. – l0ner9 Oct 27 '21 at 13:22
  • Don't use push/pop around a call, just pick a call-preserved register like EBX for your loop counter. (And generally [don't use the slow `loop` instruction anyway](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently) even if ECX was available.) – Peter Cordes Oct 27 '21 at 13:29

0 Answers0