News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

How to use instruction CLI and STI?

Started by leetow2003, April 19, 2011, 02:06:53 PM

Previous topic - Next topic

leetow2003

Who could tell me how to use instruction CLI and STI
and when to use them?And what difference are they in
R-mode and P-mode?

dedndave

not sure they are allowed in user (protected) mode - probably get GP fault
in real mode or kernel (protected) mode, they disable/enable interrupts
they are normally used in interrupt handlers, device drivers, or when changing an interrupt vector
for the later, you can use INT 21h with AH = 25h or 35h (i think that's right  :P )

they aren't used in "normal" programming very often

FORTRANS

Hi,

   CLI clears the interrupt flag so no interrupt will be serviced.
It is as if you turned off the interrupts, though in fact you just
made the CPU to ignore them when they occur.

   STI sets the interrupt flag to resume servicing interrupts.
Keyboard input, timers (clock tick), and other hardware events
that the OS wants to be notified of.

   You only need to use CLI/STI if you need to service an
interrupt or if your code is doing something that cannot be
interrupted.

   Real mode uses the Interrupt Vector Table filled with real
mode addresses of the service routines.  Protected mode
uses the Interrupt Descriptor Table which points again to the
service routines.  Either table must be initialized before being
used.

   DOS Fn 25H is used to set the interrupt vector.  DOS Fn
35H is used to read an entry in the IVT.

   As Dave says, most "normal" programs don't need CLI
and STI.  But you will need them if you are writing your own
OS or a service routine.

HTH,

Steve N.
.

leetow2003

Quote from: dedndave on April 19, 2011, 02:47:01 PM
not sure they are allowed in user (protected) mode - probably get GP fault
in real mode or kernel (protected) mode, they disable/enable interrupts
they are normally used in interrupt handlers, device drivers, or when changing an interrupt vector
for the later, you can use INT 21h with AH = 25h or 35h (i think that's right  :P )

they aren't used in "normal" programming very often
You mean the instruction CLI isn't used in protected mode?Could I comprehend right?

dedndave

in "user" mode, i don't think you can use them - never tried   :P
in user mode, there is little need for them - you can't do anything that requires them, really

in "kernel" mode, i am pretty sure you can use them
this applies to things like Kernel Mode Drivers, etc

mineiro

In old ms-dos days, when you type something in keyboard, the bios generates some interruptions at each key pressed. So, you have coded your own interruption, ms-dos have some disponible to the user, I forget now the range, but supose that you will hmm, make a hook of some ms-dos interruptions. Supose you like to check how many times the letter "A" have typed, in all context of ms-dos, so, you are in a game that uses that letter, after, you use "edit" to type some text, and after like to see how much times the letter "A" have been pressed(your routine). Remembering that ms-dos is monotask. These things described here was used in TSR(terminate and stay resident) programs.
In your interruption code, you need use cli and sti to enable/disable temporal that interruption, because in your interruption code, you need call the original interruption code.
Imagine, that you have pressed the letter "A", and the processor is executing your code, but you forgot to disable interruptions (cli), so you pressed the letter "A" again(generates an interruption), so the system call your code(int) again, but the moment back this, your code isn't fullly completed, because you generate another interruption. Can you see the problem?
Your code probably don't have the chance to call the original interruption, and the "edit","game",..., needs that to work. The result is probably a crash of system. Your code don't have the chance to call the original 'int', or, leaved some values in register,... .
regards.