[comp.os.msdos.programmer] iret and retf

leeda@frith.egr.msu.edu (Dae Hee Lee) (03/14/91)

When can I use retf instead of iret?
I was told that retf discards some register in a stack.  Then, why and when can I discard any registers in a stack?
Thank you for your help!

Daehee

rkl@cbnewsh.att.com (kevin.laux) (03/14/91)

In article <1991Mar14.031024.12576@msuinfo.cl.msu.edu>, leeda@frith.egr.msu.edu (Dae Hee Lee) writes:
> 
> When can I use retf instead of iret?
> I was told that retf discards some register in a stack.  Then, why and when can I discard any registers in a stack?
> Thank you for your help!
> 
> Daehee
> 

	RETF is used when interrupt handlers want to return one of the Flags
bits.  The INT instruction pushes the Flags onto the current stack, then CS,
then IP.  When the interrupt handler uses the flags bits to return success,
failure or whatever, it cannot use an IRET as this will end up popping the
old flags off the stack destroying the bits that were purposely set up.
The RETF 0002h instruction will pop CS:IP off the stack and then will discard
two bytes.

	For example, if one is using Int 15h, Function 4Fh, the keyboard
intercept to do character translations, returning with the Carry flag clear
causes Int 9h to ignore the character; returning with the Carry flag set
(the default) make Int 9h continue processing.

-- 
________________________________________________________________________________
	R. Kevin Laux				Email: rkl1@hound.att.com
	AT&T Bell Labs				Voice: (908) 949-1160
	Holmdel, NJ 07733			Fax:   (908) 949-0959

valley@uchicago (Doug Dougherty) (03/15/91)

leeda@frith.egr.msu.edu (Dae Hee Lee) writes:


>When can I use retf instead of iret?
>I was told that retf discards some register in a stack.  Then, why and when can I discard any registers in a stack?
>Thank you for your help!

IRET pops 3 three words off the stack, IP, CS, flags, in that order.
(I.e., IP is @SP, CS is [SP+2], flags is [SP+4], in pseudoassembler)
So, if you use RETF instead of IRET, you will be leaving one word of
flags on the stack.  I think this is what INT 25/INT 26 do.

valley@uchicago (Doug Dougherty) (03/15/91)

rkl@cbnewsh.att.com (kevin.laux) writes:

>	RETF is used when interrupt handlers want to return one of the Flags
>bits.  The INT instruction pushes the Flags onto the current stack, then CS,
>then IP.  When the interrupt handler uses the flags bits to return success,
>failure or whatever, it cannot use an IRET as this will end up popping the
>old flags off the stack destroying the bits that were purposely set up.
>The RETF 0002h instruction will pop CS:IP off the stack and then will discard
>two bytes.

I think you are wrong.  (Purely conjecture, since I haven't debugged the
code, of course).  I think routines that return information in the
flags (such as, e.g., almost all of the INT 21 routines) do it by
manipulating the word on the stack itself (at, I believe, [SP+4]), so
that the final IRET returns a diferent flags value than the one
originally pushed by the INT instruction.

rkl@cbnewsh.att.com (kevin.laux) (03/15/91)

In article <valley.668976840@gsbsun>, valley@uchicago (Doug Dougherty) writes:
> rkl@cbnewsh.att.com (kevin.laux) writes:
> 
> >	RETF is used when interrupt handlers want to return one of the Flags
> >bits.  The INT instruction pushes the Flags onto the current stack, then CS,
> >then IP.  When the interrupt handler uses the flags bits to return success,
> >failure or whatever, it cannot use an IRET as this will end up popping the
> >old flags off the stack destroying the bits that were purposely set up.
> >The RETF 0002h instruction will pop CS:IP off the stack and then will discard
> >two bytes.
> 
> I think you are wrong.  (Purely conjecture, since I haven't debugged the
> code, of course).  I think routines that return information in the
> flags (such as, e.g., almost all of the INT 21 routines) do it by
> manipulating the word on the stack itself (at, I believe, [SP+4]), so
> that the final IRET returns a diferent flags value than the one
> originally pushed by the INT instruction.

	Yep - is purely conjecture.  Debug/unassemble your BIOS and you'll
find what I said is exactly correct.

-- 
________________________________________________________________________________
	R. Kevin Laux				Email: rkl1@hound.att.com
	AT&T Bell Labs				Voice: (908) 949-1160
	Holmdel, NJ 07733			Fax:   (908) 949-0959

rcollins@altos86.Altos.COM (Robert Collins) (03/19/91)

In article <valley.668976840@gsbsun> valley@uchicago (Doug Dougherty) writes:
>rkl@cbnewsh.att.com (kevin.laux) writes:
>
>>	RETF is used when interrupt handlers want to return one of the Flags
>>bits.  
>>The RETF 0002h instruction will pop CS:IP off the stack and then will discard
>>two bytes.
>
>I think you are wrong.  

No, he is absolutely right.  

However there are two ways to do this --  RETF 2, or manipulating the flags
value on the stack and using IRET.  Manipulating the flags value on the stack
is more cumbersome, and not as convenient as using RETF 2.  On the other hand,
RETF 2 fails to restore the values of the other flags (like the interrupt
flag).  The style used depends on the person who programmed the code.  As
a BIOS programmer, I have seen both styles used inside the BIOS.

-- 
"Worship the Lord your God, and serve him only."  Mat. 4:10
Robert Collins                 UUCP:  ...!sun!altos86!rcollins
HOME:  (408) 225-8002
WORK:  (408) 432-6200 x4356

frisk@rhi.hi.is (Fridrik Skulason) (03/20/91)

In article <1991Mar14.031024.12576@msuinfo.cl.msu.edu> leeda@frith.egr.msu.edu (Dae Hee Lee) writes:
>
>When can I use retf instead of iret?

A - When returning from an interrupt routine:

Normally one uses IRET when returning from an interrupt call.  IRET will pop
the return address and the flags (total of 6 bytes) from the stack.

RETF by itself will only pop the return address from the stack, and will
leave the original flags.  This can be used at the end of an interrupt
routine, if you want to return some information in the flag register.

The INT 25H and INT 26H routines do this for example.

Another possibility is to use RETF 2, which will discard the original flags.

B - When arguments need to be removed from the stack

In languages like C and Pascal, arguments are generally passed on the stack.
In some cases the caller is supposed to clear the stack afterwards, but in
other cases the called program should do this.  


Possibility 1)
                 PUSH  ARG1
                 PUSH  ARG2
                 CALL  SUBPROGRAM
                 ADD   SP,4       ; the calling program clears the stack
                 :
                 :
SUBPROGRAM:      :
                 :
                 RETF


Possibility 2)

                 PUSH  ARG1
                 PUSH  ARG2
                 CALL  SUBPROGRAM
                 :
                 :
SUBPROGRAM:      :
                 :
                 RETF 4   ; the called program clears the stack

The methods are equally good, however.

-frisk

--
Fridrik Skulason      University of Iceland  |       
Technical Editor of the Virus Bulletin (UK)  |  Reserved for future expansion
E-Mail: frisk@rhi.hi.is    Fax: 354-1-28801  |