Assalam o Alaikum to all.

Dear friends I want to know what are the thread control instructions in multitasking of assembly? Can anyone please help me? Thanks in advance

Recommended Answers

All 5 Replies

What processor? There is no such thing on Intel or Intel compatible processors that I'm aware of. Implementing threading in assembly language would be exceedingly difficult. Better to do it in a higher language such as C.

The processor is Intel IAPX88 Architecture

;elementary multitasking of two threads
[org 0x0100]
                jmp start

                ;      ax, bx, ip, cs, flags storage area
taskstates:     dw      0, 0, 0, 0, 0           ;task0 regs
                dw      0, 0, 0, 0, 0           ;task1 regs
                dw      0, 0, 0, 0, 0           ;task2 regs

current:        db      0                       ;index of current task
chars:          db      '\|/-'                  ;shapes to form a bar

;one task to be multitasked
taskone:        mov al, [chars+bx]              ;read the next shape
                mov [es:0], al                  ;write at the top left of screen
                inc bx                          ;increment to next shape
                and bx, 3                       ;taking modulus by 4
                jmp taskone                     ;infinite task

;second task to be multitasked
tasktwo:        mov al, [chars+bx]              ;read the next shape
                mov [es:158], al                ;write at top right of screen
                inc bx                          ;increment to next shape
                and bx, 3                       ;taking modulus by 4
                jmp tasktwo                     ;infinite task
;timer interrupt service routine
timer:          push ax
                push bx

                mov bl, [cs:current]            ;read index of current task
                mov ax, 10                      ;space used by one task
                mul bl                          ;multiply to get start of task
                mov bx, ax                      ;load start of task in bx

                pop ax                          ;read original value of bx
                mov [cs:taskstates+bx+2], ax    ;space for current task
                pop ax                          ;read original value of ip
                mov [cs:taskstates+bx+4], ax    ;space for current task
                pop ax                          ;read original value of cs
                mov [cs:taskstates+bx+6], ax    ;space for current task
                pop ax                          ;read original value of flags
                mov [cs:taskstates+bx+8], ax    ;space for current task

                inc byte [cs:current]           ;update current task index
                cmp byte [cs:current], 3        ;is task index out of range
                jne skipreset                   ;no, proceed
                mov byte [cs:current], 0        ; yes, reset to task 0

skipreset:      mov bl, [cs:current]            ;read index of current task
                mov ax, 10                      ;space used by one task
                mul bl                          ;multiply to get start of task
                mov bx, ax                      ;load start of task in bx

                mov al, 0x20
                out 0x20, al                    ;send EOI to PIC

                push word [cs:taskstates+bx+8]  ;flags of new task
                push word [cs:taskstates+bx+6]  ;cs of new task
                push word [cs:taskstates+bx+4]  ;ip of new task
                mov ax, [cs:taskstates+bx+0]    ;ax of new task
                mov bx, [cs:taskstates+bx+2]    ;bx of new task
                iret                            ;return to new task

start:          mov word [taskstates+10+4], taskone     ;initialize ip
                mov [taskstates+10+6], cs               ;initialize cs
                mov word [taskstates+10+8], 0x0200      ;initialize flags
                mov word [taskstates+20+4], tasktwo     ;initialize ip
                mov [taskstates+20+6], cs               ;initialize cs
                mov word [taskstates+20+8], 0x0200      ;initialize flags
                mov word [current], 0                   ;set current task index

                xor ax, ax
                mov es, ax                      ;point es to IVT base
                cli
                mov word [es:8*4], timer
                mov [es:8*4+2], cs              ;hook timer interrupt
                mov ax, 0xb800
                mov es, ax                      ;point es to video base
                xor bx, bx                      ;initialize bx for tasks
                sti

                jmp $

The thread control instruction is in this code but I am unable to find that instruction. Kindly help me to find out that thread control instruction

See line 62. iret instruction returns control from an interrupt handler. On MS-DOS interrupt handlers are common to process hardware and software interrupts.

The program actually begins at the label named Start on line 64. It initializes a timer interrupt handler then jumps somewhere (line 82).

These are not really threads as we know them in MS-Windows and *nix. Nor will you be able to run that assembly code under MS-Windows.

This is the section where task switching is made.

Has 3 tasks switching one after the other, the calling task (program itself) and taskone and tasktwo.
Seems to be a sample program that writes something on the screen.
Can not test because I dont have DOS and my WIN XP does not accept to hook "DOS" interrupts I think.

[U]inc byte [cs:current][/U] ;update current task index
cmp byte [cs:current], 3 ;is task index out of range
jne skipreset ;no, proceed
mov byte [cs:current], 0 ; yes, reset to task 0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.