6

I am trying to load mstatus with another register t1.

 lw t1, mstatus              # load mstatys register into t1
 xori t1, t1, 0x8            # xor mstatus to set 3rd bit and leave everything else as is
 lw mstatus, t1              # set mstatus 

The initial lw t1, mstatus works just fine. However when trying to lw mstatus, t1 the assembler gives

Error: illegal operands 'lw mstatus, t1'

I have no idea what causes this error, mstatus register is a read/write register. It should work.

3
  • addi mstatus, t1, 0 brings the same error Commented Dec 30, 2019 at 1:50
  • 1
    try sw t1, mstatus Commented Dec 30, 2019 at 1:56
  • Are you trying to set 3rd bit (as the comment says), or to toggle it? Because ori would be the correct instruction for setting (xori would be for toggling, so its use is inconsistent with the comment). Commented Dec 30, 2019 at 16:51

2 Answers 2

7

mstatus is not a memory part. Then it can't be loaded/stored with lw/sw instructions under general purpose registers (x1-x31).

mstatus is part of CSR (Control Status Registers) that been accessed with Control and Status Register Instruction (see chapter 2.8 of riscv-spec).

Then to load mstatus you should use csrrs/c instruction and to write csrrw instruction depending of what you want to do you can also just clear/set individual bit of register.

Write t1 in mstatus and don't care of mstatus old value (x0):

csrrw t1, mstatus, x0

Read mstatus in t1 and don't touch mstatus value:

csrrs x0, mstatus, t1

or

csrrc x0, mstatus, t1

Sign up to request clarification or add additional context in comments.

Comments

5

In addition to @FabienM‘s answer, I would add a reference to the pseudo instructions for handling CSRs. E.g. csrr rd, csr which is short for csrrs rd, csr, x0 and simply reads the given CSR. Those can be found in chapter 9.1 "CSR Instructions", of the The RISC-V Instruction Set Manual Volume I: Unprivileged ISA.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.