Page 1 of 1

Author:  j4cobgarby [ Fri Jan 26, 2018 2:53 pm ]
Post subject:  assembly

According to Protected_Mode, to enter protected mode you use this assembly:
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

; 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?

Author:  alexfru [ Fri Jan 26, 2018 3:35 pm ]
Post subject:  Re: assembly

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.

Author:  TightCoderEx [ Fri Jan 26, 2018 4:21 pm ]
Post subject:  Re: assembly

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;
        mov     eax, 1
        mov     cr0, eax
then any of the other bits that are writable would be set to zero.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group