diff --git a/Include/Iony/Utils/System.h b/Include/Iony/Utils/System.h new file mode 100644 index 0000000..c462428 --- /dev/null +++ b/Include/Iony/Utils/System.h @@ -0,0 +1,99 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: System.h + * Description: + * This file contains specific system helper functions and constants for linux + * and windows. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef IONY_SYSTEM +#define IONY_SYSTEM + +#if defined(__GNUC__) +#if defined(__x86_64__) +#define SYS_WRITE 1 +#elif defined(__i386__) +#define SYS_WRITE 4 +#elif defined(__aarch64__) +#define SYS_WRITE 64 +#elif defined(__arm__) +#define SYS_WRITE 4 +#endif + +static inline long iony_system_call(long number, long argument1, long argument2, long argument3, long argument4, long argument5, long argument6) { + long result = 0; + #if defined(__x86_64__) + asm volatile( + "movq %1, %%rax\n" + "movq %2, %%rdi\n" + "movq %3, %%rsi\n" + "movq %4, %%rdx\n" + "movq %5, %%r10\n" + "movq %6, %%r8\n" + "movq %7, %%r9\n" + "syscall\n" + "movq %%rax, %0\n" + : "=r"(result) + : "r"(number), "r"(argument1), "r"(argument2), "r"(argument3), "r"(argument4), "r"(argument5), "r"(argument6) + : "rax", "rdi", "rsi", "rdx", "r10", "r8", "r9", "memory" + ); + #elif defined(__i386__) + asm volatile( + "movl %1, %%eax\n" + "movl %2, %%ebx\n" + "movl %3, %%ecx\n" + "movl %4, %%edx\n" + "movl %5, %%esi\n" + "movl %6, %%edi\n" + "movl %7, %%ebp\n" + "int $0x80\n" + "movl %%eax, %0\n" + : "=r"(result) + : "r"(number), "r"(argument1), "r"(argument2), "r"(argument3),"r"(argument4), "r"(argument5), "r"(argument6) + : "eax","ebx","ecx","edx","esi","edi","ebp","memory" + ); + #elif defined(__aarch64__) + register long x0 asm("x0") = argument1; + register long x1 asm("x1") = argument2; + register long x2 asm("x2") = argument3; + register long x3 asm("x3") = argument4; + register long x4 asm("x4") = argument5; + register long x5 asm("x5") = argument6; + register long x8 asm("x8") = number; + + asm volatile("svc 0" : "+r"(x0) : "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5), "r"(x8) : "memory"); + result = x0; + #elif defined(__arm__) + register long r0 asm("r0") = argument1; + register long r1 asm("r1") = argument2; + register long r2 asm("r2") = argument3; + register long r3 asm("r3") = argument4; + register long r4 asm("r4") = argument5; + register long r5 asm("r5") = argument6; + register long r7 asm("r7") = number; + + asm volatile("swi 0" : "+r"(r0) : "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5), "r"(r7) : "memory"); + result = r0; + #else + #error "Unsupported architecture" + #endif + + return result; +} +#endif + +#endif diff --git a/Src/Core/Console.c b/Src/Core/Console.c index 0f26387..9e43e1d 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -24,7 +24,7 @@ #if defined(_WIN32) #include #elif defined(__GNUC__) - +#include "../../Include/Iony/Utils/System.h" #endif IonyError iony_print(char *text) { @@ -32,7 +32,7 @@ IonyError iony_print(char *text) { return IONY_ERROR_INVALID_POINTER; } - unsigned long text_length = 0; + long text_length = 0; while (text[text_length] != '\0') { text_length += 1; } @@ -43,7 +43,7 @@ IonyError iony_print(char *text) { DWORD characters_written; WriteFile(standard_console_out_handle, text, text_length, &characters_written, 0); #elif defined(__GNUC__) - + iony_system_call(SYS_WRITE, 1, (long)text, text_length, 0, 0, 0); #endif return IONY_ERROR_NONE;