Replace invalid characters with HTML entities
— with — ’ with ’ + with + × with x ç with ç “ with “ ” with ” ‘ with ‘ • with • – with - µ with µ † with † Fix C++ θ with θ Yen symbol instead of times Fix broken apos Bullet again E-circumflex
This commit is contained in:
parent
9e52de9586
commit
500e7f5654
353 changed files with 5091 additions and 5091 deletions
18
11-08.html
18
11-08.html
|
|
@ -36,12 +36,12 @@
|
|||
</TABLE>
|
||||
</CENTER>
|
||||
<P><BR></P>
|
||||
<P>Well, there’s only one instruction other than <B>POPF</B> that loads the FLAGS register directly from the stack, and that’s <B>IRET</B>, which loads the FLAGS register from the stack as it branches, as shown in Figure 11.5. iret has no known bugs of the sort that plague <B>POPF</B>, so it’s certainly a candidate to replace popf in non-interruptible applications. Unfortunately, <B>IRET</B> loads the FLAGS register with the <I>third</I> word down on the stack, not the word on top of the stack, as is the case with <B>POPF</B>; the far return address that <B>IRET</B> pops into CS:IP lies between the top of the stack and the word popped into the FLAGS register.</P>
|
||||
<P>Obviously, the segment:offset that <B>IRET</B> expects to find on the stack above the pushed flags isn’t present when the stack is set up for <B>POPF</B>, so we’ll have to adjust the stack a bit before we can substitute <B>IRET</B> for <B>POPF</B>. What we’ll have to do is push the segment:offset of the instruction after our workaround code onto the stack right above the pushed flags. <B>IRET</B> will then branch to that address and pop the flags, ending up at the instruction after the workaround code with the flags popped. That’s just the result that would have occurred had we executed <B>POPF</B>—WITH the bonus that no interrupts can accidentally occur when the Interrupt flag is 0 both before and after the pop.</P>
|
||||
<P>Well, there’s only one instruction other than <B>POPF</B> that loads the FLAGS register directly from the stack, and that’s <B>IRET</B>, which loads the FLAGS register from the stack as it branches, as shown in Figure 11.5. iret has no known bugs of the sort that plague <B>POPF</B>, so it’s certainly a candidate to replace popf in non-interruptible applications. Unfortunately, <B>IRET</B> loads the FLAGS register with the <I>third</I> word down on the stack, not the word on top of the stack, as is the case with <B>POPF</B>; the far return address that <B>IRET</B> pops into CS:IP lies between the top of the stack and the word popped into the FLAGS register.</P>
|
||||
<P>Obviously, the segment:offset that <B>IRET</B> expects to find on the stack above the pushed flags isn’t present when the stack is set up for <B>POPF</B>, so we’ll have to adjust the stack a bit before we can substitute <B>IRET</B> for <B>POPF</B>. What we’ll have to do is push the segment:offset of the instruction after our workaround code onto the stack right above the pushed flags. <B>IRET</B> will then branch to that address and pop the flags, ending up at the instruction after the workaround code with the flags popped. That’s just the result that would have occurred had we executed <B>POPF</B>—WITH the bonus that no interrupts can accidentally occur when the Interrupt flag is 0 both before and after the pop.</P>
|
||||
<P><A NAME="Fig4"><!-- </A><A HREF="javascript:displayWindow('images/11-04.jpg',412,383 )"> --><IMG SRC="images/11-04.jpg"><BR><!-- </A>
|
||||
<BR><A HREF="javascript:displayWindow('images/11-04.jpg',412,383)"> --><FONT COLOR="#000077"><B>Figure 11.4</B></FONT></A> <I>The operation of POPF.</I>
|
||||
</P>
|
||||
<P>How can we push the segment:offset of the next instruction? Well, finding the offset of the next instruction by performing a near call to that instruction is a tried-and-true trick. We can do something similar here, but in this case we need a far call, since <B>IRET</B> requires both a segment and an offset. We’ll also branch backward so that the address pushed on the stack will point to the instruction we want to continue with. The code works out like this:</P>
|
||||
<P>How can we push the segment:offset of the next instruction? Well, finding the offset of the next instruction by performing a near call to that instruction is a tried-and-true trick. We can do something similar here, but in this case we need a far call, since <B>IRET</B> requires both a segment and an offset. We’ll also branch backward so that the address pushed on the stack will point to the instruction we want to continue with. The code works out like this:</P>
|
||||
<!-- CODE //-->
|
||||
<PRE>
|
||||
jmpshort popfskip
|
||||
|
|
@ -81,25 +81,25 @@ popfskip:
|
|||
endm
|
||||
</PRE>
|
||||
<!-- END CODE //-->
|
||||
<P>By the way, the flags can be popped much more quickly if you’re willing to alter a register in the process. For example, the following macro emulates <B>POPF</B> with just one branch, but wipes out AX:</P>
|
||||
<P>By the way, the flags can be popped much more quickly if you’re willing to alter a register in the process. For example, the following macro emulates <B>POPF</B> with just one branch, but wipes out AX:</P>
|
||||
<!-- CODE SNIP //-->
|
||||
<PRE>
|
||||
EMULATE_POPF_TRASH_AX macro
|
||||
push cs
|
||||
mov ax,offset $+5
|
||||
mov ax,offset $+5
|
||||
push ax
|
||||
iret
|
||||
endm
|
||||
</PRE>
|
||||
<!-- END CODE SNIP //-->
|
||||
<P>It’s not a perfect substitute for <B>POPF</B>, since <B>POPF</B> doesn’t alter any registers, but it’s faster and shorter than <B>EMULATE_POPF</B> when you can spare the register. If you’re using 286-specific instructions, you can use which is shorter still, alters no registers, and branches just once. (Of course, this version of <B>EMULATE_POPF</B> won’t work on an 8088.)</P>
|
||||
<P>It’s not a perfect substitute for <B>POPF</B>, since <B>POPF</B> doesn’t alter any registers, but it’s faster and shorter than <B>EMULATE_POPF</B> when you can spare the register. If you’re using 286-specific instructions, you can use which is shorter still, alters no registers, and branches just once. (Of course, this version of <B>EMULATE_POPF</B> won’t work on an 8088.)</P>
|
||||
<!-- CODE SNIP //-->
|
||||
<PRE>
|
||||
.286
|
||||
:
|
||||
EMULATE_POPFmacro
|
||||
pushcs
|
||||
pushoffset $+4
|
||||
pushoffset $+4
|
||||
iret
|
||||
endm
|
||||
</PRE>
|
||||
|
|
@ -107,8 +107,8 @@ EMULATE_POPFmacro
|
|||
<P><A NAME="Fig6"><!-- </A><A HREF="javascript:displayWindow('images/11-06.jpg',409,447 )"> --><IMG SRC="images/11-06.jpg"><BR><!-- </A>
|
||||
<BR><A HREF="javascript:displayWindow('images/11-06.jpg',409,447)"> --><FONT COLOR="#000077"><B>Figure 11.6</B></FONT></A> <I>Workaround code for the POPF bug.</I>
|
||||
</P>
|
||||
<P>The standard version of <B>EMULATE_POPF</B> is 6 bytes longer than <B>POPF</B> and much slower, as you’d expect given that it involves three branches. Anyone in his/her right mind would prefer <B>POPF</B> to a larger, slower, three-branch macro—given a choice. In noncode, however, there’s no choice here; the safer—if slower—approach is the best. (Having people associate your programs with crashed computers is <I>not</I> a desirable situation, no matter how unfair the circumstances under which it occurs.)</P>
|
||||
<P>And now you know the nature of and the workaround for the <B>POPF</B> bug. Whether you ever need the workaround or not, it’s a neatly packaged example of the tremendous flexibility of the x86 instruction set.</P><P><BR></P>
|
||||
<P>The standard version of <B>EMULATE_POPF</B> is 6 bytes longer than <B>POPF</B> and much slower, as you’d expect given that it involves three branches. Anyone in his/her right mind would prefer <B>POPF</B> to a larger, slower, three-branch macro—given a choice. In noncode, however, there’s no choice here; the safer—if slower—approach is the best. (Having people associate your programs with crashed computers is <I>not</I> a desirable situation, no matter how unfair the circumstances under which it occurs.)</P>
|
||||
<P>And now you know the nature of and the workaround for the <B>POPF</B> bug. Whether you ever need the workaround or not, it’s a neatly packaged example of the tremendous flexibility of the x86 instruction set.</P><P><BR></P>
|
||||
<CENTER>
|
||||
<TABLE BORDER>
|
||||
<TR>
|
||||
|
|
|
|||
Loading…
Reference in a new issue