Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Im am quite new to OSD, although I have developed with c before. I need to know how one would code functions such as memcpy, memset, memsetw, strlen. I have gone as far as this:
Don't expect to get too many replies giving you code, but I'll give you a simple, very basic implementation of strlen. If you've developed with C before, you should be able to take it from there.
you should find any of them in a good book about C programming (e.g. the Kernighan & Ritchie).
You could also use bits of your brain and come with
unsigned strlen (const char* str)
{
// hmm. let's see how far from str we can go before we encounter
// a nul character:
unsigned len=0;
while (*str!=0) {
len++;
str++;
}
return len;
}
or you could head yourself to the FAQ, get the all-in-one introduction page, and discover that we're indeed missing a fast way to find a free implementation of that d*mn stuff everyone needs.
So i suggest you the OSLib if what's above don't fit you.
unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count)
{
/* the code here is to copy 'count' bytes of data from 'src' to
* 'dest', finally return 'dest' */
}
unsigned char *memset(unsigned char *dest, unsigned char val, int count)
{
/* the code here is to set 'count' bytes in 'dest' to 'val'.
* Again, return 'dest' */
}
unsigned short *memsetw(unsigned short *dest, unsigned short val, int count)
{
/* Same as above, but this time, im working with a 16-bit
* 'val' and dest pointer.*/
}
int strlen(const char *str)
{
/* This needs to loop through character array 'str', returning how
* many characters it needs to check before it finds a 0.
* In simple words, it returns the length in bytes of a string */
}
and thanks, i can definintly use these examples to craft my code.
basically, those functions are
- either built using inline assembly and dedicated opcode such as 'stosb', 'rep movsd', etc.
- or built using bare stuff such as "*src=*dst" and pointers arithmetics.
Oh, and may i suggest you thoroughly test them with the debugger of your choice in user-land of your host OS before integrating that code into the kernel ?
or did i *again* completely miss the point of your question ?
char c=*src; // reads character at address <src> into <c>
src++; // advance <src> to the next character's address
*dst=c; // writes character <c> at address <dst>
and indeed, this is _much_ a C problem so rushing to the library and borrowing "The C programming Language, 2nd edition" by Brian W. Kernighan and Denis M. Ritchie will probably leaves huge amount of mysteries around it.
Dear friend, no offence what so ever, but shouldn't you tighten your C skills before trying to write an OS?
Asking how to implement memcpy or strlen indicates that you've hardly done any C code at all, not knowing such elementary things how would you go and write an operating system? Don't you think that with such lack of knowledge its a little bit hmmm.. ambitious?
Writing an operating system requires you to master the programming language you write it in, and since you've chosen C I would strongly suggest you grab a C book and go knock down some printf("Hello world\n") programs.
had to studie memory system crap... and i found my mistake...
i think this question has been answerd, and i will no longer view the thread, if you have any other scornfull suggestions, please, send them to me. :)
OS dev also requires thorough knowledge of assembly, even if you don't intend to write the main portion of your OS in assembly.
0x00100000 is one byte and 0x00100001 a whole different byte?
You can address locations as bytes, words, or dwords. A word is 2 bytes, a dword 4 bytes. So if you access 0x00100000 as a word, then you automatically access 0x00100001 also (they are one word, but two separate bytes). Accessing 0x00100000 as a dword results in actually accessing 0x00100000, 0x00100001, 0x00100002, 0x00100003. So yes, the memory address is atomic as a byte, but depending on how you access an address, several other addresses are accessed simultaneously, since they're all stored together.