So, I'm designing my own virtual CPU. I have some registers and memory and can execute some rudimentary instructions. But, now I'm stuck.
How can I differentiate (in my assembled "machine code") between:
LDA $02 ; Load the hex value 0x01 into register A
and
LDA B ; Load the value of B into A
Right now I have encoded the operand ($02
and B
) both as a value of 0x02
. The instruction LDA
is encoded as a single Word (uint16 at this point).
This will obviously give problems. What is the best way to work around this? I think I have the following options:
- Somehow encode into the 16 bits of the instruction that we're dealing with a value or a register (or later a memory location)
- Create different instructions for different operands. E.g.
LOADIA
,LOADRA
,LOADMA
for literals, registers and memory respectively.
IMHO option 1 would be best. Can you confirm 1 is a valid option or provide other methods of handling this problem. Thanks!