PDA

View Full Version : cs401 assignment no 4 idea solution on 3rd of January



rabeel
01-03-2012, 11:24 PM
Sample output (each output line is result of pressing s key):
2 1 4 3 6 5 8 7 10 9
1 2 4 3 6 5 8 7 10 9
1 2 4 3 6 5 8 7 10 9
1 2 3 4 6 5 8 7 10 9
.
.
.
.
1 2 3 4 5 6 7 8 9 10


HINT:
For checking whether s key is pressed, you need to hook keyboard interrupt.

Vuhelper
01-08-2012, 09:34 PM
SOLUTION:

001 ; elementary multitasking of two threads

002 [org 0x0100]

003 jmp start

004

005 ; ax,bx,ip,cs,flags storage area

006 taskstates: dw 0, 0, 0, 0, 0 ; task0 regs

007 dw 0, 0, 0, 0, 0 ; task1 regs

008 dw 0, 0, 0, 0, 0 ; task2 regs

009

010 current: db 0 ; index of current task

011 chars: db ‘\|/-’ ; shapes to form a bar

012

013 ; one task to be multitasked

014 taskone: mov al, [chars+bx] ; read the next shape

015 mov [es:0], al ; write at top left of screen

016 inc bx ; increment to next shape

017 and bx, 3 ; taking modulus by 4

018 jmp taskone ; infinite task

019

020 ; second task to be multitasked

021 tasktwo: mov al, [chars+bx] ; read the next shape

022 mov [es:158], al ; write at top right of screen

023 inc bx ; increment to next shape

024 and bx, 3 ; taking modulus by 4

025 jmp tasktwo ; infinite task

026

027 ; timer interrupt service routine

028 timer: push ax

029 push bx

030

031 mov bl, [cs:current] ; read index of current task

032 mov ax, 10 ; space used by one task

033 mul bl ; multiply to get start of task

034 mov bx, ax ; load start of task in bx

035 vusolutions .com

036 pop ax ; read original value of bx

037 mov [cs:taskstates+bx+2], ax ; space for current task

038 pop ax ; read original value of ax

039 mov [cs:taskstates+bx+0], ax ; space for current task

040 pop ax ; read original value of ip

041 mov [cs:taskstates+bx+4], ax ; space for current task

042 pop ax ; read original value of cs

043 mov [cs:taskstates+bx+6], ax ; space for current task

044 pop ax ; read original value of flags

045 mov [cs:taskstates+bx+8], ax ; space for current task

046

047 inc byte [cs:current] ; update current task index

048 cmp byte [cs:current], 3 ; is task index out of range

049 jne skipreset ; no, proceed

050 mov byte [cs:current], 0 ; yes, reset to task 0

051

052 skipreset: mov bl, [cs:current] ; read index of current task

053 mov ax, 10 ; space used by one task

054 mul bl ; multiply to get start of task

055 mov bx, ax ; load start of task in bx

056

057 mov al, 020

058 out 020, al ; send EOI to PIC

059

060 push word [cs:taskstates+bx+8] ; flags of new task

061 push word [cs:taskstates+bx+6] ; cs of new task

062 push word [cs:taskstates+bx+4] ; ip of new task

063 mov ax, [cs:taskstates+bx+0] ; ax of new task

064 mov bx, [cs:taskstates+bx+2] ; bx of new task

065 iret ; return to new task

066

067 start: mov word [taskstates+10+4], taskone ; initialize ip

068 mov [taskstates+10+6], cs ; initialize cs

069 mov word [taskstates+10+8], 00200 ; initialize flags

070 mov word [taskstates+20+4], tasktwo ; initialize ip

071 mov [taskstates+20+6], cs ; initialize cs

072 mov word [taskstates+20+8], 00200 ; initialize flags

073 mov word [current], 0 ; set current task index

074

075 xor ax, ax

076 mov es, ax ; point es to IVT base

077 cli

078 mov word [es:8*4], timer

079 mov [es:8*4+2], cs ; hook timer interrupt

080 mov ax, 0xb800

081 mov es, ax ; point es to video base

082 xor bx, bx ; initialize bx for tasks

083 sti

084