Search code examples
assemblydelayinterruption

Pausing an assembly program


Is there a way to pause a program in assembly for say, 1 second, and then let it resume its normal flow? I'm programming a chat through the serial port, but for some reason, when I type a string on one of the programs, the other one does not always receive it as a single string; Sometimes it prints it as a whole, but other times it displays only one part and immediatly realizes there's more string to receive and prints it as a second string. What I'm trying to do is to stop the program for a second or so, so it can be sure that it receives the whole string and not just a part of it.


Solution

  • For 80x86; if the OS is something like MS-DOS or "none" then you'd want to poll the BIOS's "ticks since midnight" in a loop while doing HLT (to save power). To get the "ticks since midnight" use int 0x1A with ah=0x00. One second will be approximately 18 ticks. Be very careful with roll-over - if you're not careful and do "expiry_time = now + 1 second" just before midnight then you could be waiting forever, and you should do if(expiry_time >= 0x001800B0) expiry_time -= 0x001800B0 to prevent that.

    If the OS is anything modern (Windows, OS X, Linux, FreeBSD, etc) then you want to tell the OS's scheduler to run other tasks for 1 second (to avoid hogging CPU time for no reason); and you'll either have to find the appropriate (OS specific) API to do that or find some sort of library to link against that does it for you.