Why does jump if carry not work after IRET

ghz 8months ago ⋅ 96 views

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?

Answers

In 8086 assembly language, the carry flag is typically used to indicate the success or failure of operations like file I/O. However, as you've noticed, the carry flag gets reset to its previous state upon executing the IRET instruction, which is commonly used at the end of interrupt service routines to return control to the main program.

To preserve the state of the carry flag after the IRET instruction, you can save its value before the interrupt call and restore it afterward. Here's how you can modify your code to achieve this:

MOV AH, 3DH   ; Function to open file
MOV AL, 0     ; Access mode (0 for read-only)
LEA DX, SYSFILENAME  ; Address of the filename
INT 21H        ; Call DOS interrupt

PUSHF          ; Push flags register onto the stack
JC DATAFILENOTFOUND  ; Jump if carry flag is set (file not found)
MOV FILEHANDLE, AX   ; Save file handle if file is opened successfully
POPF           ; Restore flags register from the stack

; Continue with your code here...

DATAFILENOTFOUND:
; Handle the case when the file is not found

In this modified version, before calling the INT 21H interrupt to open the file, we push the flags register onto the stack using the PUSHF instruction. After the interrupt call, we check the carry flag, and if it's set, indicating an error in opening the file, we jump to the DATAFILENOTFOUND label. Otherwise, if the file is opened successfully, we move the file handle to FILEHANDLE. Finally, we restore the flags register using the POPF instruction. This way, the state of the carry flag is preserved after the IRET instruction.