mes/scaffold/m.c

166 lines
3.0 KiB
C

/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* Mes 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 3 of the License, or (at
* your option) any later version.
*
* Mes 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
int g_stdin = 0;
typedef long size_t;
#if __GNUC__
void
exit (int code)
{
asm (
"movl %0,%%ebx\n\t"
"movl $1,%%eax\n\t"
"int $0x80"
: // no outputs "=" (r)
: "" (code)
);
// not reached
exit (0);
}
int
read (int fd, void* buf, size_t n)
{
int r;
//syscall (SYS_write, fd, s, n));
asm (
"movl %1,%%ebx\n\t"
"movl %2,%%ecx\n\t"
"movl %3,%%edx\n\t"
"movl $0x3,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (fd), "" (buf), "" (n)
: "eax", "ebx", "ecx", "edx"
);
return r;
}
int
open (char const *s, int mode)
{
int r;
//syscall (SYS_open, mode));
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov $0x5,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (s), "" (mode)
: "eax", "ebx", "ecx"
);
return r;
}
int
getchar ()
{
char c;
int r = read (g_stdin, &c, 1);
if (r < 1) return -1;
return c;
}
void
write (int fd, char const* s, int n)
{
int r;
//syscall (SYS_write, fd, s, n));
asm (
"mov %0,%%ebx\n\t"
"mov %1,%%ecx\n\t"
"mov %2,%%edx\n\t"
"mov $0x4, %%eax\n\t"
"int $0x80\n\t"
: // no outputs "=" (r)
: "" (fd), "" (s), "" (n)
: "eax", "ebx", "ecx", "edx"
);
}
int
putchar (int c)
{
//write (STDOUT, s, strlen (s));
//int i = write (STDOUT, s, strlen (s));
write (1, (char*)&c, 1);
return 0;
}
size_t
strlen (char const* s)
{
int i = 0;
while (s[i]) i++;
return i;
}
int
puts (char const* s)
{
//write (STDOUT, s, strlen (s));
//int i = write (STDOUT, s, strlen (s));
int i = strlen (s);
write (1, s, i);
return 0;
}
#endif
int
main (int argc, char *argv[])
{
g_stdin = open ("mesmes", 0);
int c = getchar ();
while (c != -1) {
putchar (c);
c = getchar ();
}
return c;
}
#if __GNUC__
void
_start ()
{
int r;
asm (
"mov %%ebp,%%eax\n\t"
"addl $8,%%eax\n\t"
"push %%eax\n\t"
"mov %%ebp,%%eax\n\t"
"addl $4,%%eax\n\t"
"movzbl (%%eax),%%eax\n\t"
"push %%eax\n\t"
"call main\n\t"
"movl %%eax,%0\n\t"
: "=r" (r)
: //no inputs "" (&main)
);
exit (r);
}
#endif