Question

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.

Was it helpful?

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.

OTHER TIPS

I am answering not the question itself, but anyway:

Never (I mean NEVER!) use time delays for such goals! It is very, very, very bad design. The serial port communication is based on character streams. The properly designed program (regardless of the language used) must process stream without need of any time delays.

Yes,

use GOTO and NOPS.

or create a loop conainting nops and escape it when certain condition are met. or create a GOTO label a bunchs of nops

startover
nop
nop
nop
nop
nop
nop
nop
nop
GOTO startover // this wil run forever! you need an escape condition

Yes, you can get the system clock, and then make a loop to check if a second has passed after you got the time...

This might help if you are on x86 assembly: https://softwareengineering.stackexchange.com/questions/134084/assembly-instructions-execution-time

Or you can make a very long loop to make the pause... the second one I don't recommend much, but it may suit your needs

It seems the problem came from buffering. Waiting can be good but you must answer the main question which is "How many times?". And in fact, you don't really know. Comonly in ASM, we use interrupt or we wait "until a flag is set". But only "waiting a certain amout of second" seems not to be the right choice (I agree 100% with johnfound!)

Suggestion: in the sender programm, buffer the data until CR/LF then send them. On receiver, wait until CR/LF to display. Also have a look at the serial buffer pointers (IN and OUT) to see position of start and end pointer which may give some info explaining what's happening.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top