The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Magnum on September 18, 2010, 04:21:31 AM

Title: Repeat a block of code
Post by: Magnum on September 18, 2010, 04:21:31 AM
I would like to repeat these 2 sounds 25 times and then wait the 2 secs.

I tried plugging 25 into the ecx register and decrementing it, but got an endless loop.



invoke Beep, 1000, 55 ; Sound duration and the frequency in hertz
invoke Beep, 800, 55
   
invoke Sleep, 2000

Title: Re: Repeat a block of code
Post by: MichaelW on September 18, 2010, 04:33:30 AM
Since ECX is one of the three registers not preserved across function calls, to use it as a loop counter you would need to preserve it around the function calls:

    mov ecx, 25
  @@:
    push ecx
    invoke Beep, 1000, 55 ; Sound duration and the frequency in hertz
    invoke Beep, 800, 55   
    pop ecx
    dec ecx
    jnz @B
    invoke Sleep, 2000


Or if you don't mind simply repeating the statements 25 times each , you can save some typing effort with:

REPEAT 25
    invoke Beep, 1000, 55 ; Sound duration and the frequency in hertz
    invoke Beep, 800, 55
ENDM   
invoke Sleep, 2000
Title: Re: Repeat a block of code
Post by: Magnum on September 18, 2010, 01:22:10 PM
Thanks MichaelW.