简体   繁体   English

该虚拟机如何将指令转换为十六进制值?

[英]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. 我正在学习有关虚拟机的知识,并且偶然发现了Wikipedia上的这本书,这确实很棒。 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 有人可以向我解释一下吗,这是有问题的链接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? 如果您向下滚动到指令代码,他会谈论它,但是对我来说这没有意义,有人可以像我5岁时一样向我解释吗?

The insctruction of loadi r0 #100 becomes a 16-bit instruction. loadi r0 #100指令成为16位指令。

The command loadi sets bits 11 to 15 (bits to the left) to 1: 命令loadi将11到15位(左边的位)设置为1:

0001xxxxxxxxxxxx

r0 is for register 0, and sets bits 8 to 11. r0用于寄存器0,并将位8设置为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. 值100放置在位0到7中。位4到7乘以16,然后与位0到3中的值相加。因此100 = 6乘以16(等于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 来源: http//zh.wikibooks.org/wiki/Creating_a_Virtual_Machine/Register_VM_in_C#Instruction_codes

0x1064 represents a hexidecimal number (base 16). 0x1064代表十六进制数(以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. 您可以使用计算器程序来帮助在十进制(基数10),二进制和十六进制之间进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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