Ver Mensaje Individual
  #13 (permalink)  
Antiguo 16/08/2011, 15:06
poi
 
Fecha de Ingreso: octubre-2008
Mensajes: 73
Antigüedad: 15 años, 6 meses
Puntos: 2
Respuesta: C/C++ Hola mundo sin Sistema Operativo

Gracias. Ya mas o menos voy entendiendolo.
A ver, ahora tengo este bootloader:
Código C++:
Ver original
  1. [BITS 16]       ; We need 16-bit intructions for Real mode
  2.  
  3.  
  4.  
  5. [ORG 0x7C00]    ; The BIOS loads the boot sector into memory location 0x7C00
  6.  
  7.  
  8.  
  9. reset_drive:
  10.  
  11.         mov ah, 0               ; RESET-command
  12.  
  13.         int 13h                 ; Call interrupt 13h
  14.  
  15.         or ah, ah               ; Check for error code
  16.  
  17.         jnz reset_drive         ; Try again if ah != 0
  18.  
  19.  
  20.  
  21.         mov ax, 0
  22.  
  23.         mov es, ax
  24.  
  25.         mov bx, 0x1000          ; Destination address = 0000:1000
  26.  
  27.  
  28.  
  29.         mov ah, 02h             ; READ SECTOR-command
  30.  
  31.         mov al, 02h             ; Number of sectors to read = 1
  32.  
  33.         mov ch, 0               ; Cylinder = 0
  34.  
  35.         mov cl, 02h             ; Sector = 2
  36.  
  37.         mov dh, 0               ; Head = 0
  38.  
  39.         int 13h                 ; Call interrupt 13h
  40.  
  41.         or ah, ah               ; Check for error code
  42.  
  43.         jnz reset_drive         ; Try again if ah != 0
  44.  
  45.  
  46.  
  47.         cli                     ; Disable interrupts, we want to be alone
  48.  
  49.  
  50.  
  51.         xor ax, ax
  52.  
  53.         mov ds, ax              ; Set DS-register to 0 - used by lgdt
  54.  
  55.  
  56.  
  57.         lgdt [gdt_desc]         ; Load the GDT descriptor
  58.  
  59.  
  60.  
  61.         mov eax, cr0            ; Copy the contents of CR0 into EAX
  62.  
  63.         or eax, 1               ; Set bit 0
  64.  
  65.         mov cr0, eax            ; Copy the contents of EAX into CR0
  66.  
  67.  
  68.  
  69.         jmp 08h:clear_pipe      ; Jump to code segment, offset clear_pipe
  70.  
  71.  
  72.  
  73.  
  74.  
  75. [BITS 32]                       ; We now need 32-bit instructions
  76.  
  77. clear_pipe:
  78.  
  79.         mov ax, 10h             ; Save data segment identifyer
  80.  
  81.         mov ds, ax              ; Move a valid data segment into the data segment register
  82.  
  83.         mov ss, ax              ; Move a valid data segment into the stack segment register
  84.  
  85.         mov esp, 090000h        ; Move the stack pointer to 090000h
  86.  
  87.  
  88.  
  89.         jmp 08h:01000h          ; Jump to section 08h (code), offset 01000h
  90.  
  91.  
  92.  
  93.  
  94.  
  95. gdt:                    ; Address for the GDT
  96.  
  97.  
  98.  
  99. gdt_null:               ; Null Segment
  100.  
  101.         dd 0
  102.  
  103.         dd 0
  104.  
  105.  
  106.  
  107. gdt_code:               ; Code segment, read/execute, nonconforming
  108.  
  109.         dw 0FFFFh
  110.  
  111.         dw 0
  112.  
  113.         db 0
  114.  
  115.         db 10011010b
  116.  
  117.         db 11001111b
  118.  
  119.         db 0
  120.  
  121.  
  122.  
  123. gdt_data:               ; Data segment, read/write, expand down
  124.  
  125.         dw 0FFFFh
  126.  
  127.         dw 0
  128.  
  129.         db 0
  130.  
  131.         db 10010010b
  132.  
  133.         db 11001111b
  134.  
  135.         db 0
  136.  
  137.  
  138.  
  139. gdt_end:                ; Used to calculate the size of the GDT
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. gdt_desc:                       ; The GDT descriptor
  148.  
  149.         dw gdt_end - gdt - 1    ; Limit (size)
  150.  
  151.         dd gdt                  ; Address of the GDT
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. times 510-($-$$) db 0           ; Fill up the file with zeros
  162.  
  163.  
  164.  
  165.         dw 0AA55h                ; Boot sector identifyer

y este kernel en C:

Código C:
Ver original
  1. void main()
  2.  
  3. {
  4.  
  5.  
  6.  
  7.  
  8.  
  9.   for(;;);
  10.  
  11. }

lo compilo con este script:

nasm -f bin bootsect.asm -o bootsect.bin
gcc -ffreestanding -c main.c -o main.o
ld -e main -Ttext 0x1000 -o kernel.o main.o
ld -i -e main -Ttext 0x1000 -o kernel.o main.o
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin

Y creo la imagen con:

rm boot.img
dd if=/dev/zero of=boot.img bs=512 count=15
dd if=bootloader.bin of=boot.img bs=512 count=1
dd if=kernel.bin of=boot.img bs=512 count=14 seek=1

Pero cuando prueba la imagen en la maquina virtual en un diskete, me sale el error que no encuentra un sector booteable.
¿Estoy creando mal la imagen?

Gracias.