Asm code bug, please help!

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
darklife

Asm code bug, please help!

Post by darklife »

I can't figure out why this code doesn't do its job correctly. It is suppost to run the User_Program: routine until a timer tick happens. Then it will return to DOS (for now).
The problem is odd. The program will run two or three times from the command prompt in DOS and most the time on the third or fourth time the program will stall.
All the stuff seems to be there thats needed? I have coded the EOI along with CLI and STI where needed but why does the program fail after being ran more then twice? This program will be ran MANY times so I need it working upto snuff.
When running a debugger I found that after being ran two times the timer routine doesn't want to reenter, thus the User_Program: that contains just a "jmp $" just loops forever and locks up the machine. If it worked correctly it would exit do to the fact that the timer 1Ch routine does its thing and exits to DOS (through a RETF).


BITS 16
ORG 100H

mov ax, cs
mov [Return_Addr], ax
mov ax, done
mov [Return_Addr + 2], ax
jmp Enter

done:
sti
int 0x20



Return_Addr: DW 0 ;Segment
DW 0 ;Offset

Enter:
XOR BX, BX
MOV ES, BX
MOV BX, [ES:1CH * 4 + 0]
MOV DX, [ES:1CH * 4 + 2]
MOV [Old_Vector + 0], BX
MOV [Old_Vector + 2], DX
CLI
MOV [ES:1CH * 4 + 0], WORD Task_Done
MOV [ES:1CH * 4 + 2], CS
STI
JMP User_Program


Task_Done:
CLI

XOR BX, BX
MOV ES, BX
MOV BX, [CS:Old_Vector + 0]
MOV DX, [CS:Old_Vector + 2]
MOV [ES:1CH * 4 + 0], BX
MOV [ES:1CH * 4 + 2], DX

MOV AL, 20H   ;EOI
OUT 20H, AL   ;

PUSH WORD [CS:Return_Addr + 0]
PUSH WORD [CS:Return_Addr + 2]
RETF ;Return to DOS from above..

Old_Vector: DW 0
DW 0


User_Program: ;code will go here that can only
;be ran for 18.2 x a sec MAX.
jmp $



Thanks people for your help!
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Asm code bug, please help!

Post by Pype.Clicker »

1. if "task done" is an interrupt handler, it does not need to CLI : the processor always enter an interrupt handler with IF cleared.

2. on the other hand, if you do not let the interrupt resume with an IRET, the interrupts will remain blocked when DOS get the control back. Okay, you do a STI, but still, this isn't a very clean way.

3. you leave the timer interrupt handler modified when exitting, thus later interrupts will still be routed to your code, and as your program terminated without telling the OS it should stay resident, Bad Things (tm) may occur ...

As a general rule, exitting a DOS program on an interrupt is not a very clean way to do. You should better adopt something like

Code: Select all

   interrupt_occured_flag db 0

handler:
   ; ... stuff
   mov [interrupt_occured_flag],1
   ; ... more stuff
   iret

main:
   ; ... setup
   sti
sleep:
   cmp [interrupt_occured_flag],0
   jne done
   hlt ; ... let the CPU sleep until an irq is received.
   jmp sleep
done:
   ; cleanup vektorz & stuff
   int 0x20
Schol-R-LEA

Re:Asm code bug, please help!

Post by Schol-R-LEA »

What is the purpose of the program? Is it meant to just test the timer interrupt handler, or is it meant to be something bigger? My impression is that you're writing a zen timer, which is a really good idea if you can do it, but the code is very difficult to follow.
darklife

Re:Asm code bug, please help!

Post by darklife »

Well thanks for your help. I think a lot has to do with DOSEMU and its bugs ::) This code is part of a few projects I am doing actually. Mainly a TSR for DOS that works in a preemptive multitasking model. The idea is to have the TSR work like a normal program except allowing DOS to run at the same time (multitasking). Err, maybe this can't be done. Also needed it for some robot work. More later. Thanks much.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Asm code bug, please help!

Post by Pype.Clicker »

a TSR program uses special DOS call to terminate, not just int 0x20.
darklife

Re:Asm code bug, please help!

Post by darklife »

I know, but the example was to give a "example", not the whole source.
Post Reply