diff --git a/Assignment/FiberFooGoo.cpp b/Assignment/FiberFooGoo.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e0be36f788c2220460d01a436f799529d5d733a --- /dev/null +++ b/Assignment/FiberFooGoo.cpp @@ -0,0 +1,38 @@ +#include <iostream> +#include <array> +#include "context.h" + +void foo() { + std::cout << "you called foo" << std::endl; + exit(0); // Exit to prevent returning to a different stack +} + +void goo() { + std::cout << "you called goo" << std::endl; + exit(0); +} + +int main() { + // Allocate space for stacks + std::array<char, 4096> stackFoo, stackGoo; + + // Set up stack pointers, aligned to 16 bytes and accounting for the Red Zone + char* spFoo = reinterpret_cast<char*>((reinterpret_cast<uintptr_t>(stackFoo.data() + 4096) & -16L) - 128); + char* spGoo = reinterpret_cast<char*>((reinterpret_cast<uintptr_t>(stackGoo.data() + 4096) & -16L) - 128); + + Context cFoo, cGoo; + + // Set up contexts + get_context(&cFoo); + cFoo.rip = reinterpret_cast<void*>(&foo); + cFoo.rsp = spFoo; + + get_context(&cGoo); + cGoo.rip = reinterpret_cast<void*>(&goo); + cGoo.rsp = spGoo; + + // Use set_context to switch to either foo or goo + set_context(&cFoo); // This will transfer control to foo + + return 0; // Return an integer from main +}