2

What does not mean in RiscV? I got it after translating some code from C like this:

not     a0, t1
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • Google gives no results but xori, I don't understand what is this.. –  Jan 08 '21 at 14:03
  • can I replace it with more simple commands? –  Jan 08 '21 at 14:05
  • https://www.electronics-tutorials.ws/logic/logic_4.html for each bit the zeros become ones and ones become zeros. https://en.wikipedia.org/wiki/XOR_gate xor is an overkill but if you compare if one operand is a 1 then the other inverts or is notted. See truth tables on both pages (if the links continue to work). – old_timer Jan 08 '21 at 16:38

2 Answers2

4

not is not part of the instruction set but some assemblers may use it as a shorthand. It compiles as xori a0, t1, -1 which means it will invert every bit in the register and store the result in the target.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
1

Sometimes you will see neg and not instructions but you can implement them with sub and xor using extra operands(/registers) so sometimes you do not.

not is like xor, and, or, etc and are bitwise operands so bit 0 is operated with bit 0 of the other operand to result in bit 0 of the result. Not is an instruction that if implemented logic would be done with one operand (bit 0 of the only operand is operated on to get bit 0 of the result).

The truth table for not is just a inversion

a  q
0  1
1  0

neg is a negative, and is not the same as not. It is not a bitwise operation as negative 0 = - (or just 0 bit wise) 0 and negative 1 = -1 (or just one bitwise) it has no effect on the result if considered bitwise. You can also see this with twos copmplement to do the negation. Invert and add one. Bitwise negative 0 = 1+1 = 0, bitwise negative 1 = 0+1 = 1. You can simply do a subtract with result = 0 - operand, to perform a (non-bitwise) neg. So you will not always see a neg instruction.

xor's truth table is essentially a parity check, if one and not both are true then true

a b  q
0 0  0
0 1  1
1 0  1
1 1  0

now anything xored with 0 is itself:

a b  q
0 0  0
1 0  1

and anything xored with 1 is inverted

a b  q
0 1  1
1 1  0

And that is the same as a not operation from an operand a and result perspective, so you will often see xor with all ones instead of a not instruction. In this case if the tool supports it (assembly is defined by the tool (gnu assembler, or jimmy's assembler or sue's assembler), not the target (risc-v)) it appears to be a pseudo instruction.

ARM for example has real neg (negate) and mvn (move not) instructions.

not is a bitwise inversion, each output bit is inverted from the input.

xor is a a bitwise operation where if exactly one of the two bits of the operands are 1 then the matching output bit is a 1 otherwise 0. For a multi-input xor it is the equivalent of adding the operands and taking the lsbit, it gives you the odd parity odd number of bits are set you get 1 even number (zero is an even number) then zero. (which is also true for a two input)

old_timer
  • 69,149
  • 8
  • 89
  • 168