The MASM Forum

General => The Campus => Topic started by: hellfix on June 16, 2012, 03:33:37 AM

Title: how to use msvcrt sleep function masm
Post by: hellfix on June 16, 2012, 03:33:37 AM
.386         
.model flat,c
.stack 100h   

includelib msvcrt

.data
printf proto arg1:ptr byte             
extern Sleep                            ;function is external to the program

msg byte "hellfix",0       ;main msg

.code
public main

main proc
     invoke Sleep, 5000          ;call the function 
     invoke printf,addr msg
     ret
main endp

end main

Im trying to use the sleep function included in msvcrt but it seems it does not
work in my program could someone explain what im doing wrong thanks in advance.
Title: Re: how to use msvcrt sleep function masm
Post by: Mr Hippy on June 16, 2012, 03:39:19 AM
include \masm32\include\masm32rt.inc

.data

msg DB "my message after pausing for 5 seconds",0

.code

start:

INVOKE Sleep, 5000
INVOKE printf, OFFSET msg
INVOKE ExitProcess, 0

end start





Keep your definitions above and out of your segments. You can include the masm32rt.inc include to have common Windows include files and macro definitions.
Title: Re: how to use msvcrt sleep function masm
Post by: hellfix on June 16, 2012, 04:01:49 AM
is there another way other than using masm32crt because i keep receiving unresolved functions msg in the prompt window.
Title: Re: how to use msvcrt sleep function masm
Post by: Vortex on June 16, 2012, 04:03:59 AM
Hi hellfix,

The Sleep function is exported by kernel32.dll , not msvcrt.dll
Title: Re: how to use msvcrt sleep function masm
Post by: hellfix on June 16, 2012, 04:11:24 AM
thank for your reply,would i be able to use it by calling invoke sleep,5000 with the extern Sleep declaration.
Title: Re: how to use msvcrt sleep function masm
Post by: Vortex on June 16, 2012, 04:24:02 AM
Here is an example for you :



.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\masm32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\masm32.lib


.data

m1          db 'Please wait for 3 seconds.',13,10,0
m2          db 'This is a Sleep function test.',0

.code

start:

    invoke  StdOut,ADDR m1
    invoke  Sleep,3000
    invoke  StdOut,ADDR m2
    invoke  ExitProcess,0

END start
Title: Re: how to use msvcrt sleep function masm
Post by: hellfix on June 16, 2012, 04:32:22 AM
Thanks again but the program you made dose not work with my version in visual c++ studio do i have to use masm32 or can i use the one in visual c++ studio programmed in a different way

.386         
.model flat,c
.stack 100h 

includelib msvcrt   ;printf function
includelib kernel32 ;sleep function

.data
printf proto arg1:ptr byte
Sleep proto arg2:dword       
           
msg byte "hellfix",0     
.code
public main

main proc 
     invoke Sleep,5000
     invoke printf,offset msg
     ret
main endp

end main
Title: Re: how to use msvcrt sleep function masm
Post by: dedndave on June 16, 2012, 04:42:00 AM
Sleep, as with all windows API functions, is StdCall - not C calling convention

this will get you the StdCall stuff
        INCLUDE    \masm32\include\kernel32.inc
        INCLUDELIB \masm32\lib\kernel32.lib


however, if you want to use C calling convention, you may have to create a prototype like this
(and leave out the INC file)
Sleep PROTO StdCall :DWORD
i am not sure how the decoration is handled - Erol knows that stuff inside out   :P

it might be easier to use StdCall, then specify C for certain PROC's
Title: Re: how to use msvcrt sleep function masm
Post by: Vortex on June 16, 2012, 04:53:18 AM
Hi hellfix,

Dave is right, Sleep is a STDCALL function.

You can modify .model flat,c to .model flat,stdcall. It's another solution.



Title: Re: how to use msvcrt sleep function masm
Post by: hellfix on June 16, 2012, 05:00:18 AM
Thanks again you guys are smart :t :icon14: like jedi's
Title: Re: how to use msvcrt sleep function masm
Post by: Mr Hippy on June 16, 2012, 08:59:52 AM
The C functions are already declared in the C calling convention through the use of masm32rt.inc. I'm confused, am I doing it wrong? :icon_confused:

On a side note: the method that you are using, Sleep, is different from the method, sleep, in the C standard library. The first one would be part of the Windows API.
Title: Re: how to use msvcrt sleep function masm
Post by: jj2007 on June 16, 2012, 03:31:50 PM
include \masm32\include\masm32rt.inc

.code
AppName db "Masm32:", 0

start: print "ua"
invoke Sleep, 400
print "aa"
invoke crt__sleep, 400
inkey "aaah!!"
exit

end start
Title: Re: how to use msvcrt sleep function masm
Post by: Vortex on June 16, 2012, 10:22:19 PM
Hi Mr Hippy,

If you wish to use the Sleep function, you can prefer the one exported by kernel32.dll if there is no other dependencies to msvcrt.dll ( or another C run-time library )
Title: Re: how to use msvcrt sleep function masm
Post by: hutch-- on June 17, 2012, 12:39:54 AM
hellfix,

If you are going to persist in using an out of date architecture from the 16 bit DOS era, you will not get very far with MASM coding in win32 and later. Instead of using a 1992 book on MASM you need to learn current modern 32 bit protected mode assembler that deals with the Windows API functions. To use MSVCRT properly you need to write correct 32 bit code that can call functions from the C runtime library but note that the vast majority of functions in MSVCRT are based off the lower level Windows API functions.

The MASM32 SDK is current architecture that uses the Intel ABI register convention and it can call and use the full range of Windows API functions, what you are doing is technically incorrect in Win32 and you need to use later data to succeed in what you are doing.