Page 1 of 6

MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 11:04 am
by RGOS
Hello,

I have some kernelloader-code, but I can't get it to work, because I can't move 0x0000 into AX, if I use BX it works but then I cant move 0xFFFF into SP, it's for the stack creation.
Any help would be appreciated.
The code is in the attachment.
The trouble is here:

Code: Select all

cli
xor		ax, ax
mov		ds, ax
mov		es, ax
mov		ax, 0x0000  ;The trouble.
mov		ss, ax      ;If i change AX into BX (here, and above) then it works.
mov		sp, 0xFFFF  ;Until here, this then still doesn't work.
sti
Thanks.

PS. Sorry for my English, I'm Dutch.

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 12:46 pm
by Gigasoft
Which error messages do you get from NASM?

By the way, your CS is set to 0x50 while DS and ES are set to 0. You have to use org 0x500 and use a CS of 0, or set both CS, DS and ES to 0x50 and add 0x500 to all the offsets in your protected mode code (if this uses 0-based segments).

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 12:56 pm
by neon
Hello,

Why bother resetting it to 0 when it was already set by your XOR operation?

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 1:53 pm
by geppyfx
Do any of your included FILES\...\...inc contain 'bits 32' ?
I suspect yes because 'EnablePaging' executes in 'bits 32' section and probably lies in "FILES\NewTry\Paging.inc"

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 2:08 pm
by RGOS
Gigasoft wrote:Which error messages do you get from NASM?

By the way, your CS is set to 0x50 while DS and ES are set to 0. You have to use org 0x500 and use a CS of 0, or set both CS, DS and ES to 0x50 and add 0x500 to all the offsets in your protected mode code (if this uses 0-based segments).
Hello,

I get no errors from NASM, but when I try to run my compiled OS in Bochs or VMWare it just stops at the point where I put 0x0000 into AX, I checked it by HLTing the CPU (Bochs and VMWare say when this happens, so I can look till where the excecution goes).
Thanks for the information about CS and ORG, my start looks like this now:

Code: Select all

bits	16
org		0x0500
start: jmp 0x0000:main

%define IMAGE_LMODE_BASE 0x100000
%define IMAGE_PMODE_BASE 0x100000
%define IMAGE_RMODE_BASE 0x3000
%include "FILES\NewTry\stdio.inc"
%include "FILES\NewTry\gdt.inc"
%include "FILES\NewTry\A20.inc"
%include "FILES\NewTry\FAT12.inc"
%include "FILES\NewTry\Memory.inc"
%include "FILES\NewTry\Paging.inc"

prtok:
pusha
mov		si, ok
call   	Puts16
popa
ret

main:
xor		cs, cs
cli
xor		ax, ax
mov		ds, ax
mov		es, ax
mov		ax, 0x0000
mov		ss, ax
mov		sp, 0xFFFF
sti
Is this what you ment?

@ geppyfx:

Yes, the files "FILES\NewTry\stdio.inc", and "FILES\NewTry\Paging.inc" contain bits 32, do I need to upload them, and do you think that there's the problem?


Thank you all for the quick reply's.

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 2:31 pm
by Combuster
How are you using this code? What are your assumptions? What did you base your code on? Where is the bochs log?

Rest assured. MOV AX, <constant> always works the way the manuals say it does. That can not be the problem.

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 3:11 pm
by geppyfx
RGOS wrote:Is this what you ment?
yes, I think that nasm behaves same as fasm here and 'bits 32' inside included files affects your main 'kernel.asm' file. Bits 32 is not local to the included files. Add 'use 16' before "main:" and see if it fixes anything.

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 3:37 pm
by RGOS
Hello,

I'm using this code to create the stack, and I based the code on the BrokenThorn OSDev series.
The bochs log is attached.
I haven't used the Bochs logs that much, and I saw this piece of log that repeated some times, what is it, and what is the piece " BxError: Encountered an unknown instruction b1=0xff (signalling #UD)", and what is the 0xff instruction, I think that's the problem.

Code: Select all

00016083944i[CPU0 ] 0x00000000000009f4>> (invalid)  : FFFF
00016083944d[CPU0 ] exception(0x06): error_code=0000
00016083944d[CPU0 ] interrupt(): vector = 06, TYPE = 3, EXT = 1
00016083946d[CPU0 ] BxError: Encountered an unknown instruction b1=0xff (signalling #UD)
00016083946d[CPU0 ] modrm was 0xff, nnn was 7, rm was 7
I tried to run the code with the changes from the previous post, but XOR CS, CS doesn't work, and if i first put 0x0000 into AX, and then AX into CS it works (I get no errors from NASM) but then I get an error from VMWare, a kernel stack fault.
When I run it in Bochs I get this log: BochsLog_With_0x0000_in_CS.txt.
And Bochs keeps resetting.
I then disabled the XOR CS, CS so I only changed the ORG instruction, then I get this log: BochsLog_Without_0x0000_in_CS.txt.
I saw that when I exitted Bochs that the CPU was in protected mode, so we came a step further, and I'll try the 'use 16' in front of main:.
The Bochs logs are coming later, because they are so big, I'll post them later tonight.

Thanks.

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 3:52 pm
by RGOS
Hello,

Here are links to the logs:
BochsLog_With_0x0000_in_CS.txt.
BochsLog_Without_0x000_in_CS.txt.
BochsLogs.rar.
I included the .RAR because the logs themselves are so big.

Thanks, I'm gonna test the 'use 16' now.

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 4:02 pm
by RGOS
Hello,

I tested the 'use 16', but that's not an valid opcode for NASM, but if I use 'bits 16' there changes nothing, exept for if i switch of Bochs it says that the CPU is in real mode, without the 'bits 16' it's in protected mode, so it has influence, but it's not my problem.

Thanks.

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 5:14 pm
by Combuster
For all the people who don't want to download 100MB to find the interesting part:

Code: Select all

00047200961d[CPU0 ] BxError: Encountered an unknown instruction b1=0xff (signalling #UD)
00047200961d[CPU0 ] modrm was 0xff, nnn was 7, rm was 7
00047200961i[CPU0 ] 0x00000000000009fa>> (invalid)  : FFFF
00047200961d[CPU0 ] exception(0x06): error_code=0000
00047200961d[CPU0 ] interrupt(): vector = 06, TYPE = 3, EXT = 1
00047200961e[CPU0 ] write_virtual_word_32(): segment limit violation
00047200961d[CPU0 ] exception(0x0c): error_code=0000
00047200961d[CPU0 ] interrupt(): vector = 0c, TYPE = 3, EXT = 1
00047200961e[CPU0 ] write_virtual_word_32(): segment limit violation
00047200961d[CPU0 ] exception(0x0c): error_code=0000
00047200961d[CPU0 ] exception(0x08): error_code=0000
00047200961d[CPU0 ] interrupt(): vector = 08, TYPE = 3, EXT = 1
00047200961e[CPU0 ] write_virtual_word_32(): segment limit violation
00047200961d[CPU0 ] exception(0x0c): error_code=0000
00047200961i[CPU0 ] CPU is in real mode (active)
00047200961i[CPU0 ] CS.d_b = 16 bit
00047200961i[CPU0 ] SS.d_b = 16 bit
00047200961i[CPU0 ] EFER   = 0x00000000
00047200961i[CPU0 ] | RAX=00000000d08e0000  RBX=000000000000ff07
00047200961i[CPU0 ] | RCX=0000000000000007  RDX=0000000000000f00
00047200961i[CPU0 ] | RSP=00000000e8fb0005  RBP=0000000000000000
00047200961i[CPU0 ] | RSI=00000000000e01e2  RDI=0000000000000005
00047200961i[CPU0 ] |  R8=0000000000000000   R9=0000000000000000
00047200961i[CPU0 ] | R10=0000000000000000  R11=0000000000000000
00047200961i[CPU0 ] | R12=0000000000000000  R13=0000000000000000
00047200961i[CPU0 ] | R14=0000000000000000  R15=0000000000000000
00047200961i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf sf zf af PF cf
00047200961i[CPU0 ] | SEG selector     base    limit G D
00047200961i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00047200961i[CPU0 ] |  CS:0000( 0004| 0|  0) 00000000 0000ffff 0 0
00047200961i[CPU0 ] |  DS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00047200961i[CPU0 ] |  SS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00047200961i[CPU0 ] |  ES:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00047200961i[CPU0 ] |  FS:07c0( 0005| 0|  0) 00007c00 0000ffff 0 0
00047200961i[CPU0 ] |  GS:07c0( 0005| 0|  0) 00007c00 0000ffff 0 0
00047200961i[CPU0 ] |  MSR_FS_BASE:0000000000007c00
00047200961i[CPU0 ] |  MSR_GS_BASE:0000000000007c00
00047200961i[CPU0 ] | RIP=00000000000009fa (00000000000009fa)
00047200961i[CPU0 ] | CR0=0x60000010 CR2=0x0000000000000000
00047200961i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00047200961i[CPU0 ] 0x00000000000009fa>> (invalid)  : FFFF
00047200961d[CTRL ] searching for component 'cpu' in list 'bochs'
00047200961d[CTRL ] searching for component 'reset_on_triple_fault' in list 'cpu'
00047200961e[CPU0 ] exception(): 3rd (12) exception with no resolution, shutdown status is 00h, resetting
And especially this:
00047200961i[CPU0 ] | RIP=00000000000009fa
is the real problem: the crash occurs well outside where your code is supposed to be. Most interesting, there's nothing after your code, you just let it roam free into whatever is there in RAM.

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 5:20 pm
by Gigasoft
Mov cs, ax is an invalid instruction, and I'm surprised that you got that to compile. CS is already set to 0 now with your jmp 0:main.
Bits 16 is necessary before prtok since the code is running in 16 bit mode. The reason that Bochs says it's in protected mode is that the system has triple faulted and it's back in the BIOS. It triple faults because SP is set to 5 and you execute an invalid opcode, which could be the 0xFFFF part of the mov sp, 0xFFFF instruction.

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 5:29 pm
by geppyfx

Code: Select all

bits    16
 ...
%include "FILES\NewTry\Paging.inc" ;contains bits 32, overrides bits 16 in 'kernel.asm'   (verified)

;from now on, nasm compiled your code as it was 32bit code (bits 32)
prtok:
pusha
mov             si, ok
call    Puts16
popa
ret

;this is 32bit code too
main:
cli
xor             ax, ax

Re: MOV AX, 0x0000 doesn't work.

Posted: Sat Feb 13, 2010 6:33 pm
by DednDave
try mov ax,0 and mov sp,0FFFEh (sp should always be even for 16-bit code)

Re: MOV AX, 0x0000 doesn't work.

Posted: Sun Feb 14, 2010 9:43 am
by RGOS
Hello,

I've tried the mov sp, 0FFFEh, and it does work now, but now I run into trouble somewhile later, when I try to call LoadFile, I fount that in this piece of code the error was:

Code: Select all

LoadFile:

	xor		ecx, ecx
	push	ecx

.FIND_FILE:

	push	bx
	push	bp
	call	FindFile
	cmp		ax, -1
	jne		.LOAD_IMAGE_PRE
	pop		bp
	pop		bx
	pop		ecx
	mov		ax, -1
	ret

.LOAD_IMAGE_PRE:

	sub		edi, ROOT_OFFSET
	sub		eax, ROOT_OFFSET
	push	word ROOT_SEG
	pop		es
	mov		dx, WORD [es:di + 0x001A]
	mov		WORD [cluster], dx
	pop		bx
	pop		es
	push    bx
	push	es
	call	LoadFAT
	
.LOAD_IMAGE:

	mov		ax, WORD [cluster]
	pop		es
	pop		bx
	call	ClusterLBA
	xor		cx, cx
	mov     cl, BYTE [bpbSectorsPerCluster]
	call	ReadSectors
	pop		ecx
	inc		ecx
	push	ecx
	push	bx
	push	es
	mov		ax, FAT_SEG
	mov		es, ax
	xor		bx, bx
	mov     ax, WORD [cluster]
	mov     cx, ax
	mov     dx, ax
	shr     dx, 0x0001
	add     cx, dx
	mov		bx, 0
	add		bx, cx
	mov		dx, WORD [es:bx]
	test	ax, 0x0001
	jnz		.ODD_CLUSTER

.EVEN_CLUSTER:

	and		dx, 0000111111111111b
	jmp		.DONE

.ODD_CLUSTER:

	shr		dx, 0x0004

.DONE:

	mov		WORD [cluster], dx
	cmp		dx, 0x0ff0

	jb		.LOAD_IMAGE

.SUCCESS:
	pop		es
	pop		bx
	pop		ecx
	xor		ax, ax
	ret
Then in the piece .LOAD_IMAGE, the first time it does work (it's a loop) (checked it by halting the CPU), but the second time I get the error from bochs:

Code: Select all

prefetch: EIP [00010000] > CS.limit [0000ffff]
I don't know what the problem is, the only thing I get is that the instruction pointer higher is than CS can hold, but how is it possible? :?

The full code is in the attachment, I uploaded them in .txt files, because .inc isn't allowed.

Thanks.