KAH CHUN LEE
KAH CHUN LEE

Reputation: 21

Why does jump if carry not work after IRET

I'm a beginner in learning 8086 assembly language using DOSBox. I'm using int 21h function 3Dh to open a data file, and I wanted to carry out the code using the file handle if the file is opened successfully and quit if the file is not opened successfully. It works like normal, the carry flag is set if it opened the file or clear if not. However, after the IRET instruction, it resets the flag back to the state before int 21h happened. Is there some way I can use the carry flag by preventing IRET to reset the flag?

Here's the code

 MOV AH,3DH
 MOV AL,0
 LEA DX,SYSFILENAME
 INT 21H
 JC DATAFILENOTFOUND
 MOV FILEHANDLE,AX

I've searched some post, I couldn't find any post about same as mine. Am I forgetting to do something or what?

Upvotes: 2

Views: 90

Answers (2)

Sep Roland
Sep Roland

Reputation: 39516

It works like normal, the carry flag is set if it opened the file or clear if not.

It's just the opposite! I have used DOSBox a lot and I have never seen this kind of behaviour. If the file is opened successfully then upon return, the carry flag will be clear, and if there was some sort of error while processing your request to open the file then the carry flag will be set and you will receive an accompanying errorcode in the AX register.

However, after the IRET instruction, it resets the flag back to the state before int21h happened.

If the programmer that actually wrote the interrupt handler did not want to return to you a bit of information in the carry flag, then indeed iret would return with the state of the carry flag that existed before the int 21h instruction got executed. But as it is, many of the DOS functions (including the one that you are using today) will modify the carry flag so as to inform you about the successful completion of the operation.

Whenever the int 21h function in this code snippet:

mov  dx, offset SYSFILENAME
mov  ax, 3D00h              ; DOS.OpenFile
int  21h                    ; -> AX CF
jc   Error
mov  FILEHANDLE, ax

returns to you with the carry flag set, it does not automatically mean that the file was not found. You can of course treat it that way as a simplification, but be aware that it is the errorcode from the AX register that tells you what went wrong.

Is there some way I can use the carry flag by preventing IRET to reset the flag?

If you yourself become the author of the concerned interrupt handler that returns to its caller through use of the iret instruction, then you will be in control of the flags that you want the caller to receive. This is not limited to just the carry flag CF; at least one BIOS function returns info through the zero flag ZF.
However, since the DOS.OpenFile function 3Dh was written by some third party, you'll just have to accept how those programmers chose to do things...

Upvotes: 1

fuz
fuz

Reputation: 93127

The iret instruction resets CF back to the state it had before by popping the flags off the stack into the flags register. If you want to set CF to a defined value when returning from your interrupt handler, modify the copy of CF on the stack like so for example:

MOV     BP, SP                   ; obtain access to stack
AND     BYTE PTR [BP+4], 0FEh    ; clear CF on the stack
OR      BYTE PTR [BP+4], 1       ; set CF on the stack
...
IRET                             ; return from interrupt with CF modified

Upvotes: 1

Related Questions