Question

How do I make sure that the the value I write to a processor register is not modified by another task in a multitasking environment before I do not need it any more?

mov eax , 42
;
; some calculations
;
; which use the value 
;
; stored in the eax
;

If other program will modify the register value before I finish using it my program will probably crash. How do I deal with it?

Was it helpful?

Solution

The OS takes care of this for you.

When you multitask, the OS saves your context (flags and registers). When your context is restored, the saved data is also restored, so even if you change the value of eax in another processor, your code will work. From your code point of view, you are running on the same code as before. You cannot access the registers of one processor from another processor, so you are safe on both cases.

What causes problem is the source of your data, for example, if you moved data from memory to eax, without locking, you have no garantee that that data didn't change. In this case, you lock, move, compute and unlock.

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