简体   繁体   中英

How does this virtual machine convert instructions to hexadecimal values?

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM