How do I Create my own thread handler

Started by www.:).com, November 05, 2010, 07:53:14 PM

Previous topic - Next topic

www.:).com

I am looking for a way to create a simple duel thread handler in 16-bit dos. I would like if someone could give me some code(with comments please, but without is ok to) for it that I can mess with. The main problem I am having is getting my thread handler to jump to a instruction then jump back to the handler. I use ml.exe and link16.exe to create my program and I use Dosbox to run the programs and I am only a beginner in masm, if it helps.

Any help would be appreciated, thanks :bg.

redskull

The normal method is to replace the IP saved on the stack during the call to the handler with the saved IP of the other thread, and then "returning" (a misnomer, because it will return to an entirely different thread).  FWIT you are in way over your head trying to do this, especially in DOS.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

www.:).com

Maybe so but, I'd still like to do it, thanks for your input.

www.:).com

The problem is that getting it to call back o he handler. I was thinking that if I found out how instructions are sored in memory i could set aside a section in memory then go to a thread, get the next instruction, store it in memory and directly after put "jmp (thread handler address here)" to get it to call back. After i would do this for the next thread then loop.  :dazzled:

Would this work?
-If this would I don't have enough knowledge to do it and please help.

www.:).com

Sorry for my last post my "t" key is broken on my keyboard and i keep forgetting to use a virtual keyboard. So If A word doesn't sound right like "o" or "he" it is most likely "to" or "the".

redskull

You can't just copy the instructions, for many reasons.  First and foremost, if it's a JMP instruction, then it will do just that, and never return.  Also, memory addressess will be all wrong.

There are two main ways to do: preemptive or cooperative.  In the latter, each thread must specifically call a function to return control to the handler, which is easier, but allows for misbehaving programs.  Because threads will run until they decide to give back control, the system is very fragile.  In the former, you have to hook the timer interrupt, and decide on every 'tick' whether to keep running the current thread or switch to a new one.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

www.:).com

I realize that if i were to jump to a location it will not return, but will keep running from that point. But will it work if I rearrange the code by putting it in a new part of memory, like i said in one of my other posts? What I was saying in that post was, that basically replace the code to be executed with a jump to an edited section of the code. Like (Code to execute) -followed by in memory the jump back to handler. Once executed and jumped back to handler the handler will resume he thread after the section that tells it to jump to the edited version of that part of code. Once done the old jump will be replaced with the original code and a new one created for the next line of code.

MichaelW

I'm having trouble understanding why you would take an already complex task and complicate it even further by moving instructions around in memory. This is not how threads are normally managed. What is your goal here?
eschew obfuscation

www.:).com

Well I'm new to asm and my goal is to create a simple two thread handler I plan on using this to handle drawing the mouse cursor and running the program at the same time(not literally at the same time, but instructions from each thread inter woven). I am currently looking into the possibility of using software interrupts to switch threads.

clive

You are much better off just trying co-operative multi-tasking in DOS. Basically having a Yield() type function and switching between tasks/threads in a round-robin fashion. The task switch involves stacking the current context, changing the stack to the next task and un-stacking the context and continuing. Each task has it's own stack and context. You can use interrupt to perform various operations, but I wouldn't use it to actually switch tasks as you may often find yourself interrupting DOS code, and DOS isn't very re-entrant or forgiving.

You might also look at things like uCOS/II, it used to have DOS based examples.
It could be a random act of randomness. Those happen a lot as well.

www.:).com

Ok, I'll try that but i would still like to know how to store instructions(and operands) for future reference.

clive

Quote from: www.:).com on November 06, 2010, 02:28:22 AM
Ok, I'll try that but i would still like to know how to store instructions(and operands) for future reference.

It's all bytes of data in memory, copy it from one place to another, or save it to a file.
It could be a random act of randomness. Those happen a lot as well.

MichaelW

Quotemy goal is to create a simple two thread handler I plan on using this to handle drawing the mouse cursor and running the program

The mouse cursor is normally drawn in sync with mouse motions, in response to a hardware interrupt. A thread that runs intermittently will not be able to reliably keep up with the motions.
eschew obfuscation

www.:).com

Quote from: clive on November 06, 2010, 03:43:51 AM
Quote from: www.:).com on November 06, 2010, 02:28:22 AM
Ok, I'll try that but i would still like to know how to store instructions(and operands) for future reference.

It's all bytes of data in memory, copy it from one place to another, or save it to a file.

Do you happen to know how many bytes can make up an instruction?

www.:).com

Quote from: MichaelW on November 06, 2010, 05:13:13 AM
Quotemy goal is to create a simple two thread handler I plan on using this to handle drawing the mouse cursor and running the program

The mouse cursor is normally drawn in sync with mouse motions, in response to a hardware interrupt. A thread that runs intermittently will not be able to reliably keep up with the motions.


I realize that, but I think that is only when you are using a default cursor such as:

mov ax, 02
int 33h


I think this is to turn on the dos mouse.
If not please correct me.

I am using a custom mouse i get the current position of the mouse as follows:

mov ax, 03
int 33h ; cx now holds X coordinate dx holds Y coordinate

; convert X and Y coordinates to linear here
; draw to that location here