No BIOS output from sector 1

59 views Asked by At

I'm in need for the knowledge and further clarification (either it's written in documentation that is hard for me to find just by typing on a Google search or with the GitHub repo) on going to desired sector (in my case it's sector 1), 'cause I still fail to make it fully functional due to lack of understanding the right order/code arrangement.

The code goes like this:

org 0x7c00
bits 16

mov ah, 0x02 ; reading
mov al, 0x1
mov ch, 0x0
mov cl, 0x1 ; sector 1
mov dh, 0x0
mov dl, 0x00 ; from floppy (.img)
int 13h

jc error ; error handling for the wrong/absent position
jmp 0x0200 ; trying to jump to the address

error:
  mov bx, [errorline]
  mov ah, 0x0e
  printerr:
    mov al, [bx]
    cmp al, 0
    je end
    inc bx
    jmp printerr

end:
  cli
  hlt
  jmp $

errorline:
  db "Couldn't go and execute from sector 1", 0

times 510-($-$$) db 0
dw 0x55AA

;outside the boot sector

jmp target

target:
  mov bx, [targetline]
  mov ah, 0x0e
  printtarg:
    mov al, [bx]
    cmp al, 0
    je end
    int 0x10
    inc bx
    jmp printtarg

end:
  cli
  hlt
  jmp $

targetline:
  db "Success.", 0

;end of the sector 1 code
times 0x400-($-$$) db 0

I'm struggling with that problem from couple of days now (Defining sector 1 in order to go and print something.), and still seeking for the constructive explanation and direct solution from people who are more experienced. Preferably with the full code, I'll be grateful for your help

1

There are 1 answers

3
Sep Roland On

A number of problems in this NASM code

  • Not setting up the segment registers DS and ES
  • Not providing an adequate stack through setting SS:SP
  • Not specifying a buffer address for the BIOS.ReadSector function
  • Not realizing that sector numbers are 1-based (instead of 0-based like the head- and the cylinder numbers are)
  • Loading the first character of a message where loading the address itself is required
  • Forgetting the int 10h to actually perform the BIOS.Teletype function
  • Using an hardcoded mov dl, 0x00 instead of trusting the DL value that BIOS will have passed to the bootloader at 0x7C00
  • Using a wrong signature on the bootsector which should have made it unrecognizable to BIOS
  • Redefining the end label. Didn't NASM complain about this?

The improved code

org 0x7C00
bits 16

XOR AX, AX
MOV DS, AX
MOV ES, AX
MOV SS, AX
MOV SP, 0x7C00

MOV BX, 0x7E00            ; Full pointer is ES:BX
mov ah, 0x02              ; BIOS.ReadSector
mov al, 1
mov ch, 0
mov cl, 2                 ; Sector 1 is executing already! We need sector 2
mov dh, 0                 ; BIOS gave us DL
int 13h
jc  error
jmp target                ; Corresponds to address 0x7E00

error:
  mov bx, errorline       ; This loads the address itself
  mov ah, 0x0E            ; BIOS.Teletype
  printerr:
    mov al, [bx]
    cmp al, 0
    je  end
    int 10h               ; Actually invoking BIOS
    inc bx
    jmp printerr

end:
  cli
  hlt
  jmp end

errorline:
  db "Couldn't go and execute from sector 2", 0

times 510-($-$$) db 0
dw 0xAA55                 ; In memory first byte is 55h, second byte is AAh  

;outside the boot sector
; HERE STARTS SECTOR 2

target:
  mov bx, targetline      ; This loads the address itself
  mov al, [bx]            ; First char is non-zero for sure
  printtarg:
    mov ah, 0x0E          ; BIOS.Teletype
    int 0x10
    inc bx
    mov al, [bx]
    cmp al, 0             ; Efficient loops have their loop condition below
    jne printtarg
    jmp end               ; Sector 1 is still there, so re-use some of its code
                          ; Could have done the same for the printing of course!
targetline:
  db "Success.", 0

;end of the sectors 1 and 2 code
times 1024-($-$$) db 0    ; No signature needed here

It just print one miscellaneous symbol (≡) instead. If there's any unnecessary code lines that cause that problem, you can point to which one to get rid of. Code line numbers are optional, for better clarification.

The above full code should resolve the issues. Pay attention to the tail comments I added in the program.