An example we can build upon
This is a short example where we will create our own stack and make our CPU return out of its current execution context and over to the stack we just created. We will build on these concepts in the following chapters.
Setting up our project
First, let’s start a new project by creating a folder named a-stack-swap
. Enter the new folder and run the following:
cargo init
Tip
You can also navigate to the folder called ch05/a-stack-swap
in the accompanying repository and see the whole example there.
In our main.rs
, we start by importing the asm!
macro:
ch05/a-stack-swap/src/main.rs
use core::arch::asm;
Let’s set a small stack size of only 48 bytes here so that we can print the stack and look at it before we switch contexts after we get the first example to work:
const SSIZE: isize = 48;
Note
There seems to be an issue in macOS using such a small stack. The minimum for this code to run is a stack size of 624 bytes...