Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
16 years ago

Nios II Access General Purpose Registers

I'm new to programming the Nios, and I'm stuck on what should be a simple task.

Is there a way in C, or with inline assembly instructions, to read/write to the 32 general purpose registers?

I've found rdctl/wrctl and rdprs/wrprs. But how, for instance, do I store the address pointed to by the stack pointer into a variable?

Thanks.

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    You don't!

    The compiler uses registers for your variables ...

    --- Quote End ---

    Thanks for the response. So there's no way to save the address pointed to by the stack pointer? I was planning on using the shadow registers (without using the interrupts). How else could you change the value of the stack pointer?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Ah ....

    In that case you probably need to use 'stw' to save sp in memory, switch registers, then 'ldw' the value back - probably via offset from gp (which you want the same in both register sets).

    I don't think there is anywhere (other than memory) the sp value can be saved across the register switch - although I guess a custom instruction could save it somewhere.

    You have bigger problems if you are writing in C though - as gcc will have all sorts of values in registers!

    On he other hand, if you are worried enough about perfromance somewhere to be avoiding register save/restore, I'd have thought you'd want to avoid all stack accesses!

    I've been compiling code in a single source module so that gcc inlines all static function (anything called only once is always inlined), and taken care elsewhere that no locals end up on the stack - so the only stack accesses are saving the callee saved registers on entry (which never get restored because the function doesn't return).

    After specifying -fcall-used-rxx for r16-25,29-31, it is a shame you can't include sp itself! (nor fp or r1).