diff --git a/src/common/common.c b/src/common/common.c new file mode 100644 index 0000000..28dcfae --- /dev/null +++ b/src/common/common.c @@ -0,0 +1,61 @@ +#include "common.h" + +void print(char* string) { + asm volatile ("mov $0, %%eax; mov %0, %%esi; int $0x7f" : : "m" (string) : "eax", "esi"); +} + +char getch() { + asm volatile ("mov $1, %%eax; int $0x7f" : : :); +} + +uint8_t getchPS2() { + asm volatile ("mov $2, %%eax; int $0x7f" : : :); +} + +void readfile(char* filename, uint8_t* buffer) { + asm volatile ("mov $3, %%eax; mov %0, %%esi; mov %1, %%edi; int $0x7f" : : "m" (filename), "m" (buffer) : "eax", "esi", "edi"); +} + +void* localalloc(uint32_t size) { + asm volatile ("mov $4, %%eax; mov %0, %%esi; int $0x7f" : : "m" (size) : "esi"); +} + +void localdelete(void* ptr) { + asm volatile ("mov $5, %%eax; mov %0, %%esi; int $0x7f" : : "m" (ptr) : "esi"); +} + +uint32_t filesize(char* filename) { + asm volatile ("mov $6, %%eax; mov %0, %%esi; int $0x7f" : : "m" (filename) : "esi"); +} + +int int_to_decimal(unsigned int number, char* string_buffer) { + for (int i=0; i<11; i++) + string_buffer[i] = 0; + + int index = 9; + unsigned int acc = number; + if (acc == 0) + string_buffer[index--] = '0'; + while (acc != 0) { + string_buffer[index--] = 0x30+(acc%10); + acc /= 10; + } + return (index+1); +} + +char dec_to_hex[16] = "0123456789abcdef"; +int int_to_hex(unsigned int number, char* string_buffer) { + for (int i=0; i<8; i++) + string_buffer[i] = '0'; + string_buffer[8] = 0; + + int index = 7; + unsigned int acc = number; + if (acc == 0) + string_buffer[index--] = '0'; + while (acc != 0) { + string_buffer[index--] = dec_to_hex[acc%0x10]; + acc /= 0x10; + } + return (index+1); +} \ No newline at end of file diff --git a/src/common/common.h b/src/common/common.h new file mode 100644 index 0000000..bde0021 --- /dev/null +++ b/src/common/common.h @@ -0,0 +1,17 @@ +#ifndef COMMON_H +#define COMMON_H + +#include "../kernel/types.h" + +void print(char* string); +char getch(); +uint8_t getchPS2(); +void readfile(char* filename, uint8_t* buffer); +void* localalloc(uint32_t size); +void localdelete(void* ptr); +uint32_t filesize(char* filename); + +int int_to_decimal(unsigned int number, char* string_buffer); +int int_to_hex(unsigned int number, char* string_buffer); + +#endif \ No newline at end of file diff --git a/src/program/hello.c b/src/program/hello.c new file mode 100644 index 0000000..659d50e --- /dev/null +++ b/src/program/hello.c @@ -0,0 +1,7 @@ +#include "../common/common.h" + +int main() { + while (1) { + print("Hello"); + } +} \ No newline at end of file diff --git a/src/program/hello.ld b/src/program/hello.ld new file mode 100644 index 0000000..8503d53 --- /dev/null +++ b/src/program/hello.ld @@ -0,0 +1,14 @@ +OUTPUT_FORMAT(binary) +OUTPUT_ARCH(i386:i386) + +OUTPUT(build/program/hello.bin) + +SECTIONS { + . = 0x8020; + + .text : { + build/program_code_entry.o(.text) + build/program/hello.o(.text) + build/common/common.o(.text) + } +} \ No newline at end of file diff --git a/src/program/program.c b/src/program/program.c index b9f390b..e220caf 100644 --- a/src/program/program.c +++ b/src/program/program.c @@ -1,64 +1,5 @@ #include "../kernel/types.h" - -void print(char* string) { - asm volatile ("mov $0, %%eax; mov %0, %%esi; int $0x7f" : : "m" (string) : "eax", "esi"); -} - -char getch() { - asm volatile ("mov $1, %%eax; int $0x7f" : : :); -} - -uint8_t getchPS2() { - asm volatile ("mov $2, %%eax; int $0x7f" : : :); -} - -void readfile(char* filename, uint8_t* buffer) { - asm volatile ("mov $3, %%eax; mov %0, %%esi; mov %1, %%edi; int $0x7f" : : "m" (filename), "m" (buffer) : "eax", "esi", "edi"); -} - -void* localalloc(uint32_t size) { - asm volatile ("mov $4, %%eax; mov %0, %%esi; int $0x7f" : : "m" (size) : "esi"); -} - -void localdelete(void* ptr) { - asm volatile ("mov $5, %%eax; mov %0, %%esi; int $0x7f" : : "m" (ptr) : "esi"); -} - -uint32_t filesize(char* filename) { - asm volatile ("mov $6, %%eax; mov %0, %%esi; int $0x7f" : : "m" (filename) : "esi"); -} - -int int_to_decimal(unsigned int number, char* string_buffer) { - for (int i=0; i<11; i++) - string_buffer[i] = 0; - - int index = 9; - unsigned int acc = number; - if (acc == 0) - string_buffer[index--] = '0'; - while (acc != 0) { - string_buffer[index--] = 0x30+(acc%10); - acc /= 10; - } - return (index+1); -} - -char dec_to_hex[16] = "0123456789abcdef"; -int int_to_hex(unsigned int number, char* string_buffer) { - for (int i=0; i<8; i++) - string_buffer[i] = '0'; - string_buffer[8] = 0; - - int index = 7; - unsigned int acc = number; - if (acc == 0) - string_buffer[index--] = '0'; - while (acc != 0) { - string_buffer[index--] = dec_to_hex[acc%0x10]; - acc /= 0x10; - } - return (index+1); -} +#include "../common/common.h" int main() { print("Testing C code program\n"); @@ -76,5 +17,4 @@ int main() { print("\n"); readfile("HELLO TXT", alpha_buffer); print(alpha_buffer); - while (1); } \ No newline at end of file diff --git a/src/program/program.ld b/src/program/program.ld index ae32311..89fa91f 100644 --- a/src/program/program.ld +++ b/src/program/program.ld @@ -9,5 +9,6 @@ SECTIONS { .text : { build/program_code_entry.o(.text) build/program/program.o(.text) + build/common/common.o(.text) } } \ No newline at end of file diff --git a/src/program/world.c b/src/program/world.c new file mode 100644 index 0000000..8603aae --- /dev/null +++ b/src/program/world.c @@ -0,0 +1,7 @@ +#include "../common/common.h" + +int main() { + while (1) { + print("World"); + } +} \ No newline at end of file diff --git a/src/program/world.ld b/src/program/world.ld new file mode 100644 index 0000000..055bbc1 --- /dev/null +++ b/src/program/world.ld @@ -0,0 +1,14 @@ +OUTPUT_FORMAT(binary) +OUTPUT_ARCH(i386:i386) + +OUTPUT(build/program/world.bin) + +SECTIONS { + . = 0x8020; + + .text : { + build/program_code_entry.o(.text) + build/program/world.o(.text) + build/common/common.o(.text) + } +} \ No newline at end of file