flat assembler
Message board for the users of flat assembler.

Index > OS Construction > .

Author
Thread Post new topic Reply to topic
mikegonta



Joined: 20 Nov 2005
Posts: 99
mikegonta 19 Jun 2007, 00:29
[ Post removed by author. ]


Last edited by mikegonta on 27 Jan 2009, 22:04; edited 4 times in total
Post 19 Jun 2007, 00:29
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 19 Jun 2007, 01:15
hmm, should i link this from somewhere ???
Post 19 Jun 2007, 01:15
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Mac2004



Joined: 15 Dec 2003
Posts: 314
Mac2004 05 Jul 2007, 15:47
mikegonta: You have posted a very good example! Very Happy Seems to be very clear and properly documented.

regards,
Mac2004
Post 05 Jul 2007, 15:47
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 05 Jul 2007, 17:04
There has been a demo of this on the forum for at lest 3 years and as it stands, i would eat my hat, if that code worked without moding, if you had nomal things set up like int etc Laughing
Post 05 Jul 2007, 17:04
View user's profile Send private message Reply with quote
mikegonta



Joined: 20 Nov 2005
Posts: 99
mikegonta 15 Jul 2007, 23:53
[ Post removed by author. ]


Last edited by mikegonta on 27 Jan 2009, 22:05; edited 2 times in total
Post 15 Jul 2007, 23:53
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 16 Jul 2007, 01:48
For example do you make addressing identical in real mode and protected mode, by setting the base of the code and data descriptors to DS * 16 ?.
If so you should point that out.

Also in the example in the link, you have this:
Code:
TIMES 510-($-start) db 0 
   dw 0AA55h 
    
But i see no "start" label.
Post 16 Jul 2007, 01:48
View user's profile Send private message Reply with quote
mikegonta



Joined: 20 Nov 2005
Posts: 99
mikegonta 16 Jul 2007, 02:21
[ Post removed by author. ]


Last edited by mikegonta on 27 Jan 2009, 22:05; edited 2 times in total
Post 16 Jul 2007, 02:21
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 16 Jul 2007, 03:17
But i was talking above return to realmode functions, in your first post.
Post 16 Jul 2007, 03:17
View user's profile Send private message Reply with quote
jatos



Joined: 04 Nov 2006
Posts: 20
jatos 29 Nov 2007, 16:16
Ok, I have just been studying this code, and theres two things I am sure about

I don't if this is just me making mistakes, but I tryed getting the code to work, in my own test kernel and it didn't

As far I as can see:

gdt: starts at the GDT pointer and not the GDT itself. Effectively the GDT pointer points to itself???

The null descriptor would appear to be 1 byte, when I thought it should be 8 bytes???

If anyone wants to look at my code, then here it is.

Code:
org 0x1600

call enableA20

app_start:
    lgdt [gdt]
  jmp 8:continue

continue:
     mov ax, 10h      
   mov ds, ax
  mov es, ax
  mov fs, ax
  mov gs, ax
  mov ss, ax
  
func_set_registers:
        MOV ebx, 0xB800
        MOV ebp, [msg_1]
          MOV eax, 0x0000
        MOV ebx, 0x0000
        MOV ecx, 0x0000

func_print_string:
        MOV al, [ds:ebp]
        CMP al, 0
        JE app_end
        MOV [ds:ebx], al    

        INC ebp
        ADD ebx, 2
        JMP func_print_string

jmp app_end


align 8
gdt:
  dw 39                      ; Global Descriptor Table size - 1
  dd gdt                     ; pointer to table
  dw 0                       ; null selector is a good location for this
  dw 0FFFFh, 0, 9A00h, 0CFh  ; maximum pm code selector = #8
  dw 0FFFFh, 0, 9200h, 0CFh  ; maximum pm data selector = #16
  dw 0FFFFh, 0, 9A00h, 0     ; 64Kb rm code selector = #24
  dw 0FFFFh, 0, 9200h, 0     ; 64Kb rm data selector = #32 
  
app_data:

        msg_1 db "Hello World!", 0 

app_end:
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        jmp app_end

;;
;; enableA20.s (adapted from Visopsys OS-loader)
;;
;; Copyright (c) 2000, J. Andrew McLaughlin
;; You're free to use this code in any manner you like, as long as this
;; notice is included (and you give credit where it is due), and as long
;; as you understand and accept that it comes with NO WARRANTY OF ANY KIND.
;; Contact me at jamesamc@yahoo.com about any bugs or problems.
;;

enableA20:
    ;; This subroutine will enable the A20 address line in the keyboard
 ;; controller.  Takes no arguments.  Returns 0 in EAX on success, 
  ;; -1 on failure.  Written for use in 16-bit code, see lines marked
 ;; with 32-BIT for use in 32-bit code.

  pusha

   ;; Make sure interrupts are disabled
        cli

     ;; Keep a counter so that we can make up to 5 attempts to turn
      ;; on A20 if necessary
      mov CX, 5

       .startAttempt1:             
    ;; Wait for the controller to be ready for a command
        .commandWait1:
  xor AX, AX
  in AL, 64h
  bt AX, 1
    jc .commandWait1

        ;; Tell the controller we want to read the current status.
  ;; Send the command D0h: read output port.
      mov AL, 0D0h
        out 64h, AL

     ;; Wait for the controller to be ready with a byte of data
  .dataWait1:
     xor AX, AX
  in AL, 64h
  bt AX, 0
    jnc .dataWait1

  ;; Read the current port status from port 60h
       xor AX, AX
  in AL, 60h

      ;; Save the current value of (E)AX
  push AX                 ; 16-BIT
    ;; push EAX             ; 32-BIT

        ;; Wait for the controller to be ready for a command
        .commandWait2:
  in AL, 64h
  bt AX, 1
    jc .commandWait2

        ;; Tell the controller we want to write the status byte again
       mov AL, 0D1h
        out 64h, AL     

        ;; Wait for the controller to be ready for the data
 .commandWait3:
  xor AX, AX
  in AL, 64h
  bt AX, 1
    jc .commandWait3

        ;; Write the new value to port 60h.  Remember we saved the old
      ;; value on the stack
       pop AX                  ; 16-BIT
    ;; pop EAX              ; 32-BIT

        ;; Turn on the A20 enable bit
       or AL, 00000010b
    out 60h, AL

     ;; Finally, we will attempt to read back the A20 status
     ;; to ensure it was enabled.

    ;; Wait for the controller to be ready for a command
        .commandWait4:
  xor AX, AX
  in AL, 64h
  bt AX, 1
    jc .commandWait4

        ;; Send the command D0h: read output port.
      mov AL, 0D0h
        out 64h, AL     

        ;; Wait for the controller to be ready with a byte of data
  .dataWait2:
     xor AX, AX
  in AL, 64h
  bt AX, 0
    jnc .dataWait2

  ;; Read the current port status from port 60h
       xor AX, AX
  in AL, 60h

      ;; Is A20 enabled?
  bt AX, 1

        ;; Check the result.  If carry is on, A20 is on.
    jc .success

     ;; Should we retry the operation?  If the counter value in ECX
      ;; has not reached zero, we will retry
      loop .startAttempt1


 ;; Well, our initial attempt to set A20 has failed.  Now we will
    ;; try a backup method (which is supposedly not supported on many
       ;; chipsets, but which seems to be the only method that works on
    ;; other chipsets).


     ;; Keep a counter so that we can make up to 5 attempts to turn
      ;; on A20 if necessary
      mov CX, 5

       .startAttempt2:
 ;; Wait for the keyboard to be ready for another command
    .commandWait6:
  xor AX, AX
  in AL, 64h
  bt AX, 1
    jc .commandWait6

        ;; Tell the controller we want to turn on A20
       mov AL, 0DFh
        out 64h, AL

     ;; Again, we will attempt to read back the A20 status
       ;; to ensure it was enabled.

    ;; Wait for the controller to be ready for a command
        .commandWait7:
  xor AX, AX
  in AL, 64h
  bt AX, 1
    jc .commandWait7

        ;; Send the command D0h: read output port.
      mov AL, 0D0h
        out 64h, AL     

        ;; Wait for the controller to be ready with a byte of data
  .dataWait3:
     xor AX, AX
  in AL, 64h
  bt AX, 0
    jnc .dataWait3

  ;; Read the current port status from port 60h
       xor AX, AX
  in AL, 60h

      ;; Is A20 enabled?
  bt AX, 1

        ;; Check the result.  If carry is on, A20 is on, but we might warn
  ;; that we had to use this alternate method
 jc .warn

        ;; Should we retry the operation?  If the counter value in ECX
      ;; has not reached zero, we will retry
      loop .startAttempt2


 ;; OK, we weren't able to set the A20 address line.  Do you want
   ;; to put an error message here?
    jmp .fail


   .warn:
  ;; Here you may or may not want to print a warning message about
    ;; the fact that we had to use the nonstandard alternate enabling
   ;; method

       .success:
       sti
 popa
        xor EAX, EAX
        ret

     .fail:
  sti
 popa
        mov EAX, -1
 ret
    

_________________
Jamie
Post 29 Nov 2007, 16:16
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 29 Nov 2007, 18:01
rb 510-($-$$)
it's better because fasm puts 90h for each reserved byte ; rb
nop = 90h
and it's shorter to write than
times 510-($-$$) db 0
Post 29 Nov 2007, 18:01
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 29 Nov 2007, 19:15
Quote:

rb 510-($-$$)
it's better because fasm puts 90h for each reserved byte ; rb

Actually pads with zeros and only if assembly-time defined data values follows. It is the align directive which pads with NOPs and again, only when more defined data/instructions follows.

Code:
format binary
rb 128
align 256
db 0    
Produces a 257 bytes binary (128 zeros, 128 NOPs and a zero).
Post 29 Nov 2007, 19:15
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 29 Nov 2007, 19:49
Code:
format binary
rb 128
align 256
db 0    
Produces a 257 bytes binary (128 zeros, 128 NOPs and a zero).
sure. but you need to add an extra byte
iuse this some times.
but the best in MY opinion is
rb size-($-$$)

if no data follows the rb, effectivelly, it don't add bytes.
but it's not important for asm coder.
you can easily add bytes at the end of the file with an hexeditor.

sorry, the rb pad with 0, the align X pad with 90. i witten an error, i'm confuse.


errors are just after lgdt & in gdt definition.
try to know.
Code:

        org 1600h
        call enableA20
app_start:
;THE error, doesn't cli before GDT loading and PM switch
        lgdt [gdt] 
;first error, missing pm switch
        mov eax,crO
        or eax,pebit
        mov cr0,eax
;don't forget to switch to pm to be able to load descriptor
;first error continued...
        jmp 8:continue  ;to make this jmp, you need to be in PM
;if not, you will jmp to linear 80h+1600h+continue
continue:
;first error continued
;if not, an attempt to load 10h will simply set segment register to real mode 100h linear

        mov ax, 10h       
        mov ds, ax 
        mov es, ax 
        mov fs, ax 
        mov gs, ax 
        mov ss, ax 
;second error, esp define missing
        mov esp,100000h ;stack is at linear 16Mb
func_set_registers:
;third error, screen is not at B800h, but B8000h
;B800h is for the real mode segment
        MOV ebx, 0xB8000 
        MOV eax, 0x0000
;not an error, why using ebp? it's the last register to use, when you don't have the choise
;esi is better because it means 'extented source index'
        mov esi,[msg1]
;error, you try to print text at linear 0
;;;;        MOV ebx, 0x0000 
;why define ecx?
func_print_string:
        MOV al, [esi] 
        CMP al, 0 
        JE app_end 
        MOV [ebx], al     
        INC ebp
        ADD ebx, 2 
        JMP func_print_string
;why this second jmp????? 
;        jmp app_end
align 8
;not an error, but better to align for dword
align 2                   ;leave aligned the dword of gdtr
gdt:
  dw 39                      ; Global Descriptor Table size - 1 
  dd gdt                     ; pointer to table 
;fourth error , null desc is 8bytes long not 2
  dq 0                    ;null desc is now 8 bytes long
; your mistake was the dw instead of dq
  dw 0FFFFh,0,9A00h,0CFh  ; maximum pm code selector = #8
  dw 0FFFFh,0,9200h,0CFh  ; maximum pm data selector = #16
  dw 0FFFFh,0,9A00h,0     ; 64Kb rm code selector = #24
  dw 0FFFFh,0,9200h,0     ; 64Kb rm data selector = #32
app_data:
        msg_1 db "Hello World!", 0
app_end:
        jmp app_end
enableA20:
        pusha
        cli
;not an error, but with @@: it's more readable
@@:
        xor AX, AX 
        in AL, 64h 
        bt AX, 1 
        jc @b
        mov AL, 0D0h
        out 64h, AL 
@@:
        xor AX, AX
        in AL, 64h 
        bt AX, 0 
        jnc @b
        xor AX, AX
        in AL, 60h 
        push eAX
@@:
        in AL, 64h 
        bt AX, 1 
        jc @b
        mov AL, 0D1h
        out 64h, AL      
@@:
        xor AX, AX 
        in AL, 64h 
        bt AX, 1 
        jc @b
        pop eAX
        or AL, 00000010b
        out 60h, AL 
@@:
        xor AX, AX 
        in AL, 64h 
        bt AX, 1 
        jc @b
        mov AL, 0D0h
        out 64h, AL      
@@:
        xor AX, AX 
        in AL, 64h 
        bt AX, 0 
        jnc @b
        xor AX, AX
        in AL, 60h 
        bt AX, 1
        jc .success
.fail:
        sti 
        popa 
        mov EAX, -1 
        ret 
.success:
        sti 
        popa 
        xor EAX, EAX 
        ret 
                                 


why testing if A20 is enabled?
or it works the first time, or the PC is dead.
Post 29 Nov 2007, 19:49
View user's profile Send private message Visit poster's website Reply with quote
jatos



Joined: 04 Nov 2006
Posts: 20
jatos 29 Nov 2007, 21:23
Enough errors there on my behalf. Thanks for pointing all those out.

One thing I will say, if you look in my original post, that dw instead of dq was mikes error, and I actually queried in my original post. I still got to make the GDT pointer actually point to GDT not itself. Looks like dex4u won't be eating his hat Wink

_________________
Jamie


Last edited by jatos on 29 Nov 2007, 22:42; edited 1 time in total
Post 29 Nov 2007, 21:23
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 29 Nov 2007, 21:33
Quote:

sure. but you need to add an extra byte
iuse this some times.
but the best in MY opinion is
rb size-($-$$)

I added the extra byte just to not get a zero byte in size binary.

And since in this particular context "rb size-($-$$)" is followed by the boot signature then it is prefered but if not then the prefered is "db size+$$-$ dup 0" (or dup $90 if you want).
Post 29 Nov 2007, 21:33
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 29 Nov 2007, 21:42
of course.
but i said that for an asm coder, modifying the binary size is not a problem.
the only possibility of problem is when loading this file in memory directlly followed by another file.
in this case we certainly obtain a GP error.
ok, let's go for
times size-($-$$) db 0
but for case where defiened data are after i'll use the
rb size-($-$$) statement
Wink
Post 29 Nov 2007, 21:42
View user's profile Send private message Visit poster's website Reply with quote
jatos



Joined: 04 Nov 2006
Posts: 20
jatos 29 Nov 2007, 23:00
Ok, I think I have sorted all those errors, but the code keeps rebooting my PC

Code:
org 1600h 

app_start: 
   cli
        mov eax, cr0
        or eax, 1
        mov cr0, eax
        jmp 8:continue 

continue: 
        mov ax, 10h        
        mov ds, ax  
        mov es, ax  
        mov fs, ax  
        mov gs, ax  
        mov ss, ax  
        mov esp,100000h ;stack is at linear 16Mb 

func_set_registers: 
        MOV ebx, 0xB8000  
        MOV eax, 0x0000 
        LEA esi,[msg_1] 

func_print_string: 
        MOV al, [esi]  
        CMP al, 0  
        JE app_end  
        MOV [ebx], al      
        INC ebp 
        ADD ebx, 2  
        JMP func_print_string 

align 8 

align 2                 
gdt_ptr: 
      dw 23
       dd gdt

gdt:
      dq 0
        dw 0FFFFh,0,9A00h,0CFh  ; maximum pm code selector = #8 
    dw 0FFFFh,0,9200h,0CFh  ; maximum pm data selector = #16 

app_data: 
        msg_1 db "Hello World!", 0 

app_end: 
        jmp app_end 
    
Post 29 Nov 2007, 23:00
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 30 Nov 2007, 00:13
where is lgdt?
lgdt [gdt_ptr]
in the bootloader
jmp to 0:1600h
instead of
lea esi,[msg_1]
write
mov esi,msg_1
and where is your enable A20?
i'm not sure it's need ed for PM but it's better to make it.
Post 30 Nov 2007, 00:13
View user's profile Send private message Visit poster's website Reply with quote
jatos



Joined: 04 Nov 2006
Posts: 20
jatos 30 Nov 2007, 14:09
lgdt: How did I forget that and not notice, despite checking my code several times??? You have permission to call me an idiot!

A20, done a reasonable amount of research on that, its completely usually if your going into protected mode.

_________________
Jamie
Post 30 Nov 2007, 14:09
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 30 Nov 2007, 15:34
Here's a demo that i wrote that goes to and from real, to demo vesa mode switching, it needs vesa2 .
It may help ?.
http://www.dex4u.com/demos/DemoVesa.zip
Post 30 Nov 2007, 15:34
View user's profile Send private message Reply with quote
jatos



Joined: 04 Nov 2006
Posts: 20
jatos 30 Nov 2007, 18:11
Thanks for Dex4u, I aways like to see examples.

I will have a look at that, hopefully it will allow to me solve at least some of my difficulties without asking yet another question on the forums!

_________________
Jamie
Post 30 Nov 2007, 18:11
View user's profile Send private message 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.