Separating itoa and ltoa to own file

This commit is contained in:
Jarkko Toivanen 2023-06-15 06:40:38 +03:00
parent dd78c1cc7d
commit 76c9075f71
Signed by: jt
GPG Key ID: 9151B109B73ECAD5
4 changed files with 67 additions and 33 deletions

View File

@ -10,9 +10,10 @@ vga.o: vga.c
tcc -m32 -c vga.c
serial.o: serial.c
tcc -m32 -c serial.c
kernel-i386.elf: kernel.o start32.o vga.o serial.o
tcc -m32 -nostdlib -Wl,-Ttext,0x100000 start32.o kernel.o vga.o serial.o -o kernel-i386.elf
xtoa.o: xtoa.c
tcc -m32 -c xtoa.c
kernel-i386.elf: kernel.o start32.o vga.o serial.o xtoa.o
tcc -m32 -nostdlib -Wl,-Ttext,0x100000 start32.o kernel.o vga.o serial.o xtoa.o -o kernel-i386.elf
qemu-multiboot: kernel-i386.elf
qemu-system-i386 -kernel kernel-i386.elf -serial stdio
@ -23,7 +24,7 @@ qemu-image: kernel-i386.elf mount
mount: koalemos.img mnt/
@if ! mountpoint -q "mnt/"; then \
sudo mount -o loop,offset=1048576,umask=177,dmask=222,uid=$(shell id -u),gid=$(shell id -g) koalemos.img mnt/; \
sudo mount -o loop,offset=1048576,umask=177,dmask=022,uid=$(shell id -u),gid=$(shell id -g) koalemos.img mnt/; \
fi;
umount:
@sudo umount mnt

View File

@ -1,6 +1,7 @@
#include "multiboot.h"
#include "vga.h"
#include "serial.h"
#include "xtoa.h"
static inline void outb(unsigned short port, unsigned char val) {
asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port) : "memory");
@ -11,32 +12,6 @@ static inline unsigned char inb(unsigned short port) {
return ret;
}
char* itoa(int value, int base) {
char* result;
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
void kmain (unsigned int mbootmagick, multiboot_info_t* mbootinfo) {
// Cursor disabling
@ -83,11 +58,11 @@ void kmain (unsigned int mbootmagick, multiboot_info_t* mbootinfo) {
if (mbootinfo->flags & MULTIBOOT_INFO_FRAMEBUFFER_INFO) {
vga_write("\nFramebuffer: ");
vga_write("address: 0x");
vga_write(itoa(mbootinfo->framebuffer_addr, 16));
vga_write(ltoa(mbootinfo->framebuffer_addr, 16));
serial_write_string("\nFramebuffer: ");
serial_write_string("address: 0x");
serial_write_string(itoa(mbootinfo->framebuffer_addr, 16));
unsigned long *vmem = &mbootinfo->framebuffer_addr;
serial_write_string(ltoa(mbootinfo->framebuffer_addr, 16));
unsigned long *vmem = mbootinfo->framebuffer_addr;
*vmem = 0xff00ff;
} else {

51
xtoa.c Normal file
View File

@ -0,0 +1,51 @@
#include "xtoa.h"
char* itoa(int value, int base) {
char* result;
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
char* ltoa(long value, int base) {
char* result;
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
}

7
xtoa.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef HEADER_XTOA
#define HEADER_XTOA
char* itoa(int value, int base);
char* ltoa(long value, int base);
#endif