flat assembler
Message board for the users of flat assembler.

Index > OS Construction > [Solved!] Task switching fails.

Author
Thread Post new topic Reply to topic
Coty



Joined: 17 May 2010
Posts: 553
Location: ␀
Coty 06 Jan 2011, 16:52
Hello again everyone!

I have been working on a small code demo that swaps between two tasks in 32bit P-mode, sadly I am stuck Sad

The goal is to swap between Task A and Task B infinitely using software task switching, I rely on the stack, and IRQ 0, or 'PIT', Now on the bright side, it half works, I can get it to swap to task B from task A, but for some unknown reseon I cannot get it to swap back to task A.

I set up for task switching like this:

Code:
   xor     ebp, ebp               ; EBP == 0x00000000
  mov     esp, stack1_base       ; Declare task 1 stack. Smile
  ;'''''''''''''''''''''''''''''';
      ; Put stuff onto the stack for ;
    ; IRET/IRETD.                  ;
    ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;
    mov     eax, Task1
  sti
 nop
 pushf                          ; Push Eflags
        cli
 push cs                        ; Push CS
    push eax                       ; Push EIP (well, it will be EIP Smile.

        pusha
       ;'''''''''''''''''''''''''''''';
      ; Swap to stack 0. Smile         ;
    ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;
    mov     edi, _esp
   mov     esi, edi
    lodsd
       xchg    eax, esp
    stosd
       lodsd
       xchg    eax, ebp
    stosd
       ;'''''''''''''''''''''''''''''';
      ; Goto task 0.  .\m/           ;
   ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;
    sti
 jmp     Task0                  ; Goto Task 0.
    

Now in theory, it should I should be able to simply swap back to Task1 By switching the Stack and using an iret, this works perfectly! Here is the task switching code:
Code:
Timer_handle:
; The CPU has pushed the following onto the stack.
; eflags, cs, eip.
   cli
 pusha                         ; Save regs.
  ;''''''''''''''''''''''''''''';
        ; Swap ESP's stack locations. ;
    ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;
     mov     esi, _esp             ; ESP save spot into ESI.
     mov     edi, esi              ; Put it into EDI as well.
    lodsd                         ; Load double word
    xchg    eax, esp              ; exchange EAX and ESP.
       stosd                         ; Save old ESP.
       ;''''''''''''''''''''''''''''';
        ; Swap EBP's location to s1   ;
    ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;
     lodsd                         ; Get old EBP
 xchg    eax, ebp              ; exchange EAX and EBP.
       stosd                         ; Store old EBP.
      popa                          ; Restore regs.
       ; Normaly you would want to use STI, but there is no need
   ; As IRET pops the eflags reg of the stack, there is a 99%
  ; chance that interupts are enabled Smile
     iret
_esp:       dd stack0_base ; Extra stack pointer save location.
 dd 0x00000000  ; Extra base pointer save location.
stack0_base = 0x00007100
stack1_base = 0x00007200
    


Now like said It switches me to task1 perfectly, but then it freezes... I have been looking through this code for... a while, and I can't seem to find the problem, it works fine in my head, but sadly I have found that just because it works in my head, does not meen it works in real life Sad

Full source attached below. Anyone who can give pointers I will truly be thankful Very Happy

Cheers!

***** I have removed the source code, but you can get the fixed copy on This Post. *****

_________________
http://codercat.org/


Last edited by Coty on 06 Jan 2011, 21:08; edited 2 times in total
Post 06 Jan 2011, 16:52
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 06 Jan 2011, 18:11
Do you send a end of irq
eg:
Code:
 mov   al,0x20
       out   0xa0,al
       out   0x20,al
    

At the end of your Timer_handle ?.
Post 06 Jan 2011, 18:11
View user's profile Send private message Reply with quote
Coty



Joined: 17 May 2010
Posts: 553
Location: ␀
Coty 06 Jan 2011, 19:05
Laughing That makes me feel stupid, to be honest this was my third(?) code attempt at this so no wonder it slipped my mind, well thats one problem fixed, thanks Smile

Now it is going "ABAAAA..." but at-least it isn't sticking anymore, its probably a problem as small as this one... I think I will take a break, if I have anymore problems I'll ask.

_________________
http://codercat.org/
Post 06 Jan 2011, 19:05
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 06 Jan 2011, 20:29
Thats some cool code, I did notice this
Code:
;=============================================================================;

; Task A and B go here...                                                     ;

;=============================================================================;

Task0:

  Mov      ah, 0                 ; INT 0x70, AH = 0, Print Char

   Mov      al, 'A'             ; Print an A

     Int    0x70                    ; Print
      hlt                            ; print only 1 char each time the task

                                  ; is returned to...
  Jmp     Task0

end_0:



Task1:

       Mov      ah, 0                 ; INT 0x70, AH = 0, Print Char

   Mov      al, 'B'             ; Print an B

     Int    0x70                    ; Print
      hlt                            ; print only 1 char each time the task

                                  ; is returned to...

      Jmp     Task0    ;SHOULD THIS NOT BE Task1 ?
end_1:
    
I only know where to look, because i have made the same mistakes as you Laughing


Last edited by Dex4u on 06 Jan 2011, 21:13; edited 2 times in total
Post 06 Jan 2011, 20:29
View user's profile Send private message Reply with quote
Coty



Joined: 17 May 2010
Posts: 553
Location: ␀
Coty 06 Jan 2011, 21:05
Dex4u wrote:

Code:
       Jmp     Task0    ;SHOULD THIS NOT BE Task1 ?
end_1:
    


Shocked Well, I guess that what I get when I just copy/paste task A Laughing

Dex4u wrote:
I only know where to look, because i have made the same mists as you Laughing

In that case someday maby I will know were to look to Smile

I give you a million thanks!, but If I typed them all it would probably be considered a waste of bandwidth Very Happy


I have attached below a version with the problems fixed and a memory map for future reference/if anyone is interested.

Cheers!


Description:
Download
Filename: boot_2.zip
Filesize: 5 KB
Downloaded: 365 Time(s)


_________________
http://codercat.org/
Post 06 Jan 2011, 21:05
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 06 Jan 2011, 21:09
Coty wrote:

I have attached below a version with the problems fixed and a memory map for future reference/if anyone is interested.

Cheers!

Cool thanks.
Post 06 Jan 2011, 21:09
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.