Hello,

I'm messing around with MASM, making some very basic little programs to get familiar with the syntax. However, I ran into some kind of problem.

Here is my code


.386P

.MODEL Flat,STDCALL
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib

yes equ offset saidyes
no equ offset saidno
dur equ 200

.DATA
myVar db "Blah, a string, yay",0
myVar2 db "another string, yay2",0
saidno db "You clicked no",0
saidyes db "You clicked yes",0

.CODE
Main:
push MB_YESNO
push offset myVar
push offset myVar2
push 0
call MessageBoxA

cmp eax,IDYES
jnz said_no
jmp said_yes

said_no:
push MB_OK
push no
push no
push 0
call MessageBoxA
jmp _end

said_yes:
mov ecx,50

piano:
push dur
push ecx
call Beep

test eax,eax
jz error_msg

inc ecx
cmp ecx,100
jz msg
jmp piano

error_msg:
push MB_OK
push yes
push no
push 0
call MessageBoxA

msg:
push MB_OK
push yes
push yes
push 0
call MessageBoxA

_end:
call ExitProcess
End Main


The problem is in the beep loop. The idea is that it goes up in tone till it's at a freq of 100 and then stops. However, after 1 tone I get the error_msg jump...

It seems like ecx is destroyed after the call to Beep. I tried popping it, moving and removing it, none of it works...

What am I doing wrong here? Similar constructions tend to work fine in inline ASM in C and Delphi...

- Fahr
Posted on 2003-03-21 15:26:53 by Fahr
Try this:

Originally posted by Fahr


.386P

.MODEL Flat,STDCALL
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib

yes equ offset saidyes
no equ offset saidno
dur equ 200

.DATA
myVar db "Blah, a string, yay",0
myVar2 db "another string, yay2",0
saidno db "You clicked no",0
saidyes db "You clicked yes",0

.CODE
Main:
push MB_YESNO
push offset myVar
push offset myVar2
push 0
call MessageBoxA

cmp eax,IDYES
jnz said_no
jmp said_yes

said_no:
push MB_OK
push no
push no
push 0
call MessageBoxA
jmp _end

said_yes:
mov ecx,50

piano:
[B]
push ecx
[/B]
push dur
push ecx
call Beep

[B]pop ecx
[/B]

test eax,eax
jz error_msg


inc ecx
cmp ecx,100
jz msg
jmp piano

error_msg:
push MB_OK
push yes
push no
push 0
call MessageBoxA

msg:
push MB_OK
push yes
push yes
push 0
call MessageBoxA

_end:
call ExitProcess
End Main




Fake
Posted on 2003-03-21 15:39:25 by Fake51
and it works :P


stupid, I could have thought of that myself... odd also that it works without in inline, most of the times...

thanks,
- Fahr
Posted on 2003-03-21 15:42:12 by Fahr
First Rule of Win32ASM programming...

Never assume EAX, ECX, or EDX will remain the same after an API call! (EAX should be a no-brainer tho ;) )

In this case your call to Beep alters ECX before it returns..
Posted on 2003-03-21 21:12:56 by NaN
ecx is changed in the Beep proc. I agree with Nan. :grin:
Posted on 2003-03-22 04:53:52 by roticv