Skip to content
Permalink
Browse files

Add a simple echo program written in C

- Implement a minimal read() syscall (for serial and text console)
- Implement keyboard circular buffer
  • Loading branch information...
rene committed May 26, 2018
1 parent d47c434 commit 0a0e27494138402785494b271eae88b91e832f9c
@@ -0,0 +1,25 @@
##
# Makefile for calc.
#
CC=gcc
CFLAGS=-m32 -Os -mpush-args -fno-pie -fomit-frame-pointer -fno-builtin -nostartfiles -nostdlib -nodefaultlibs
AS=as
LD=ld
OUTPUT=init

OBJECTS=crt0.o syscalls.o printf.o stdlib.o init.o

all: $(OBJECTS)
$(LD) $(OBJECTS) -melf_i386 -Ttext=C0000C --oformat=binary -o $(OUTPUT)
#$(LD) $(OBJECTS) -melf_i386 -o $(OUTPUT)

%.o: %.c
$(CC) $(CFLAGS) -c $<

%.o: %.s
$(AS) --32 $< -o $@

clean:
@rm $(OBJECTS) || true
@[ -f $(OUTPUT) ] && rm $(OUTPUT) || true

@@ -0,0 +1,9 @@
.global _start,main

.text
_start:
call main

loop:
jmp loop

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2009 Renê de Souza Pinto
* Tempos - Tempos is an Educational and multi purpose Operating System
*
* File: ctype.h
*
* This file is part of TempOS.
*
* TempOS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* TempOS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef CTYPE_H

#define CTYPE_H

int isalnum(int c);
int isalpha(int c);
int isascii(int c);
int isblank(int c);
int iscntrl(int c);
int isdigit(int c);
int isgraph(int c);
int islower(int c);
int isprint(int c);
int ispunct(int c);
int isspace(int c);
int isupper(int c);
int isxdigit(int c);

#endif /* CTYPE_H */

@@ -0,0 +1,35 @@
#include "minilibc.h"

int main(void)
{
char wmsg[] = "Welcome to Echo program (written in C)!\n";
char prompt[] = "> ";
char token[512];
char key[2];
int i, p;

_printf(wmsg);
_printf(prompt);
p = 0;
while (1) {
/* Read a key */
if (read(0, key, 1) > 0) {
switch (key[0]) {
case '\r':
case '\n':
token[p] = '\0';
_printf("Echo: %s\n", token);
_printf(prompt);
p = 0;
break;

default:
token[p++] = key[0];
}
}
}

for(;;);
return 0;
}

@@ -0,0 +1,18 @@

#ifndef _MINILIBC
#define _MINILIBC

#include "stdarg.h"

#define CHECK_BIT(a, b) ((a >> b) & 0x01)
#define SET_BIT(a, b) a |= (0x01 << b)

long write(int fd, void *buf, unsigned long count);
long read(int fd, void *buf, unsigned long count);
int vsprintf(char *str, const char *format, va_list ap);
int sprintf(char *str, const char *format, ...);
int _printf(const char *format, ...);
int atoi(const char *nptr);

#endif

0 comments on commit 0a0e274

Please sign in to comment.
You can’t perform that action at this time.