OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 3:14 am

All times are UTC - 6 hours




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

Joined: Sun Oct 21, 2018 1:37 pm
Posts: 38
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: 5137
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
Member
Member

Joined: Sun Oct 21, 2018 1:37 pm
Posts: 38
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: 1108
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: 5137
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
Member
Member

Joined: Sun Oct 21, 2018 1:37 pm
Posts: 38
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: 74
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 :)

_________________
https://blackdev.org/ - system programming, my own 64 bit kernel and software.


Top
 Profile  
 
 Post subject: Re: CS reset to 0 creates a loop
PostPosted: Sat Dec 01, 2018 6:44 am 
Offline
Member
Member

Joined: Sun Oct 21, 2018 1:37 pm
Posts: 38
akasei wrote:
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 :)


Thanks for the tips Akasei.

The rest of the code was still work in progress, so do not pay much attention to it :-)


Top
 Profile  
 
 Post subject: Re: CS reset to 0 creates a loop
PostPosted: Sun Dec 02, 2018 3:26 pm 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 201
I've just tried it with nasm, yasm and as. None of them complained. Not even a warning. I've never noticed that. Nice catch.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: SemrushBot [Bot] and 279 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