Automated unit testing is considered a staple of modern software development. Unit testing is hard to do in assembly, because we often don't have the scaffolding needed. Depending on our asm environment, we may or may not have the ability to:
- Call library functions
- Write to stdout or stderr
- Associate messages with test cases
Even incrementing counters for passed and failed tests isn't simple in asm, as we must avoid clobbering registers.
Consequently, when I test asm, I usually use a debugger and inspect it manually (shudder). I may do something like:
cmp rbx, expected_test1
jne .failed_test1
...
.failed_test1:
int3
db `FAIL: Failed test1\0`
If test1 fails, the debugger will stop, and I'll see the bytes "FAIL: Failed test1" in the display. A hack, but easier and less invasive than the other methods I can think of.
A better approach would be:
- Automated: Run and get a count of pass and failed tests
- Diagnostic: Print messages for failed tests
- Non-invasive: Don't clobber regs or mem.
- Minimal assumptions: Don't assume a platform. Often my asm doesn't even load glibc.
Given the above: What's an effective way to unit test asm?