Page 1 of 1

Bochs wrong instruction executed

Posted: Sat May 27, 2017 6:53 am
by atilali
Hello, i am debugging my code on bochs. at a point as you can see cs:ip is 0x0000:0x7eef and the next instruction is supposed to be at 0x0000:0x7ef2. but when i execute "s 1" command( which executes only one instruction), it executes "push ax" at address f000:e9df. do you have any idea why i get such a behaviour?
picture : http://i.imgur.com/H4vhB1g.jpg

Re: Bochs wrong instruction executed

Posted: Sat May 27, 2017 7:28 am
by iansjack
I can't really read your screen shot, but I'm guessing you have triggered an exception and are seeing the first instruction of the exception handler.

Re: Bochs wrong instruction executed

Posted: Sat May 27, 2017 7:34 am
by Octocontrabass
It looks like Bochs is telling you exactly what's wrong.

Code: Select all

00014176438e[CPU0 ] write_virtual_word_32(): segment limit violation

Re: Bochs wrong instruction executed

Posted: Sat May 27, 2017 7:47 am
by atilali
I am in real mode, do you have any idea about the triggered exception by executing the following commands?

Code: Select all

    7eef:	67 89 02             	mov    WORD PTR [edx],ax
    7ef2:	66 a1 1c 00          	mov    eax,ds:0x1c
(disassembly has generated by OBJDUMP command)

Re: Bochs wrong instruction executed

Posted: Sat May 27, 2017 7:51 am
by Brendan
Hi,
atilali wrote:I am in real mode, do you have any idea about the triggered exception by executing the following commands?

Code: Select all

    7eef:	67 89 02             	mov    WORD PTR [edx],ax
    7ef2:	66 a1 1c 00          	mov    eax,ds:0x1c
In real mode the segment limits are all 64 KiB, so if EDX contains a value that is larger than 0x0000FFFC (which is "0x00010000 - 4") when you execute "mov WORD PTR [edx],ax" you get a General Protection Fault (exception) because of a segment limit violation.


Cheers,

Brendan

Re: Bochs wrong instruction executed

Posted: Sat May 27, 2017 8:00 am
by atilali
Thank you Brendan!

EDX had the value of 0xb8000. Is it allowed to use EDX register for addressing like that(MOV WORD PTR [edx],ax) in real mode? I didn't know that i am unable to address beyond 0xFFFC even though it's allowed to use edx(if it is allowed). i will use segment:offset addresing to fix the problem.

Re: Bochs wrong instruction executed

Posted: Sat May 27, 2017 8:53 am
by Brendan
Hi,
atilali wrote:Is it allowed to use EDX register for addressing like that(MOV WORD PTR [edx],ax) in real mode?
Yes. In real mode (and in 16-bit code in general - e.g. including 16-bit protected mode code) the default is "16 bit", but that default can be overridden with instruction prefixes and nothing prevents you from using 32-bit registers/addresses/instructions (with appropriate size override prefixes) if the CPU supports it (80386 or later).

In 32-bit code it's the opposite - the default is "32 bit", but the same prefixes can be used to override that if you want to use 16-bit registers/addresses/instructions. For 64-bit code things get a little strange - the default is typically "32-bit", and prefixes can be used to get 16-bit or 64-bit.

Fortunately the assembler will handle the size override prefixes for you (you can just do whatever you like and let the assembler figure them out.


Cheers,

Brendan