OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 1:10 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: assembly
PostPosted: Fri Jan 26, 2018 2:53 pm 
Offline
Member
Member

Joined: Fri Jan 26, 2018 11:43 am
Posts: 64
According to Protected_Mode, to enter protected mode you use this assembly:
Code:
cli            ; disable interrupts
lgdt [gdtr]    ; load GDT register with start address of Global Descriptor Table
mov eax, cr0
or al, 1       ; set PE (Protection Enable) bit in CR0 (Control Register 0)
mov cr0, eax

; Perform far jump to selector 08h (offset into GDT, pointing at a 32bit PM code segment descriptor)
; to load CS with proper PM32 descriptor)
jmp 08h:PModeMain

PModeMain:
; load DS, ES, FS, GS, SS, ESP

Which I'm sure does work, but it seems like a strange way of doing it. I have fairly limited knowledge of nasm compared to - I assume - whoever wrote this code, but why do they first move cr0 to eax, then set the bit to say it's protected mode, and then move eax back into cr0?
I though - according to CR0 - that the cr0 register was read/write,
Quote:
unlike the other that can be accessed only via the MOV instruction

Is there any reason why it's done this way?


Top
 Profile  
 
 Post subject: Re: assembly
PostPosted: Fri Jan 26, 2018 3:35 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
You can’t perform ALU operations on control registers directly. There are no instructions for that. You can only move to and from them. Altering system registers is done rarely compared to general computational tasks. So, there’s no reason to penalize the latter by making longer ALU instructions just to make them able to operate on more registers. See, every register is encoded by its number in an instruction. The more registers you allow in an instruction, the more bits are needed to encode each register by its number. Longer instructions need more memory and specifically larger instruction caches. You should also keep in mind that the 80386 control registers appeared several years after the 8086/8088 ALU instructions and the control registers couldn’t be incorporated into the already fixed ALU instructions. They could be extended by way of instruction prefixes, but it would increase complexity without much gain.


Top
 Profile  
 
 Post subject: Re: assembly
PostPosted: Fri Jan 26, 2018 4:21 pm 
Offline
Member
Member
User avatar

Joined: Sun Jan 13, 2013 6:24 pm
Posts: 90
Location: Grande Prairie AB
The reason the control register is read first, is that is the only way of only setting bit 0 or any bit for that matter without altering the rest. Otherwise, if you did something like;
Code:
        mov     eax, 1
        mov     cr0, eax
then any of the other bits that are writable would be set to zero.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 36 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