C lang goes crazy???
Posted: Thu Apr 28, 2022 4:28 am
I'm writing a simple OS, mainly to practice C and 'cause it fun.
I've already written a bootloader (in asm), I've successfully moved to 32 bit protected mode and I run and load C code.
Currently I'm trying to build a simple screen driver.
I'm initializing this pointer (so far, nothing wrong).
now I want to access the pointer.
now I want to do it with a variable
The code doesn't crash, it will access always the same place no matter if I change the offset variable or not.
Lets say I want to access the pointer in other way.
I don't have a clue of what causing this, maybe I got something wrong with my C code or I have some bug in the bootloader or something .
[EDIT]
bootlaoder:
makefile:
I've already written a bootloader (in asm), I've successfully moved to 32 bit protected mode and I run and load C code.
Currently I'm trying to build a simple screen driver.
I'm initializing this pointer (so far, nothing wrong).
Code: Select all
unsigned char *video_memory = (unsigned char*) VIDEO_ADDRESS;
Code: Select all
video_memory[0]; (so far so god)
Code: Select all
int offset = 2;
video_memory[offset]; (now it doesn't work)
Lets say I want to access the pointer in other way.
Code: Select all
video_memory += offset;
*video_memory = lalala (it will work)
[EDIT]
bootlaoder:
Code: Select all
[org 0x7c00] ; bootloader offset
KERNEL_OFFSET equ 0x1000
mov [BOOT_DRIVE], dl
mov bp, 0x9000 ; set the stack
mov sp, bp
mov bx, MSG_REAL_MODE
call print.print_string ; This will be written after the BIOS messages
call load_kernel
call switch_to_pm ; We are never return from here.
jmp $ ; this will actually never be executed
%include "src/boot/modules/disk.asm"
%include "src/boot/switch_pm/gdt.asm"
%include "src/boot/modules/console.asm"
%include "src/boot/modules/console32.asm"
%include "src/boot/switch_pm/switch_to_pm.asm"
[bits 16]
; loads the kernel
load_kernel:
mov bx, MSG_LOAD_KERNEL
call print.print_string
mov bx, KERNEL_OFFSET
mov dh, 15
mov dl, [BOOT_DRIVE]
call disk.load
ret
[bits 32]
BEGIN_PM: ; after the switch we will get here
call console.clear
mov ebx, MSG_PROT_MODE
call console.print_string_pm ; Note that this will be written at the top left corner
call KERNEL_OFFSET
jmp $
; globals
BOOT_DRIVE db 0
MSG_REAL_MODE db "Started in 16-bit real mode", 0
MSG_PROT_MODE db "Loaded 32-bit protected mode", 0
MSG_LOAD_KERNEL db "Starts loading the kernel", 0
; bootsector
times 510-($-$$) db 0
dw 0xaa55
Code: Select all
OSNAME = "SuperDuperOS"
C_SOURCES = $(wildcard src/kernel/*.c src/drivers/*.c)
HEADERS = $(wildcard kernel /*.h drivers /*.h)
OBJ = ${C_SOURCES:.c=.o}
# call with make src='your-asm-file.asm'
# Example make src=bootloader.asm
all: os-image
# Compiles the bootloader
bin/bootloader.bin: src/boot/bootloader.asm
nasm $< -f bin -o $@
# Compiles the kernel_entry.
bin/kernel_entry.o: src/boot/kernel_entry.asm
nasm $< -f elf64 -o $@
# Compiles the kernel.
bin/kernel.o: src/kernel/kernel.c
gcc -ffreestanding -c $< -o $@
bin/kernel.bin: bin/kernel_entry.o ${OBJ}
ld -o $@ -Ttext 0x1000 $^ --oformat binary
# Generic rule for building ’somefile.o’ from ’somefile.c’
%.o: %.c ${HEADERS}
gcc -ffreestanding -c $< -o $@
# lunch the last binary via qemu
qemu:
qemu-system-x86_64 bin/$(OSNAME)
# This is the actual disk image that the computer loads ,
# which is the combination of our compiled bootsector and kernel
os-image: bin/bootloader.bin bin/kernel.bin
cat $^ > bin/$(OSNAME)
# cleans the entire bin folder.
clean:
rm -r bin || true
mkdir bin
# Disassemble our kernel - might be useful for debugging.
bin/kernel.dis : bin/kernel.bin
ndisasm -b 32 $< > $@