Page 1 of 1

very strange behaviour indeed...

Posted: Wed Jul 25, 2007 4:22 pm
by sancho1980
hi

i just came across some very strange behaviour of the nasm compiler
take a look at this piece of code:

Code: Select all

lgdt [gdt_desc]

	mov eax, cr0	;Enable

	or eax, 1	;protected

	mov cr0, eax	;mode

	jmp 20h:enter_32 ;where 20h is selector for this very segment




	BITS 32

enter_32:

	hlt	
	jmp enter_32
if i execute this, the screen completely blanks upon the jump to enter_32

now if i leave out "BITS 32", the screen stays as it is!

is there a sensible explanation for this??

Posted: Wed Jul 25, 2007 4:26 pm
by Alboin
I think you have to disable interrupts before you enter pmode....

Posted: Wed Jul 25, 2007 4:27 pm
by sancho1980
yeah i did that! forgot to post that line

Code: Select all

cli
comes just before

Code: Select all

lgdt [gdt_desc]

Posted: Wed Jul 25, 2007 11:26 pm
by pcmattman
All the 'bits 32' prefix does is modify the opcodes to have a special prefix, iirc.

Posted: Thu Jul 26, 2007 12:55 am
by JamesM
Have you traced it through bochs?

Posted: Thu Jul 26, 2007 2:59 am
by sancho1980
i still havent figured out how to do that
but would a simple "hlt" instruction be compiled differently depending on whether BITS 32 or BITS 16 is enabled??

Re: very strange behaviour indeed...

Posted: Thu Jul 26, 2007 3:42 am
by XCHG
sancho1980 wrote:hi

i just came across some very strange behaviour of the nasm compiler
Remember that it is almost never the behavior of NASM that is wrong. You are doing something wrong and unfortunately, given the code snippet that you have put in your post, it is impossible to find the problem.

Posted: Thu Jul 26, 2007 7:41 am
by sancho1980
Ok, I had attached these files in relation to a less specific question in a different thread already, but I'll attach them yet again so you see exactly what I mean. the code does the following:

-loads 512 byte boot sector (512.asm) (well thats done by the bios)
-the 512 bytes then relocate to 0500h
-then the gdt (gdt.asm) is read from disk into 010700h (gdt-reserved space til 0126ffh)
-012700h-0146ffh shall be used for the idt
-then the c-code shall eventually be loaded at 014700h, but the jump to it is was taken out (because i don get it compiled properly)

there is also a makefile which i cant attach:

Code: Select all

all: Makefile
   make bootdisk.bin
bootdisk.bin: 512.bin gdt.bin boot.bin Makefile
   -rm bootdisk.bin
   touch bootdisk.bin
   dd if=512.bin of=bootdisk.bin count=1 seek=0
   dd if=gdt.bin of=bootdisk.bin count=1 seek=1
   dd if=boot.bin of=bootdisk.bin count=1 seek=3
512.bin: 512.asm Makefile
   -rm 512.bin
   nasm -f bin 512.asm -o 512.bin
gdt.bin: gdt.asm Makefile
   -rm gdt.bin   
   nasm -f bin gdt.asm -o gdt.bin
boot.bin: boot.o Makefile
   -rm boot.bin
   ld -o boot.bin -Ttext 0x0 -e main --oformat binary boot.o
boot.o: boot.c Makefile
   -rm boot.o
   gcc -ffreestanding -c boot.c -o boot.o
If you compile all this and boot from bootdisk.bin, the code will hang (as intended) with a blank screen (unintended)
But if you leave out the line that says "BITS 32" (i.e. just the single line BEFORE enter_32: in "512.asm"), compile again and boot, then the code will hang (as expected) AND you'll still see the output from when the code was still in real mode...just have alook at the code, its very heavily commented and pretty self-explanatory,,i'm really stuck here! it really seems the compiler produces different output for this single near-jump instruction..and funnily, it seems to produce the right output when youre actually using the wrong compiler option!

Posted: Thu Jul 26, 2007 9:46 am
by sancho1980
found the problem! dont bother :-)
yes it was me not the compiler :oops:
such trivial things drive me crazy..i spent 2 days looking for the cause of this!!!!! and it was so trivial...