From c40459d55b29b15096cc5750c270f3f8bf0fb95b Mon Sep 17 00:00:00 2001
From: j2-tulloch <james2.tulloch@live.uwe.ac.uk>
Date: Fri, 29 Dec 2023 16:54:15 +0000
Subject: [PATCH] Add new file

---
 Assignment/FiberFooGoo.cpp | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Assignment/FiberFooGoo.cpp

diff --git a/Assignment/FiberFooGoo.cpp b/Assignment/FiberFooGoo.cpp
new file mode 100644
index 0000000..6e0be36
--- /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
+}
-- 
GitLab