Pages

Monday, November 25, 2024

Fedora 41 : assembly fasm with cat command and qemu test.

I tested the basic direct binary concatenation where the second file is appended to the end of the first file, preserving all bytes exactly as they are without any linking metadata or relocations with the cat command.
Let's see how can do this.
You need to create two files: kernel.fasm and kernel2.fasm.
First will like this:
org 7C00h

; First stage bootloader
start:
    mov [bootdrive], dl
    mov ax, 0x2000  ; Load kernel at 0x2000:0
    mov es, ax
    xor bx, bx      ; ES:BX = buffer
    
    mov ah, 02h     ; Read sectors
    mov al, 6       ; Number of sectors to read
    mov ch, 0       ; Cylinder 0
    mov cl, 2       ; Start from sector 2
    mov dh, 0       ; Head 0
    mov dl, [bootdrive]
    int 13h
    
    jmp 0x2000:0    ; Jump to second stage

bootdrive db 0
times 510-($-$$) db 0
dw 0xAA55
The second one named kernel2.fasm will come with new features:
org 0

COLS equ 80
ROWS equ 25
VIDEO_MEM equ 0xB800

; Box drawing characters
BOX_DR    equ 201  ; ╔
BOX_HL    equ 205  ; ═
BOX_DL    equ 187  ; ╗
BOX_VL    equ 186  ; ║
BOX_UR    equ 200  ; ╚
BOX_UL    equ 188  ; ╝
BOX_BLOCK equ 219  ; █
...
Use fasm and cat commands to create the bin files and the result file for qemu:
mythcat@localhost:~/fasm$ ./fasm.x64 kernel.fasm kernel.bin
flat assembler  version 1.73.32  (16384 kilobytes memory, x64)
2 passes, 512 bytes.
mythcat@localhost:~/fasm$ ./fasm.x64 kernel2.fasm kernel2.bin
flat assembler  version 1.73.32  (16384 kilobytes memory, x64)
2 passes, 132 bytes.
mythcat@localhost:~/fasm$ cat kernel.bin kernel2.bin > os.img
The last step is to run qemu-system-i386 to test the result
mythcat@localhost:~/fasm$ qemu-system-i386 -fda os.img
The result is this: