在 C 中的 _asm 块中使用局部变量
我正在基于部分汇编和 C 代码使用 16 位 Turbo C++ 3.0 版编写一个程序。我必须在汇编块中使用 C 代码的局部变量。我在 Borland C++ 5 和 MS VC++ 6 中调试了以下代码。它工作正常,但这些编译器是 32 位,因此我必须在 Turbo C 中执行此操作。在 Turbo C++ 中,它显示错误:“操作码和操作数的组合无效”代码是:
void AssignPixel (int X,int Y,int R,int G, int B )
{
_asm {
mov dx, 0x3c8
mov al, 4
out dx, al
inc dx
mov al, R // ERROR
out dx, al
mov al, G // ERROR
out dx, al
mov al, B // ERROR
out dx, al
mov ah , 0x0c
mov al , 4
mov cx , X // ERROR
mov dx , Y // ERROR
int 0x10
}
有没有办法将变量与汇编代码一起使用。代码示例将不胜感激。
I am writing a program in 16 bit Turbo C++ version 3.0 based on partial Assembly and C code. I have to use the local variables of C code in assembly block. I have debug the following code in Borland C++ 5 and MS VC++ 6. It works fine but these compilers are 32 bit therefore I have to do this in Turbo C. In Turbo C++ it displays an Error: "Invalid combination of opcode and oprends" the code is:
void AssignPixel (int X,int Y,int R,int G, int B )
{
_asm {
mov dx, 0x3c8
mov al, 4
out dx, al
inc dx
mov al, R // ERROR
out dx, al
mov al, G // ERROR
out dx, al
mov al, B // ERROR
out dx, al
mov ah , 0x0c
mov al , 4
mov cx , X // ERROR
mov dx , Y // ERROR
int 0x10
}
Is there any way to use Variables with assembly code. Code Sample will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Turbo C 汇编中,变量名代表它们的地址。因此,如果您这样做,
您将把 R 的地址(即
&R
)加载到 ax.但如果你尝试:你会得到一个错误,因为地址是 16 位宽,而
al
寄存器只有 8 位。如果你写:
相当于:
那么你正在将
R
指向的字节加载到al
寄存器中。(
byte ptr
是根据已知操作数的大小自动推导出来的,在本例中为al
)。但请注意,汇编器对变量的类型一无所知,因此您独自一人。例如,在您的代码中,
R
是一个整数(16 位),但最后一行仅加载一个字节。由于您使用的是小端机器,因此您将获得最低有效字节,这相当于强制转换为 char,这可能是您想要的。当然,如果你需要R的整个值,只需写:
相当于:
In Turbo C assembly, variable names represent their addresses. So if you do
you are loading the address of R (that is
&R
) into ax. But if you try:you get an error, because addresses are 16 bits wide, and
al
register is 8 bits only.If you write:
that is equivalent to:
then you are loading the byte pointed to by
R
into theal
register.(The
byte ptr
thing is deduced automatically from the size of the known operand, in this case,al
).But note that the assembler know nothing about the types of your variables, so you are alone on that. For example, in your code,
R
is an integer (16 bits) but that last line loads only one byte. Since you are using a little endian machine, you are getting the least significant byte, that is equivalent to a cast to char, probably what you want.Of course, if you need the whole value of R, just write:
that is equivalent to:
您不能执行这些操作很可能是因为寄存器和变量的大小不匹配(8 位与 16 位):
这里汇编器可能需要不同的语法:
请尝试以下操作:
You can't do these most likely because the sizes of the registers and variables don't match (8-bit vs 16-bit):
And here the assembler probably expects different syntax:
Try these instead:
要在汇编中使用局部变量,您所需要做的就是将代码保留在寄存器中。如果你用完了寄存器,你必须将它移动到内存中的某个地方,然后当你再次使用它时重新加载它。
变量名本质上是变量所在位置的宏,在实现中(出于汇编目的)是寄存器或内存中的槽。华泰
All you need to do to use a local variable in assembly is leave code in a register. If you run out of registers, you have to move it to memory somewhere and then re-load it when you use it again.
A variable name is essentially a macro for where a variable is, which in implementation (for assembly purposes) is either a register or a slot in memory. HTH