OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Nov 15, 2018 12:01 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: CS reset to 0 creates a loop
PostPosted: Tue Oct 30, 2018 3:31 am 
Offline

Joined: Sun Oct 21, 2018 1:37 pm
Posts: 5
Hello everybody,

just a simple question that bugs me. In this code below, the loader loops on "MOV CS, AX" (AX = 0), even when CS was already 0 so no changes happened to the register.

I assume something is triggered behind (maybe some microcode does something on IP when CS is changed?) but I just wanted to understand why.

I am aware the solution is just not reset it, but I would like to understand the behavior so I can change the CS value in the future with guarantees of knowing what I am doing.

Any hint, observation, suggestion or comment will be very welcomed !

Thanks in advance!!!!

Image

Image

Image

After this, it goes back to the MOV instruction, and CS is 0 again... but it just loops there.


Top
 Profile  
 
 Post subject: Re: CS reset to 0 creates a loop
PostPosted: Tue Oct 30, 2018 3:43 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 1334
That's not a valid instruction. It's causing an invalid opcode exception, and since you haven't installed a handler for that, the handler installed by the BIOS is running. The BIOS handler does nothing and returns to the invalid opcode, which causes the same exception again, and loops forever.

If you need to set CS, use a far JMP.


Top
 Profile  
 
 Post subject: Re: CS reset to 0 creates a loop
PostPosted: Tue Oct 30, 2018 3:51 am 
Offline

Joined: Sun Oct 21, 2018 1:37 pm
Posts: 5
Octocontrabass wrote:
That's not a valid instruction. It's causing an invalid opcode exception, and since you haven't installed a handler for that, the handler installed by the BIOS is running. The BIOS handler does nothing and returns to the invalid opcode, which causes the same exception again, and loops forever.

If you need to set CS, use a far JMP.


Thanks for the explanation! It is clear now. However, this makes me question why NASM assembler did not complain. Does this mean that this operation is allowed in some other scenarios (maybe different mode)?

Thanks!!


Top
 Profile  
 
 Post subject: Re: CS reset to 0 creates a loop
PostPosted: Tue Oct 30, 2018 4:02 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 907
mihe wrote:
However, this makes me question why NASM assembler did not complain. Does this mean that this operation is allowed in some other scenarios (maybe different mode)?


It's possible it worked on something ancient like the 8086/8088. They had POP CS. But then the opcode for POP CS was reused as a prefix for new 80186/80286 instructions. This page suggests the move worked.


Top
 Profile  
 
 Post subject: Re: CS reset to 0 creates a loop
PostPosted: Tue Oct 30, 2018 4:36 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 1334
I'm not sure why NASM didn't complain. Possibly they didn't feel the need to catch this specific situation.

And yes, it did work on the 8086 and 8088. It's not especially useful though. You can't use it in a bootloader to set CS, since you don't know what IP will be.


Top
 Profile  
 
 Post subject: Re: CS reset to 0 creates a loop
PostPosted: Tue Oct 30, 2018 5:32 am 
Offline

Joined: Sun Oct 21, 2018 1:37 pm
Posts: 5
Thank you both for your answers.


Top
 Profile  
 
 Post subject: Re: CS reset to 0 creates a loop
PostPosted: Tue Nov 13, 2018 3:39 pm 
Offline
Member
Member
User avatar

Joined: Tue Feb 11, 2014 4:59 pm
Posts: 44
First of, why didn't you stop the code by using "jmp $" instruction at line 19?
Second, why there is no loop in "print_real" procedure at line 32? It just leaves this procedure after "lodsb" instruction :) and do nasty things. Just first look at this code makes me angry :twisted:
Third, please try to study my code of bootloader. It's well commented. https://gitlab.com/akasei/Zero/tree/master
Fourth, I'm glad that You use Bochs Enchanced Debugger. It's very powerfull tool :)

_________________
wataha.net - system programming, my own 64 bit kernel and software.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group