在 C 中的 _asm 块中使用局部变量

发布于 2024-12-07 23:16:17 字数 567 浏览 0 评论 0原文

我正在基于部分汇编和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别挽留 2024-12-14 23:16:17

在 Turbo C 汇编中,变量名代表它们的地址。因此,如果您这样做,

mov ax, R

您将把 R 的地址(即 &R)加载到 ax.但如果你尝试:

mov al, R

你会得到一个错误,因为地址是 16 位宽,而 al 寄存器只有 8 位。

如果你写:

mov al, [R]

相当于:

mov al, byte ptr [R]

那么你正在将 R 指向的字节加载到 al 寄存器中。
byte ptr 是根据已知操作数的大小自动推导出来的,在本例中为 al)。

但请注意,汇编器对变量的类型一无所知,因此您独自一人。例如,在您的代码中,R 是一个整数(16 位),但最后一行仅加载一个字节。由于您使用的是小端机器,因此您将获得最低有效字节,这相当于强制转换为 char,这可能是您想要的。

当然,如果你需要R的整个值,只需写:

mov ax, [R]

相当于:

mov ax, word ptr [R]

In Turbo C assembly, variable names represent their addresses. So if you do

mov ax, R

you are loading the address of R (that is &R) into ax. But if you try:

mov al, R

you get an error, because addresses are 16 bits wide, and al register is 8 bits only.

If you write:

mov al, [R]

that is equivalent to:

mov al, byte ptr [R]

then you are loading the byte pointed to by R into the al 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:

mov ax, [R]

that is equivalent to:

mov ax, word ptr [R]
雪化雨蝶 2024-12-14 23:16:17

您不能执行这些操作很可能是因为寄存器和变量的大小不匹配(8 位与 16 位):

mov al, R
mov al, G
mov al, B

这里汇编器可能需要不同的语法:

mov cx, X
mov dx, Y

请尝试以下操作:

mov al, byte ptr [R] ; I don't remember if the brackets are required here
mov cx, [X]

You can't do these most likely because the sizes of the registers and variables don't match (8-bit vs 16-bit):

mov al, R
mov al, G
mov al, B

And here the assembler probably expects different syntax:

mov cx, X
mov dx, Y

Try these instead:

mov al, byte ptr [R] ; I don't remember if the brackets are required here
mov cx, [X]
笑咖 2024-12-14 23:16:17

要在汇编中使用局部变量,您所需要做的就是将代码保留在寄存器中。如果你用完了寄存器,你必须将它移动到内存中的某个地方,然后当你再次使用它时重新加载它。

变量名本质上是变量所在位置的宏,在实现中(出于汇编目的)是寄存器或内存中的槽。华泰

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文