0

How is the neg pseudo instruction implemented with only one sub?

I don't understand, as neg is R[rd] = -R[rs1]. But if I have sub, it is R[rs1] - something.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
RUI WANG
  • 3
  • 2

1 Answers1

1

The "something" in this case is the zero register. but you're not subtracting that from the register, you're subtracting the register from that.

The:

neg rd, rs

pseudo-instruction is meant to put the negation of rs into rd. The

sub rd, zero, rs

instruction subtracts rs from zero, placing the result into rd.

rd := -rs        ; example: -(42)  -> -42
rd := 0 - rs     ;          0 - 42 -> -42

Since -x is the same as 0 - x, they are equivalent.


If you want a more comprehensive list of pseudo instructions and what they map to, here an image which details some, including the specific one you asked about:

enter image description here

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953