The PIC series allow direct access to the program counter so you can use it to store the result of a calculation. The RETLW instruction means "RETurn with Literal in W", in other words it is a subroutine return instruction that carries the value you placed in the instruction beck in the W register.
What you need to do is calculate a subroutine address from your 'x' value, call it and return with correct number in W, in other words use it as a look-up table. You call it with 'x' in W and it returns with the 'x'th value in W.
It's very easy to do and very efficient in code usage:
1. put 'x' in the W register
2. call the subroutine (must be a CALL, not a GOTO !)
3. the subroutine contains this:
addwf PCL,f ;add w to the PCL value so the next instruction is W places ahead.
retlw <value 1>
retlw <value 2>
.
.
retlw <value b>
What you are doing is carrying the offset 'x' into the subroutine, there you add it to the present program counter value. Because the program counter holds the address of the next instruction to execute, it jumps ahead 'x' places. At the 'x'th address you place your RETLW instruction which returns back to the main program with the new value in W.
Warning: PCL is an 8-bit register so you have to be careful that when you add 'x' to it, you don't overflow and end jumping to the wrong address. There are tricks to avoid this, the simplest is to place the subroutine at a page boundary so no matter what value you call it with, you never exceed the length of the page. the other is to check for overflow and if necessary, increase the value in PCLATH which is effectively the higher half of the memory address.
Brian.