0

I'm learning about virtual machines and I came across this Wikipedia book thing, and it's really good. However, I'm at the section where the writer is explaining how he converts instructions such as:

loadi r0 #100

to

0x1064

And I have no idea how it works? Can someone please explain this to me, here's the link in question http://en.wikibooks.org/wiki/Creating_a_Virtual_Machine/Register_VM_in_C

If you scroll down to instruction codes he talks about it, but it doesn't make sense to me, can someone explain to me like I'm 5?

Seki
  • 11,135
  • 7
  • 46
  • 70
metro-man
  • 1,763
  • 2
  • 15
  • 28

2 Answers2

1

The insctruction of loadi r0 #100 becomes a 16-bit instruction.

The command loadi sets bits 11 to 15 (bits to the left) to 1:

0001xxxxxxxxxxxx

r0 is for register 0, and sets bits 8 to 11.

00010000xxxxxxxx

The value 100 is placed in bits 0 to 7. Bits 4 to 7 are multiplied by 16, then added to value in bits 0 to 3. So 100 = 6 times 16 (equals 96) + 4.

0001_0000_0110_0100 (in binary, seperations for clarity)
1064 (in hexa)

Source: http://en.wikibooks.org/wiki/Creating_a_Virtual_Machine/Register_VM_in_C#Instruction_codes

AntonH
  • 6,359
  • 2
  • 30
  • 40
  • FYI, this would be 0xF064. – R Dub Jul 16 '14 at 04:21
  • @RDub I noticed, was correcting when you made comment. – AntonH Jul 16 '14 at 04:22
  • May I ask, how does he encode `add r2 r0 r1` since there are 3 'parameters' this time – metro-man Jul 16 '14 at 04:49
  • @user3839220 Format for `add` seems to be `add target source1 source2`. Code for `add` is 2, the rest are numbers encoded on 4 bits. So: `add r2 r0 r1` in binary is `0010 0010 0000 0001`, or `2 2 0 1` – AntonH Jul 16 '14 at 04:53
  • That makes sense, I was confused since it said for the immediate value it was bits 0-7, whereas there is no immediate value in this case. Thanks a ton :) – metro-man Jul 16 '14 at 04:58
  • @user3839220 "Intermediate value" was for "loadi". Other commands have other information for bits 0 to 11. – AntonH Jul 16 '14 at 05:06
1

0x1064 represents a hexidecimal number (base 16).

This number represents multiple pieces of information:
bits 15-12 = 0001 (loadi)
bits 11- 8 = 0000 (register)
bits  7- 0 = 0110 0100 (value to load)

So in binary (base2) the instruction is 0001 0000 0110 0100.
Converting to hex (base 64) the instruction is 0x1064.

You can use a calculator program to help converting between decimal (base 10), binary, and hex.

R Dub
  • 678
  • 6
  • 23