2

How can you forcibly change CS and IP both in assembly language ?

ORG directive can be used to change the number of the IP, but how do you change the CS?

Basically I wan to implement multi-threading using assembly.

Many forums, including a question in stack overflow has said its impossible, but then how does C have multi-threading options even when it is made from assembly code ?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Possible duplicate: http://stackoverflow.com/questions/714905/threads-in-x86-assembler-using-the-gnu-assember-as – Macmade Sep 17 '12 at 17:11
  • 1
    ORG doesn't change IP, it changes the number the assembler uses when it needs IP. "Changing" CS in an equivalent way would do absolutely nothing because the assembler doesn't use that value anyway. – harold Sep 17 '12 at 17:11
  • @harold Depends on the memory model... – Macmade Sep 17 '12 at 17:13
  • ya exactly, thanks but how do you change the number of CS ? – Total Anime Immersion Sep 17 '12 at 17:13
  • @Macmade I saw that question itself before posting this but it was quite vast and didn't solve my doubt, that's why i re-posted a smaller question. :) – Total Anime Immersion Sep 17 '12 at 17:14
  • Changing IP cannot be done directly, but it changes when you 'jump'. About threads, either you use the OS with a syscall, or you write your own scheduler. – Macmade Sep 17 '12 at 17:18
  • @harold - Why i need that is, if i am able to change the number of cs, then probably i maybe able to find out a way to do multi-threading in assembly. – Total Anime Immersion Sep 17 '12 at 17:19
  • @TotalAnimeImmersion so that's the *runtime* CS then, right? See far jumps, far calls and far returns. – harold Sep 17 '12 at 17:21
  • Thanks @Macmade but could you guide me to any resource. Basically I am working on making an OS, so i want it to have multi-threading capabilities and am not interested in doing it in C. If possible, please suggest me a method or a link. Would be of great help. – Total Anime Immersion Sep 17 '12 at 17:22
  • @TotalAnimeImmersion See this: http://wiki.osdev.org/Processes_and_Threads – Macmade Sep 17 '12 at 17:26

4 Answers4

7

To change cs:ip just make a long jump with jmp (eg. jmp segment:offset) or a long call (eg. call segment:offset) depending on your needs. There are several different addressing modes available for jmp link and call link. Implementing multithreading is a totally different matter from simply changing cs:ip.

nrz
  • 10,435
  • 4
  • 39
  • 71
  • Could you help me with implementation of multi-threading. In C it is possible but in Assembly, people say it isn't. How is that is when C is made using Assembly? – Total Anime Immersion Sep 17 '12 at 23:18
  • 3
    As assembly is a more or less direct representation of processor's instruction set, any program possible in any other programming language can be written in assembly too (at least in theory), and it's also possible to disassemble any given program. As answers to http://stackoverflow.com/questions/714905/threads-in-x86-assembler-using-the-gnu-assember-as suggest, you need to either use kernel's scheduler or write your own scheduler. http://stackoverflow.com/questions/980999/what-does-multicore-assembly-language-look-like has useful info. You need to choose the type of multithreading you want. – nrz Sep 18 '12 at 01:41
  • @TotalAnimeImmersion If you want to write your own multithreading operating system from scratch or at least write your own scheduler, I believe [Intel 64 and IA-32 Architectures Software Developer's Manual](http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-manual-325462.html) will be very useful. Chapter 8 (Multiple-Processor Management) of Volume 3 (System Programming Guide) has a lot of information about how to implement multithreading. – nrz Sep 18 '12 at 06:43
3

I think you could use just

jmp segment:offset

call segment:offset
Pyjong
  • 3,095
  • 4
  • 32
  • 50
1

Only far control transfer instructions (jmp, call, ret) can change the CS or EIP registers. I think the CS register can only be changed in real mode.

RET basically takes the value at ESP (stack pointer) and pushes that onto IP/EIP. Then the ESP is incremented by 8 plus the immediate offset (if exists).

1

To change both cs and ip registers, use the following in AT&T syntax:

ljmp $segment, $offset

Or the following in Intel syntax:

jmp segment:offset
Akib Azmain Turja
  • 1,142
  • 7
  • 27