From c185345dd341ce4412a169085ab09ca85b017268 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Thu, 27 Jan 2022 23:09:49 +0500 Subject: [PATCH] Split into sections --- build.sh | 7 +++++-- src/x86/stage1.S | 18 ++++++------------ src/x86/stage1.ld | 2 ++ src/x86/stage2.S | 9 +++------ src/x86/stage2.ld | 2 ++ src/x86/start.S | 11 +++++++++++ 6 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 src/x86/start.S diff --git a/build.sh b/build.sh index 27cef5b..db09cec 100755 --- a/build.sh +++ b/build.sh @@ -8,9 +8,12 @@ CROSS="$BIN/i386-elf-" ./clean.sh +${CROSS}gcc -c $SRC/start.S -o $SRC/start.o ${CROSS}gcc -c $SRC/stage1.S -o $SRC/stage1.o ${CROSS}gcc -c $SRC/stage2.S -o $SRC/stage2.o -${CROSS}ld -T$SRC/stage1.ld -o $SRC/stage1.bin $SRC/stage1.o -${CROSS}ld -T$SRC/stage2.ld -o $SRC/stage2.bin $SRC/stage2.o + +${CROSS}ld -T$SRC/stage1.ld -o $SRC/stage1.bin $SRC/start.o $SRC/stage1.o +${CROSS}ld -T$SRC/stage2.ld -o $SRC/stage2.bin $SRC/start.o $SRC/stage2.o + ./testyboot mbr mbr.bin $SRC/stage1.bin cat mbr.bin $SRC/stage2.bin > disk.img diff --git a/src/x86/stage1.S b/src/x86/stage1.S index 02b604a..c462628 100644 --- a/src/x86/stage1.S +++ b/src/x86/stage1.S @@ -4,18 +4,12 @@ #define STACK_BASE 0x2000 #define STAGE2_BASE 0x7e00 - -#define DIST(x) (x - _start) - -#define STAGE2_ADDR(x) (DIST(x) + STAGE2_BASE) +#define STAGE2_SIZE 0x7e06 .code16 -.global _start - -.section .text -_start: - ljmp $0, $main +.global init +.section .init .align 2 size: .word 0 @@ -29,7 +23,7 @@ parenth: .string " (" of: .string " of " to_address: .string ") to address " -main: +init: cli xor %ax, %ax mov %ax, %ss @@ -58,14 +52,14 @@ main: mov $STAGE2_BASE, %bx // Address int $0x13 - mov $STAGE2_ADDR(size), %bx + mov $STAGE2_SIZE, %bx mov (%bx), %ax call print_stage2_size // DX - dividend high (always zero) xor %dx, %dx // AX - dividend low - mov $STAGE2_ADDR(size), %bx + mov $STAGE2_SIZE, %bx mov (%bx), %ax // BX - divisor (always 512) mov $512, %bx diff --git a/src/x86/stage1.ld b/src/x86/stage1.ld index 44a521e..018a8e5 100644 --- a/src/x86/stage1.ld +++ b/src/x86/stage1.ld @@ -11,6 +11,8 @@ SECTIONS .text : { + *(.start); + *(.init); *(.text); } diff --git a/src/x86/stage2.S b/src/x86/stage2.S index 4928252..db06de1 100644 --- a/src/x86/stage2.S +++ b/src/x86/stage2.S @@ -3,13 +3,10 @@ #endif .code16 -.global _start +.global init .extern program_size -.section .text -_start: - ljmp $0, $main - +.section .init .align 2 size: .word program_size @@ -17,7 +14,7 @@ hello2: .string "Hello from stage 2!\r\n" hello3: .string "Hello from sector 3!\r\n" hello4: .string "Hello from sector 4!\r\n" -main: +init: mov $hello2, %si call print_str diff --git a/src/x86/stage2.ld b/src/x86/stage2.ld index d5cbf88..08399b4 100644 --- a/src/x86/stage2.ld +++ b/src/x86/stage2.ld @@ -11,6 +11,8 @@ SECTIONS .text : { + *(.start); + *(.init); *(.text); } diff --git a/src/x86/start.S b/src/x86/start.S new file mode 100644 index 0000000..d4a534d --- /dev/null +++ b/src/x86/start.S @@ -0,0 +1,11 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +.code16 +.global _start +.extern init + +.section .start +_start: + ljmp $0, $init