-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanolib.c
73 lines (65 loc) · 1.08 KB
/
nanolib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "nanolib.h"
void *memcpy(void *dest, const void *source, size_t n)
{
char *dst = dest;
const char *src = source;
while (n--)
*dst++ = *src++;
return dest;
}
void *memset(void *s, int c, size_t n)
{
char *dst = s;
while (n--)
*dst++ = c;
return s;
}
int memcmp(const void *s1, const void *s2, size_t n)
{
const uint8_t *p = s1;
const uint8_t *q = s2;
while (n--)
{
int diff = (int)*p - (int)*q;
if (diff)
return diff;
p++;
q++;
}
return 0;
}
void hexstr(uint8_t *data, char *str, size_t n)
{
const char hex[] = "0123456789abcdef";
while (n--)
{
uint8_t b = data[n];
str[n * 2] = hex[b >> 4];
str[n * 2 + 1] = hex[b & 15];
}
}
void hexdump(uint8_t *data, size_t n)
{
char hex[n * 2];
hexstr(data, hex, n);
sys_write(2, hex, n * 2);
}
void putchar(int c)
{
char ch = c;
sys_write(2, &ch, 1);
}
int __lshrsi3(int a, int b)
{
while (b--)
a >>= 1;
return a;
}
int __ashrsi3(int a, int b)
{
if (!(a & 0x8000))
return __lshrsi3(a, b);
while (b--)
a = 0x8000 | (a >> 1);
return a;
}