Skip to content

Commit

Permalink
Add string and monitor write hex and dec functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpas committed Jan 18, 2018
1 parent 3d3cb9e commit 69f48b4
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
BIN = bin
SRC = src
CC = gcc
CFLAGS = -Wall -Wno-main -fno-builtin -fno-stack-protector -m32 -nostdlib -nostdinc -std=gnu99 -ggdb3
CFLAGS = -Wall -ffreestanding -m32 -std=gnu99 -ggdb3
LINK = ld
LINKFLAGS = -melf_i386 -Tlink.ld
CASM = nasm
Expand Down
46 changes: 40 additions & 6 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,58 @@ u16int inw(u16int port) {
}

void memcpy(u8int *dest, const u8int *src, u32int len) {
// TODO Implement
const u8int *sp = (const u8int *)src;
u8int *dp = (u8int *)dest;

while (len != 0) {
*dp++ = *sp++;
--len;
}
}

void memset(u8int *dest, u8int val, u32int len) {
// TODO Implement
u8int *temp = (u8int *)dest;

while (len != 0) {
*temp++ = val;
--len;
}
}

int strcmp(char *str1, char *str2) {
// TODO Implement
return 0;
int failed = 0;
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if(str1[i] != str2[i]) {
failed = 1;
break;
}
++i;
}

if ((str1[i] == '\0' && str2[i] != '\0') || (str1[i] != '\0' && str2[i] == '\0')) {
failed = 1;
}

return failed;
}

char *strcpy(char *dest, const char *src) {
// TODO Implement
do {
*dest++ = *src++;
} while (*src != 0);

return dest;
}

char *strcat(char *dest, const char *src) {
// TODO Implement
while (*dest != 0) {
*dest = *dest + 1;
}

do {
*dest++ = *src++;
} while (*src != 0);

return dest;
}
2 changes: 2 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#pragma once

#include <stddef.h>

// typedefs to standardise sizes across platforms, written for 32-bit x86
typedef unsigned int u32int;
typedef int s32int;
Expand Down
4 changes: 3 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ struct multiboot;

int main(struct multiboot *mboot_ptr) {
monitor_clear();
monitor_write("Welcome to Tyr, the one-handed OS!");
monitor_write("Welcome to Tyr (");
monitor_write_hex(0x547972);
monitor_write("), the one-handed OS!");

return 0;
}
50 changes: 48 additions & 2 deletions src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,55 @@ void monitor_write(char *c) {
}

void monitor_write_hex(u32int n) {
// TODO Implement
s32int tmp;
monitor_write("0x");

char noZeroes = 1;
for (int i = 28; i > 0; i -= 4) {
tmp = (n >> i) & 0xF;
if (tmp == 0 && noZeroes != 0) {
continue;
}

noZeroes = 0;
if (tmp >= 0xA) {
monitor_put(tmp - 0xA + 'A');
} else {
monitor_put(tmp + '0');
}
}

tmp = n & 0xF;
if (tmp >= 0xA) {
monitor_put(tmp - 0xA + 'A');
} else {
monitor_put(tmp + '0');
}
}

void monitor_write_dec(u32int n) {
// TODO Implement
if (n == 0) {
monitor_put('0');
return;
}

char c[32];

s32int acc = n;
int i = 0;
while (acc > 0) {
c[i] = '0' + acc%10;
acc /= 10;
++i;
}
c[i] = 0;

char c2[32];
c2[i--] = 0;
int j = 0;
while(i >= 0) {
c2[i--] = c[j++];
}

monitor_write(c2);
}

0 comments on commit 69f48b4

Please sign in to comment.