News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

how to use msvcrt sleep function masm

Started by hellfix, June 16, 2012, 03:33:37 AM

Previous topic - Next topic

hellfix

.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.

Mr Hippy

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.

hellfix

is there another way other than using masm32crt because i keep receiving unresolved functions msg in the prompt window.

Vortex

Hi hellfix,

The Sleep function is exported by kernel32.dll , not msvcrt.dll

hellfix

thank for your reply,would i be able to use it by calling invoke sleep,5000 with the extern Sleep declaration.

Vortex

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

hellfix

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

dedndave

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

Vortex

Hi hellfix,

Dave is right, Sleep is a STDCALL function.

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




hellfix

Thanks again you guys are smart :t :icon14: like jedi's

Mr Hippy

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.

jj2007

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

Vortex

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 )

hutch--

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.