Page 1 of 1

32bit registers in 16bit real mode.

Posted: Thu May 22, 2008 2:22 am
by jinksys
I've been trying to get back into writing a kernel, so I've been reading everything I can get my hands on. I stumbled upon brokenthorn.com and was reading this tutorial:

http://www.brokenthorn.com/Resources/OSDev4.html

When describing real mode the author writes...
# Is limited to 16 bit registers only.
Please tell me he's dead wrong.

Posted: Thu May 22, 2008 2:49 am
by AJ
He's dead wrong.

[edit]I've just e-mailed the author to let him correct it.[/edit]

Cheers,
Adam

Posted: Thu May 22, 2008 6:11 am
by Combuster
Officially you shouldn't use 32-bit code unless you know you have a 386+

But who cares? :D

Posted: Thu May 22, 2008 4:36 pm
by neon
Its been corrected--Sorry about that :)

Posted: Thu May 22, 2008 5:12 pm
by Osbios
I used 16bit only in the boot loader. Then I test for 386+ and stop and complain about the system if the CPU is too old.

Some detection for 386+

Code: Select all

 ;CPU detect  <80286, 80286, 80386+
 ;OUT: ax = CPU type ;destroys all other registers! no register save!!!
 ;0 = older then 286    O.o
 ;1 = 286               -_-
 ;2 = 386 or better     ^.^
init1.cpu.dedect:
  ;test 286
  pushf
  pop  bx
  and  bx, 0x0FFF
  push bx
  popf
  pushf
  pop  bx
  and  bx, 0x0F000
  cmp  bx, 0x0F000
 jne  .its286orbetter
  xor  ax, ax ;older then a 286
 ret
.its286orbetter:

  ;test 386 (Set bit18, read bit18, result=0 then <386)
  pushfd
  pop  eax
  or   eax, 0x40000 ;bit 18
  push eax
  popfd
  pushfd
  pop  eax
  and  eax, 0x40000
  cmp  eax, 0x40000

 je   .its386orbetter
  mov  ax, 1
 ret
.its386orbetter:
  mov  ax, 2
 ret

Posted: Fri May 23, 2008 11:00 am
by JAAman
that is (in my personal opinion) ugly and inefficient 386-detect code, as there is no reason to test 286 and 386 separately (this code was originally written on this forum 4+ years ago, to demonstrate the 386-detect algorithm, and surprised myself that it was actually correct, and with how well its written)

here is mine, from my bootloader:

Code: Select all

386detect:
	PUSHF
	MOV AX, 0xF000
	PUSH AX
	POPF
	PUSHF
	POP AX
					; REMOVED POPF
	AND AH, 0xF0
	JNG ErrorCrash:				; unrecoverable ERROR (#1 == no386)(z=0,s=0)