Page 1 of 1

having trouble preserving flags through IRET

Posted: Sat Mar 14, 2009 10:53 pm
by Firestryke31
I've abandoned trying to keep Socks in 512 bytes, which has allowed me to add a good number of features I couldn't have before. One of these features I'm struggling with is my system API ISR returning with the Carry or Zero flags set. I can't seem to get the flags past the IRET opcode, no matter what I try. I'm guessing I'm using the wrong stack offset, but I've checked the numbers several times.

Here's what I have (16-bit code):

Code: Select all

paramEDI equ BP+ 0
paramESI equ BP+ 4
paramEBP equ BP+ 8
paramESP equ BP+12
paramEBX equ BP+16
paramEDX equ BP+20
paramECX equ BP+24
paramEAX equ BP+28
intIP equ BP+32
intCS equ BP+34
intEF equ BP+36

socksISR:
	pushad
	mov bp, sp

;; Snip out calling the functions, they work correctly
;; I've very thoroughly tested them and they leave
;; SP and BP exactly as they were.

	pushf
	and byte [intEF], 0xBE
	popf
	pushf
	jnc .noCarry
	or byte [intEF], 0x01
.noCarry:
	popf
	jnz .noZero
	or byte [intEF], 0x40
.noZero:

;; I've also tried this with the same results:
;;  lahf
;;  and ah, 0x41
;;  or [intEF], ah

	mov sp, bp
	popad
	iret
My readFile function worked just fine (it successfully read folders from the disk) before I realized that the flags weren't being returned properly, but now it seems to think that there's always a disk read error, even though Bochs says there aren't. I'm pretty sure that I'm just doing something stupid. Could someone please tell me what it is?

Re: having trouble preserving flags through IRET

Posted: Sat Mar 14, 2009 11:00 pm
by Troy Martin
This is what I do:

Code: Select all

    pop ax	; IP
    pop bx	; CS
    pop cx	; Old FLAGS

    pushf		; Push new FLAGS
    push bx		; CS
    push ax		; IP

    iret

Re: having trouble preserving flags through IRET

Posted: Sat Mar 14, 2009 11:16 pm
by Firestryke31
I was trying to preserve the other flags, but on further review they don't exactly matter, so I'll try that. I hope it works.

Edit: That actually won't work due to the pushad/popad. But I'll try not preserving the other flags anyway and see how that goes.

Edit II: The Solution:

Here's the code I ended up using:

Code: Select all

	pushf
	pop ax
	mov [intEF], ax
Much simpler, and it works. I think in my old code I needed to use [intEF+1] instead.

Now I just have to find why my attempts to fail to run a program fail. But that's probably a different topic.