YJDoc2 / 8086-emulator-web

Repository for 8086 emulator web implementation
Apache License 2.0
231 stars 27 forks source link

Could you please add CS, IP register? #18

Open wathenjiang opened 3 years ago

wathenjiang commented 3 years ago

As we know, 8086CPU has 14 registers, but 8086-emulator-web lacks both of CS and IP.

I want to verify this theory that jmp ax:bx semantically equivalent to execute mov cs,ax & mov ip,bx.

Could you please add CS & IP? Or just tell me why this emulator does not include CS and IP.

wathenjiang commented 3 years ago

And it seems do not support all feature of jmp command, image ref: wiki-JMP (x86 instruction)

YJDoc2 commented 3 years ago

Hey, Thanks for using the emulator!

So the answer for both of your questions is same : it is because the emulator does not translate the instructions to an actual bytecode.

The way emulator works now, is in first pass it converts the code to a simple intermediate representation, which is text based. Then instead of converting that to bytecode, which would be stored in memory, the actual running of program is done directly from this text-based IR, where each instruction is parsed and evaluated each time it is to be run.

Ideally, the IR should be converted to a bytecode which then should be stored in the memory ; but when making this, we could not find a definitive definition of Hex codes for 8086 assembly. The few sources which we found seemed to disagree with each other and did not cover all the instructions, so we kept the IR text-based. (We could have defined out own bytecode, but we didn't).

Thus we cannot actually store the instructions in the memory like the data is stored. Hence there is no meaning of CS and IP, as we cannot treat any place in memory as the "code" segment, and the IP is maintained internally by the emulator, and it should be modified.

For the same reason, the jmp instruction only supports jumping using labels, as any memory address given explicitly or in register cannot be an instruction address.

In case you have any idea about 8086 asm hex codes, we can try implementing that, or if you have any idea of making a consistent bytecode for all ~100 instructions, please feel free to mention!

wathenjiang commented 3 years ago

Thx for your replying.

Your project is awsome. When the code work with memory, I found it does not work well. Such as mov [0],cx, cs:ip.

The implementation details you gave us tells me the reson why such type of code does not work well.

YJDoc2 commented 3 years ago

Your project is awsome

Thank you!

When the code work with memory, I found it does not work well. Such as mov [0],cx, cs:ip.

For the reasons explained above, cs and ip will not work in code. For the mov [0], cx example, it will give error because you need to explicitly tell that the memory you are giving the address of should be treated as byte or word. That way mistakenly using cx which is a 16 bit register when you wanted to move 8-bit value is prevented. The correct syntax for this in the emulator is mov word [0], cx.

You can check out the instructions page on the website : https://yjdoc2.github.io/8086-emulator-web/help for details of the instruction syntax. This lists all the instructions, and the operands supported by them. For example , for move : syntax of mov instruction

This specifies, that for mov instruction using memory as one of the operand, you need to add "byte" or "word" in front of it.

Hope this will be helpful.