assembly

Programming, for all ages and all languages.
Post Reply
j4cobgarby
Member
Member
Posts: 64
Joined: Fri Jan 26, 2018 11:43 am

assembly

Post by j4cobgarby »

According to Protected_Mode, to enter protected mode you use this assembly:

Code: Select all

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,
unlike the other that can be accessed only via the MOV instruction
Is there any reason why it's done this way?
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: assembly

Post by alexfru »

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.
User avatar
TightCoderEx
Member
Member
Posts: 90
Joined: Sun Jan 13, 2013 6:24 pm
Location: Grande Prairie AB

Re: assembly

Post by TightCoderEx »

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: Select all

        mov     eax, 1
        mov     cr0, eax
then any of the other bits that are writable would be set to zero.
Post Reply