Electrical – Assembly code, what does it do

assemblyrisc-v

I'm interested to know what this assembly code do knowing that X1 is full of zeroes.

ori  X2, X0, 0xFFF
slli X2, X2, 12
ori  X2, X2, 0xFFF
slli X2, X2, 8
ori  X2, X2, 0xFF
xor  X2, X2, X1
addi X2, X2, 1
and  X2, X2, X1

The problem is that I don't understand what 0xFFF and 0xFF are. How am I supposed to know their values at all?

Best Answer

These look like instructions for a RISC-V microprocessor.
https://riscv.org/wp-content/uploads/2017/05/riscv-spec-v2.2.pdf

  • ori is the "OR Immediate" instruction.
  • slli is a "Left Shift Logical Immediate" instruction.
  • xor is an "Exclusive Or" instruction.
  • addi is an "Add Immediate" Instruction.
  • and is a "Bitwise AND instruction".

The problem is that I can't understand what are those 0xFFF and 0xFF how am I supposed to know their values at all?

These are integer literal values written in hexadecimal. The value shown literally is their value. We can guess based on the fact that these are all literal type instructions that the numeric operands are literals rather than a memory address.

  • 0xFF is 11111111 in binary or 255 in decimal.
  • 0xFFF is 111111111111 in binary or 4095 in decimal.

Writing the numbers in hexadecimal improves the readability of the code. Especially because the code contains several bit-wise instructions.

Note that the X0 register is a hardwired zero per page 109 of the "RISC-V User-Level ISA V2.2".

ORI  X2, X0, 0xFFF //X2 <= 1111 1111 1111 1111 1111 1111 1111 1111
SLLI X2, X2, 12    //X2 <= 1111 1111 1111 1111 1111 0000 0000 0000
ORI  X2, X2, 0xFFF //X2 <= 1111 1111 1111 1111 1111 1111 1111 1111
SLLI X2, X2, 8     //X2 <= 1111 1111 1111 1111 1111 1111 0000 0000
ORI  X2, X2, 0xFF  //X2 <= 1111 1111 1111 1111 1111 1111 1111 1111
XOR  X2, X2, X1    //X2 <= 1111 1111 1111 1111 1111 1111 1111 1111
                   //      since it was given that X1 is all 0s.
ADDI X2, X2, 1     //X2 <= 0000 0000 0000 0000 0000 0000 0000 0000
AND  X2, X2, X1    //X2 <= 0000 0000 0000 0000 0000 0000 0000 0000
                   // since X1 was all 0s, the AND operation would
                   // clear X2 no matter what its prior value.