Peer review of inline assembly
Chris Mahoney (1684) 2165 posts |
I’ve had to call a couple of SWIs from assembly, and not knowing assembly I’ve basically just copied an example or two and worked them backwards. While the code below does run on my machine, I don’t know what some parts of it are doing (eg. MOVVC) so can someone please take a look and comment on whether it’s “right”?
|
Rick Murray (539) 13840 posts |
Functions return error if something went wrong. MOVVC sets R0 to null if V flag is clear, meaning the reply is error block or null. Do you need the # before the numbers with inline assembly? |
Chris Mahoney (1684) 2165 posts |
Apparently not; I just removed them and it still works. |
Jeffrey Lee (213) 6048 posts |
The three {}’s after SWI (and BL / BLX, if you need to call assembler functions) are the input registers, output registers, and clobber list. Since SWIs can return errors in R0, you’ll want both R0 and the PSR in the output list (unless you’re ignoring the errors, in which case you can put them in the clobber list). LR is only required in the clobber list if you’re calling the SWI from SVC mode, so if this is a plain user mode app you can omit it.
This can be simplified to XOS_ReadUnsigned (AFAIK swis.h should contain #defines for the X and non-X forms of each SWI) You’ll want R1 in the clobber list. For maximum safety, R3 and R4 as well (or pass 0 to R4 on entry), since R4 is now used as a magic value to select 64bit output. https://www.riscosopen.org/wiki/documentation/show/OS_ReadUnsigned
Remember my previous comment about making sure the OS_NVMemory arguments are sane :-) Setting R1 to point to an empty string should be sufficient. |
Chris Mahoney (1684) 2165 posts |
Thanks; I’ll make some tweaks. It currently returns the expected errors even without R0 in the output list but who knows whether it’ll work on all systems. I prefer to do things “properly” when I can so I’ll certainly update it :) |