This project is a custom implementation of several standard C library functions written entirely in x86-64 assembly (NASM). The functions are compiled into a dynamic shared library (.so) that can be loaded and used in C programs.
The following standard library functions have been re-implemented:
strlenstrchrstrrchrstrcmpstrncmpstrcasecmpstrstrstrpbrkstrcspnmemsetmemcpymemmove
To build and test the library, you will need the following tools:
makenasm(Netwide Assembler)gcc(GNU Compiler Collection)criterion(for running the unit tests)
You can use the provided Makefile to build the shared library libasm.so.
-
Build the library:
make
-
Clean object files:
make clean
-
Clean all generated files (library and objects):
make fclean
-
Rebuild the library from scratch:
make re
The repository includes a comprehensive test suite using the Criterion framework. The tests compare the behavior of each custom assembly function against its original C standard library counterpart.
- To compile and run the unit tests:
make test_run
You can use the generated libasm.so in your own C projects by dynamically loading it.
Here is an example of how to use the custom strlen function from this library.
-
First, build the library:
make
-
Create a C file, for example,
main.c:#include <stdio.h> #include <dlfcn.h> #include <stdlib.h> int main(void) { void *handle; size_t (*my_strlen)(const char *); char *error; // Load the shared library handle = dlopen("./libasm.so", RTLD_LAZY); if (!handle) { fprintf(stderr, "Error loading library: %s\n", dlerror()); exit(EXIT_FAILURE); } // Clear any existing error dlerror(); // Get a pointer to the custom strlen function *(void **)(&my_strlen) = dlsym(handle, "strlen"); error = dlerror(); if (error != NULL) { fprintf(stderr, "Error finding function: %s\n", error); exit(EXIT_FAILURE); } // Use the function const char *str = "Hello from MiniLibC!"; printf("String: \"%s\"\n", str); printf("Length (using my_strlen): %zu\n", my_strlen(str)); // Unload the library dlclose(handle); return 0; }
-
Compile your C program, linking against the dynamic linking library (
-ldl):gcc -o my_program main.c -ldl
-
Run your executable. Ensure
libasm.sois in the same directory or in yourLD_LIBRARY_PATH../my_program
Output:
String: "Hello from MiniLibC!" Length (using my_strlen): 20