简体   繁体   中英

Multi Threading in Assembly 32 bit or 16 bit

How can we perform two work at the same time?

While displaying something continuously program should wait for a keypress event at other side.

I want to develop a simple game for my class project. In C++ I used different threads one for display one for input events.

How can we get a keypress value while displaying something continuously?

Can an interrupt can be used? If yes, then how can we use it? INT ?

I am using Masm 32bit or 16 bit

int synchronously generates an interrupt. It's what you use for software-generated interrupts.

You "get" interrupts by installing an interrupt handler, if your code is running in ring 0 (ie kernel mode, which is normal in 16-bit real mode but not in 32-bit protected mode).

Or ( as Ped7G says ) you can poll for key-presses once per draw loop with a non-blocking int 21h or int 10h system call.


When running under a full multi-tasking OS (eg Windows NT or later, or Linux or OS X, but not DOS), your only option is to ask (by using a system call) the OS to notify your process of async events. This has basically nothing to do with assembly language because you can't access the hardware directly, and the OS keeps control of all the interrupt handlers. ie you'd do it exactly the same way you would in C, with the same args to the same system calls. The only difference is that you push ecx / push ebx / push eax / call foo / add esp, 12 instead of foo(a,b,c) .

For example, you could start another thread and have it call a wait-for-key system call (so it blocks), then either set a flag (which your main thread polls occasionally), or have the thread that waits for keypresses actually handle the keypress itself.

You could also have the key-wait thread send some kind of signal to the main thread, which is almost exactly like an interrupt handler in user-space. Except that signals are pure software inventions, and it's up to the OS to deliver the "signal" by making the main thread execute the signal-handler function that it installed. (IDK if Windows has something that's exactly like POSIX signals, so assuming you're using MASM to develop Windows executables this might not be an option for interrupting an infinite loop in your main thread.)

Other ways of doing asynchronous I/O are also possible, for example have your main thread poll for key-presses with a non-blocking system call after drawing every frame.

I'm not going into a lot of detail here, but you should be able to google some of the phrases I used and find more. The question is too general for a specific answer, but this is how the OS machinery works to support this kind of thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM