Matthew-McRaven / Pepp

The next generation of Pep series simulators
https://compsys-pep.com
GNU General Public License v3.0
6 stars 0 forks source link

Change `MOVASP` to `SWAPSPA` #444

Closed Matthew-McRaven closed 7 months ago

Matthew-McRaven commented 7 months ago

Moving A to SP without first saving SP is almost universally a mistake. The value in A can usually be re-created in a handful of instructions. The value of SP tracks the entire run of a program. Failure to first preserve SP will make it nearly impossible to re-enter the OS after main returns.

I drew some inspiration from C++17 for changing a MOV to a SWAP. In C++17 a [[nodiscard]] attribute was added. It warns the user if they don't explicit capture the return value in variable. This can be used to help prevent leaking raw pointers across returns. The programmer may choose to explicitly (void) the return value, but they are required to do something with it.

In a similar way, we could swap A and SP rather than copying from A to SP. The programmer will be required to do something with the old SP value.

I expect this will save ~3 bytes in the OS. The most important benefit is in the instruction set not providing you an unnecessary tool to crash your OS with.

SP/A movement mnemonics today:

Proposed mnemonics:

Mnemonic names not chosen:

We will need to re-arrange the opcode space so that all MOV* instructions are before SWAPSPA.