flat assembler
Message board for the users of flat assembler.

Index > Main > hello everone.this is fasm complie boot.asm error

Author
Thread Post new topic Reply to topic
kerr



Joined: 24 Feb 2016
Posts: 156
kerr 19 Aug 2018, 17:30
Code:
org 0
use16
                jmp             boot
                db              0x90                            ; jump instruncatin (to skip over header on boot)
                db              "HDOS"                          ; OEM Name
                dw              512                                     ; Bytes per sector. The BIOS Parmeter Block starts here.
                db              1                                       ; Sectors per cluster
                db              1                                       ; Reserved sector count(including boot sector)
                db              2                                       ; Number of file allocation tables
                dw              0xe0                            ; Maximum number root directory enries
                dw              0xb40                           ; Total sector:80 tracks*18 sectors*2 sides=2880
                db              0xf0                            ; Media descriptor
                db              9                                       ; Sectors per File Allocation Table
                db              18                                      ; Sectors per tracks
                db              2                                       ; Numver of heads
                db              0                                       ; Hidden sectors
                dw              0xb40                           ; Total sectors again
                db              0                                       ; Physical drive number
                db              0                                       ; Reserved("current head")
                db              0x29                            ; Signature
                dd              0xffffffff                      ; ID(serial number)
                db              "HDOS OS"                       ; Volume Label
                db              "FAT16  "                       ; FAT file system type,FAT16
;               segment ".text"
                times   18 db 0
        
                boot:
                                        mov                     ax,0x7c0                                ; Initialization
                                        push            ax
                                        push            ax
                                        pop                     ds
                                        pop                     es
                                        xor                     ax,ax
                                        push            ax
                                        pop                     ss
                                        mov                     sp,0x7c0                                ; Set stack initialization
                                        mov                     si,msg
                                        call            print
                read:                   
                                        mov                     ax,0x9000
                                        mov                     es,ax                                   ; 0x9000(es)*16 = 0x90000
                                        mov                     ch,0                                    ; track/cylinderr number
                                        mov                     dh,0                                    ; head number
                                        mov                     cl,2                                    ; sector number
                                        mov                     ah,0x02                                 ; status of reading disk sector
                                        mov                     al,1                                    ; number of sectors read
                                        mov                     bx,0                                    ; (es)*16+bx = 9000:0
                                        mov                     dl,0x00                                 ; A drive
                                        int                     0x13                                    ; DISK - READ SECTORS INTO MEMORY
                                                                                                                ; AL = number of sectors to read, CH = track, CL = sector
                                                                                                                ; DH = head, DL = drive, ES:BX -> buffer to fill
                                                                                                                ; Return: CF set on error, AH = status, AL = number of sectors read
                                        mov                     si,msgstr
                                        call            print
                                        jnc                     short loads                             ; If CF = 0 goto 0x9000:0 address.
                                        jc                      short error
                                        jc                      short error                             ; IF read error  goto show error string

                error:
                                        xor                     ax,ax                                   ; clean registers data  
                                        mov                     es,ax                                   ; Initialization segment of es registers
                                        mov                     si,errstr
                                        call            print
                        

                print:
                                        mov                     ah,0xe                                  ; Show string
                                        lodsb                                                           ; Load Accumulator from string
                                        test            al,al                                   ; test al and al value
                                        jz                      short stop                              ; Stop of cpu
                                        int                     0x10                                    ; - VIDEO - WRITE CHARACTER AND ADVANCE CURSOR (TTY WRITE)
                                                                                                                ; AL = character, BH = display page (alpha modes)
                                                                                                                ; BL = foreground color (graphics modes)
                                        jmp                     short print
                stop:                   
                                        hlt
                                        ret
                loads:          
                                        jmp                     0x9000:0
                                        
                msg:            db              0xd,0xa,"HDOS OS Initialize...",0xd,0xa
                                        db                      "Reading disk...",0xd,0xa,0
                msgstr:         db                      "The sector was reading...",0xd,0xa
                                        db                      "Please wait!",0xd,0xa,0
                errstr:         db                      0xd,0xa,"Warning: Sorry can't read disk",0xd,0xa
                                        db                      "Please Try Again",0xd,0xa,0
                                        times           510-($-$$)  db  0
                                        dw              0xaa55
                                        
use16
                start:
                                        push            0x9000
                                        pop                     ds
                                        cli             
                                        in                      al,0x92                                 ; Open the South Bridge
                                        or                      al,2                                    ; Read fast A20 port data
                                        out                     0x92,al                                 ; When A20's bit 1 is 1, open A20
                                        lgdt            [gdtr-start]            ; Calculate the offset relative to the second sector, plus 0x9000
                                        
                                        mov                     eax,cr0                                 ; Open protection mode 
                                        or                      eax,1                                   ; CR0 register 0 position 1
                                        mov                     cr0,eax                                 ; Setting CR0 register
                                        jmp                     dword 0x8:in_32bit-start+0x90000        ; 8 (1000B), that is, index=1, TI=0, which means GDT's sequence number is 1.

use32
align 16
                in_32bit:
                                        mov                     ax,0x10
                                        mov                     ds,ax
                                        mov                     ax,0x18
                                        mov                     es,ax
                                        mov                     esi,tip-start+0x90000
                                        xor                     edi,edi
                                        mov                     ecx,25
                                        cld
                                        rep                     movsb
                                        
                                        mov                     ax,0x18
                                        mov                     ds,ax
                                        mov                     ax,0x20
                                        mov                     es,ax
                                        xor                     esi,esi
                                        xor                     edi,edi
                                        mov                     ecx,25
                show:
                                        mov                     al,[esi]
                                        mov                     byte [es:edi],al
                                        inc                     esi
                                        add                     edi,2
                                        loop            show
                                        
                                        align           8
                gdt:            
                                        dq                      0
                                        dq                      0xc0cf9a000000ffff              ; kernel 4GB code at 0xc0000000
                                        dq                      0xc0cf92000000ffff              ; kernel 4GB data at 0xc0000000
                                        dq                      0x00cffa000000ffff              ; user   4GB code at 0x00000000
                                        dq                      0x00cff2000000ffff              ; user   4GB data at 0x00000000
                gdt_len         equ                     $-gdt                                   ; get gdt length                
                gdtr:
                                        dw                      gdt_len - 1
                                        dd                      gdt - start + 0x90000
                tip:
                                        db                      "Welcome to protect mode!!",0xa,0xd,0
                tip_len         equ             $-tip
                                        
                                        times           1024-($-$$) db 0
    


hello friends.

Next, I want to enter the protection mode and prompt the instruction error when compiling.
boot.asm [103]:
jmp dword 0x8:in_32bit-start+0x90000 ; 8 (1000B), that is, index=1, TI=0, which means GDT's sequence number is 1.
processed: jmp dword 0x8:in_32bit-start+0x90000
error: value out of range.

But I modified the 0x9000 virtual machine VBox to execute in CR0. This is wrong. What is the error?

_________________
I hope we will be good friends.
Post 19 Aug 2018, 17:30
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 19 Aug 2018, 19:05
You’re writing 16-bit code in that place. In 16-bit real mode address can only be 32 bits (16 bits for segment index, 16 bits for offset). Your jump uses the 0x90000 constant which is outside 16-bit integer range and thus cannot be encoded in this mode.

See 2.1.6 in FASM.pdf for description of pword that might help here. Never tried it though since I find using 32-bit encodings in 16-bit mode (i.e. mixing 16 and 32 bits) a bit weird.

P.S. Note also that you have some redundant instructions in your code like, say, jnc followed by two jc’s.
Post 19 Aug 2018, 19:05
View user's profile Send private message Visit poster's website Reply with quote
kerr



Joined: 24 Feb 2016
Posts: 156
kerr 20 Aug 2018, 13:31
DimonSoft wrote:
You’re writing 16-bit code in that place. In 16-bit real mode address can only be 32 bits (16 bits for segment index, 16 bits for offset). Your jump uses the 0x90000 constant which is outside 16-bit integer range and thus cannot be encoded in this mode.

See 2.1.6 in FASM.pdf for description of pword that might help here. Never tried it though since I find using 32-bit encodings in 16-bit mode (i.e. mixing 16 and 32 bits) a bit weird.

P.S. Note also that you have some redundant instructions in your code like, say, jnc followed by two jc’s.



If you compile with NASM, it's fine, but CR0 can't run virtual machine here.

_________________
I hope we will be good friends.
Post 20 Aug 2018, 13:31
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 20 Aug 2018, 15:25
kerr wrote:
If you compile with NASM, it's fine, but CR0 can't run virtual machine here.

I don’t see how a control register can “run virtual machine”.

My guess is that NASM just truncates the constant silently. Which is a bad behaviour. And this may explain why something goes wrong in runtime.
Post 20 Aug 2018, 15:25
View user's profile Send private message Visit poster's website Reply with quote
kerr



Joined: 24 Feb 2016
Posts: 156
kerr 22 Aug 2018, 14:27
DimonSoft wrote:
kerr wrote:
If you compile with NASM, it's fine, but CR0 can't run virtual machine here.

I don’t see how a control register can “run virtual machine”.

My guess is that NASM just truncates the constant silently. Which is a bad behaviour. And this may explain why something goes wrong in runtime.


I mean that the virtual machine was wrong when it ran to CR0.

_________________
I hope we will be good friends.
Post 22 Aug 2018, 14:27
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 22 Aug 2018, 18:08
kerr wrote:
DimonSoft wrote:
kerr wrote:
If you compile with NASM, it's fine, but CR0 can't run virtual machine here.

I don’t see how a control register can “run virtual machine”.

My guess is that NASM just truncates the constant silently. Which is a bad behaviour. And this may explain why something goes wrong in runtime.


I mean that the virtual machine was wrong when it ran to CR0.

How did the “wrong” state manifest itself?
Post 22 Aug 2018, 18:08
View user's profile Send private message Visit poster's website Reply with quote
kerr



Joined: 24 Feb 2016
Posts: 156
kerr 23 Aug 2018, 02:51
DimonSoft wrote:
kerr wrote:
DimonSoft wrote:
kerr wrote:
If you compile with NASM, it's fine, but CR0 can't run virtual machine here.

I don’t see how a control register can “run virtual machine”.

My guess is that NASM just truncates the constant silently. Which is a bad behaviour. And this may explain why something goes wrong in runtime.


I mean that the virtual machine was wrong when it ran to CR0.

How did the “wrong” state manifest itself?


A critical error has occurred while running the virtual machine and machine
execution has been stopped.

_________________
I hope we will be good friends.
Post 23 Aug 2018, 02:51
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 23 Aug 2018, 07:08
So, why are you sure it was caused by access to CR0, not by the JMP?
Post 23 Aug 2018, 07:08
View user's profile Send private message Visit poster's website Reply with quote
kerr



Joined: 24 Feb 2016
Posts: 156
kerr 25 Aug 2018, 13:58
DimonSoft wrote:
So, why are you sure it was caused by access to CR0, not by the JMP?


I found it through debugging.

_________________
I hope we will be good friends.
Post 25 Aug 2018, 13:58
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 25 Aug 2018, 17:01
kerr wrote:
DimonSoft wrote:
So, why are you sure it was caused by access to CR0, not by the JMP?


I found it through debugging.

There seems to be nothing wrong with access to CR0. I think you should double-check what you really see. Does VBox stand for VirtualBox? I don’t remember it to have built-in debugger.
Post 25 Aug 2018, 17:01
View user's profile Send private message Visit poster's website Reply with quote
kerr



Joined: 24 Feb 2016
Posts: 156
kerr 06 Sep 2018, 16:41
DimonSoft wrote:
kerr wrote:
DimonSoft wrote:
So, why are you sure it was caused by access to CR0, not by the JMP?


I found it through debugging.

There seems to be nothing wrong with access to CR0. I think you should double-check what you really see. Does VBox stand for VirtualBox? I don’t remember it to have built-in debugger.



is windbag + vbox found it

_________________
I hope we will be good friends.
Post 06 Sep 2018, 16:41
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 06 Sep 2018, 18:29
kerr wrote:
DimonSoft wrote:
kerr wrote:
DimonSoft wrote:
So, why are you sure it was caused by access to CR0, not by the JMP?


I found it through debugging.

There seems to be nothing wrong with access to CR0. I think you should double-check what you really see. Does VBox stand for VirtualBox? I don’t remember it to have built-in debugger.



is windbag + vbox found it

Found some time to give your code a try. A few notes…
1. The hlt instruction inside print procedure seems strange.
2. I’ve already mentioned the duplication of jc instruction. It is used after print procedure for some reason. I wouldn’t rely on FLAGS being preserved by a BIOS call due to lots of buggy BIOSes out there plus your implementation might change later to some piece of code that would overwrite FLAGS.
3. Base address of your kernel code and data segments is set to 0xC0000000 but the code you load is somewhere near 0x90000. Changing the base to zero makes it work right (although you’ll see an exception soon after your protected mode code starts to execute).

No problems with writing to CR0 are seen in Bochs. Maybe your toolset lies to you.
Post 06 Sep 2018, 18:29
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.