-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.c
More file actions
84 lines (70 loc) · 1.57 KB
/
common.c
File metadata and controls
84 lines (70 loc) · 1.57 KB
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
74
75
76
77
78
79
80
81
82
83
#if CHAPTER >= 3
// common.c -- Defines some global functions.
// From JamesM's kernel development tutorials.
#include "common.h"
// Write a byte out to the specified port.
void outb(uint16_t port, uint8_t value)
{
asm volatile ("outb %1, %0" : : "dN" (port), "a" (value));
}
uint8_t inb(uint16_t port)
{
uint8_t ret;
asm volatile("inb %1, %0" : "=a" (ret) : "dN" (port));
return ret;
}
uint16_t inw(uint16_t port)
{
uint16_t ret;
asm volatile ("inw %1, %0" : "=a" (ret) : "dN" (port));
return ret;
}
// Copy len bytes from src to dest.
void memcpy(uint8_t *dest, const uint8_t *src, uint32_t len)
{
for(; len != 0; len--) *dest++ = *src++;
}
// Write len copies of val into dest.
void memset(uint8_t *dest, uint8_t val, uint32_t len)
{
for ( ; len != 0; len--) *dest++ = val;
}
// Compare two strings. Should return -1 if
// str1 < str2, 0 if they are equal or 1 otherwise.
int strcmp(char *str1, char *str2)
{
while (*str1 && *str2 && (*str1++ == *str2++))
;
if (*str1 == '\0' && *str2 == '\0')
return 0;
if (*str1 == '\0')
return -1;
else return 1;
}
// Copy the NULL-terminated string src into dest, and
// return dest.
char *strcpy(char *dest, const char *src)
{
while (*src)
*dest++ = *src++;
*dest = '\0';
}
// Concatenate the NULL-terminated string src onto
// the end of dest, and return dest.
char *strcat(char *dest, const char *src)
{
while (*dest)
*dest = *dest++;
while (*src)
*dest++ = *src++;
*dest = '\0';
return dest;
}
int strlen(char *src)
{
int i = 0;
while (*src++)
i++;
return i;
}
#endif