Nios/memenable2

From ASMBits

You have a 4 KB (kilobyte) memory that is mapped to the address range 0xff3000 – 0xff3fff. In reality, this 4 KB memory is built out of 16 256-byte devices. When a memory access to the 4 KB range occurs, only one of the 256-byte devices should be enabled.

Write a function that returns which device should be enabled when an access to the given address occurs. If the access is outside the 4 KB range, return -1. You must not use any add, sub, mul, div, or greater/less-than comparisons or conditional branches (equal or not-equal comparisons and branches are allowed).

  • 0xfff3000 – 0xfff30ff: Return 0
  • 0xfff3100 – 0xfff31ff: Return 1
  • 0xfff3200 – 0xfff32ff: Return 2
  • ...etc
  • 0xfff3f00 – 0xfff3fff: Return 15
  • Outside 0xfff3000 – 0xfff3fff: Return -1
unsigned int memenable(unsigned int address);


Expected solution length: Around 10 lines.

Sample Input

memenable(0xff3100)

Sample Output

r2=1

Write your solution here

Upload a source file...