字/字节/字 ptr 的汇编正确用法 [英] Assembly correct usage of word/byte/word ptr
问题描述
我正在编写一个用于汇编的小型机器代码生成器.我有一个关于立即内存指令的问题:
I'm writing a little machine code generator for assembly. I have a question regarding immediate to memory instructions:
让我们来看看这 3 个场景:
Let's take these 3 scenarios:
add [ebx+04], 0x1
add [ebx+04], 0x4040
add [ebx+04], 0x401000
我所做的是立即数,我检查它可以容纳的最小字节数是多少,然后我假设它的字节 ptr、word ptr、dword ptr
What I do is going by the immediate constant, I check what is the smallest number of bytes it can fit in then I assume whether its byte ptr, word ptr, dword ptr
第一个可以容纳 1 个字节,所以我假设它是:
First one can fit in 1 byte so I assume it to be:
add byte ptr [ebx+04], 0x1
Second 可以容纳 2 个字节,所以我假设它是:
Second can fit in 2 bytes so I assume it to be:
add word ptr [ebx+04], 0x4040
第三个适合 4 个字节,所以我假设它是:
third fits in 4 bytes so I assume it to be:
add dword ptr [ebx+04], 0x401000
这是正确的吗?
推荐答案
您绝对需要接受此类说明中的尺寸标签.
You most definitely need to accept size tags on these kind of instructions.
但我不会忽略代码来查看哪些数字适合,因为您需要一种方法来决定使用基于符号扩展字节的较小编码的可能性:
But I wouln't dismiss the code to see what numbers fit, because you will need a way to decide on the possibiliy to use a smaller encoding based on sign extending a byte:
add dword ptr [ebx+4], 1
这可以用 4 字节的立即数编码,但更有趣的是用 1 字节的立即数编码.
This can be encoded with a 4 byte immediate, but also more interestingly with a 1 byte immediate.
这篇关于字/字节/字 ptr 的汇编正确用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!