Moved syscall related code to common. Add hello.c and world.c in preparation for testing pre-emptive multitasking

This commit is contained in:
Xnoe 2021-11-27 13:10:20 +00:00
parent c84a3ffdea
commit bfa648d96b
Signed by: xnoe
GPG Key ID: 45AC398F44F0DAFE
8 changed files with 122 additions and 61 deletions

61
src/common/common.c Normal file
View File

@ -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);
}

17
src/common/common.h Normal file
View File

@ -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

7
src/program/hello.c Normal file
View File

@ -0,0 +1,7 @@
#include "../common/common.h"
int main() {
while (1) {
print("Hello");
}
}

14
src/program/hello.ld Normal file
View File

@ -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)
}
}

View File

@ -1,64 +1,5 @@
#include "../kernel/types.h" #include "../kernel/types.h"
#include "../common/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);
}
int main() { int main() {
print("Testing C code program\n"); print("Testing C code program\n");
@ -76,5 +17,4 @@ int main() {
print("\n"); print("\n");
readfile("HELLO TXT", alpha_buffer); readfile("HELLO TXT", alpha_buffer);
print(alpha_buffer); print(alpha_buffer);
while (1);
} }

View File

@ -9,5 +9,6 @@ SECTIONS {
.text : { .text : {
build/program_code_entry.o(.text) build/program_code_entry.o(.text)
build/program/program.o(.text) build/program/program.o(.text)
build/common/common.o(.text)
} }
} }

7
src/program/world.c Normal file
View File

@ -0,0 +1,7 @@
#include "../common/common.h"
int main() {
while (1) {
print("World");
}
}

14
src/program/world.ld Normal file
View File

@ -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)
}
}