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:
MOVASP
MOVSPA
Proposed mnemonics:
MOVSPA
SWAPSPA
Mnemonic names not chosen:
XCHG*, EX* style mnemonics not used because the X in the name may be confusing
SWPSPA not used because we have 7 chars for mnemonics, and SWAPSPA sounds nicer.
SWAPASP not used to keep parity with MOVSPA.
We will need to re-arrange the opcode space so that all MOV* instructions are before SWAPSPA.
Moving
A
toSP
without first savingSP
is almost universally a mistake. The value inA
can usually be re-created in a handful of instructions. The value ofSP
tracks the entire run of a program. Failure to first preserveSP
will make it nearly impossible to re-enter the OS after main returns.I drew some inspiration from
C++17
for changing aMOV
to aSWAP
. InC++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
andSP
rather than copying fromA
toSP
. 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:
MOVASP
MOVSPA
Proposed mnemonics:
MOVSPA
SWAPSPA
Mnemonic names not chosen:
XCHG*
,EX*
style mnemonics not used because the X in the name may be confusingSWPSPA
not used because we have 7 chars for mnemonics, andSWAPSPA
sounds nicer.SWAPASP
not used to keep parity withMOVSPA
.We will need to re-arrange the opcode space so that all
MOV*
instructions are beforeSWAPSPA
.