我正在尝试使用C变量在C代码中使用汇编.我的代码看起来像这样:
__asm { INT interruptValue };
Run Code Online (Sandbox Code Playgroud)
其中'interruptValue'是我从用户那里获得的变量(例如15或15h).当我尝试编译时,我得到:
汇编程序错误:'无效的指令操作数'
我不知道interruptValue的正确类型是什么.我尝试了长\ int\short\char\char*,但没有一个工作.
const int howmany = 5046;
char buffer[howmany];
asm("lea buffer,%esi"); //Get the address of buffer
asm("mov howmany,%ebx"); //Set the loop number
asm("buf_loop:"); //Lable for beginning of loop
asm("movb (%esi),%al"); //Copy buffer[x] to al
asm("inc %esi"); //Increment buffer address
asm("dec %ebx"); //Decrement loop count
asm("jnz buf_loop"); //jump to buf_loop if(ebx>0)
Run Code Online (Sandbox Code Playgroud)
我正在使用gcc编译器.出于某种原因,我的缓冲区/ howmany变量在我的asm眼中是不确定的.我不知道为什么.我只想将缓冲区数组的起始地址移动到esi寄存器中,在将每个元素复制到al寄存器时循环"howmany"次.
问题:如何从非POD类中访问程序集中的成员变量?
阐述:
我已经为类成员函数编写了一些内联汇编代码,但是我想到的是如何访问类成员变量.我尝试过offsetof宏,但这是一个非POD类.
我正在使用的当前解决方案是将指向全局范围的指针分配给成员变量,但这是一个混乱的解决方案,我希望有更好的东西,我不知道.
注意:我正在使用G ++编译器.使用英特尔语法Asm的解决方案会很好但我会采取任何措施.
我想做的例子(英特尔语法):
class SomeClass
{
int* var_j;
void set4(void)
{
asm("mov var_j, 4"); // sets pointer SomeClass::var_j to address "4"
}
};
Run Code Online (Sandbox Code Playgroud)
目前的hackish解决方案:
int* global_j;
class SomeClass
{
int* var_j;
void set4(void)
{
asm("mov global_j, 4"); // sets pointer global_j to address "4"
var_j = global_j; // copy it back to member variable :(
}
};
Run Code Online (Sandbox Code Playgroud)
这些是粗略的例子,但我认为他们得到了重点.
我想从128位寄存器中提取值(第一个字16bits),我得到了这个命令,但这不起作用.设置a的值后会有一些算术运算,比变量里面会有一些算术运算最后会改变我想要提取第一个字......我怎么能这样做...
int r;
int inm=0;
__m128i a=_mm_setr_epi16(8,9,3,2,4,5,6,11);
_asm{
r = _mm_extract_epi16(a,inm);
}
Run Code Online (Sandbox Code Playgroud) 我试图通过youtube上发布的一系列非常好的教程来学习汇编:
我熟悉netbeans中的C++和Java编程,我正在使用MinGW编译器集.我在netbeans编译器属性中设置了我的C++和汇编编译器.
我的C++代码正在编译,但是使用_asm {}来尝试内联汇编代码并不能正确编译.
我收到的错误是:
main.cpp: In function 'int getValueFromASM()':
main.cpp:18:5: error: '_asm' was not declared in this scope
main.cpp:18:10: error: expected ';' before '{' token
make[2]: *** [build/Debug/MinGW_1-Windows/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
Run Code Online (Sandbox Code Playgroud)
代码是:
#include <cstdlib>
#include <iostream>
using namespace std;
int getValueFromASM()
{
_asm {
mov eax, 39
}
}
int main(int argc, char** argv) {
cout << "Hello World from C++ !\n";
cout << "ASM said " …Run Code Online (Sandbox Code Playgroud) 我正在编写一个内核模块,它是关于读写MSR的.我写了一个简单的测试程序,但它仍然失败.它所做的只是写入MSR,然后再读回来.这是代码:
static int __init test3_init(void)
{
uint32_t hi,lo;
hi=0; lo=0xb;
asm volatile("mov %0,%%eax"::"r"(lo));
asm volatile("mov %0,%%edx"::"r"(hi));
asm volatile("mov $0x38d,%ecx");
asm volatile("wrmsr");
printk("exit_write: hi=%08x lo=%08x\n",hi,lo);
asm volatile("mov $0x38d,%ecx");
asm volatile("rdmsr":"=a"(lo),"=d"(hi));
printk("exit_write2: hi=%08x lo=%08x\n",hi,lo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出如下:
exit_write: hi=00000000 lo=0000000b
exit_write2: hi=00000000 lo=00000000
有人能告诉我为什么第二个输出中的返回值为0,而不是原始值?我的代码有问题吗?非常感谢.
非常自我解释的代码.为什么不起作用!
#include <stdio.h>
int main() {
__asm__("number dw 0"); // declare number?
printf("%d",number);
__asm__("mov %eax,number"
"inc %eax"
"mov number,%eax");
printf("%d",number);
return 0;
}
cc ex1.c -o ex1
ex1.c: In function ‘main’:
ex1.c:22:17: error: ‘number’ undeclared (first use in this function)
ex1.c:22:17: note: each undeclared identifier is reported only once for each function it appears in
make: *** [ex1] Error 1
Run Code Online (Sandbox Code Playgroud)
谢谢.
我有很多知识空白要填补... gcc手册让我对内联汇编问题感到困惑,谷歌搜索教程结果......
在intel i7处理器上工作
我有这个功能,主要由内联asm组成.
long *toarrayl(int members, ...){
__asm{
push esp
mov eax, members
imul eax, 4
push eax
call malloc
mov edx, eax
mov edi, eax
xor ecx, ecx
xor esi, esi
loopx:
cmp ecx, members
je done
mov esi, 4
imul esi, ecx
add esi, ebp
mov eax, [esi+0xC]
mov [edi], eax
inc ecx
add edi, 4
jmp loopx
done:
mov eax, edx
pop esp
ret
}
}
Run Code Online (Sandbox Code Playgroud)
运行时,我在返回指令上遇到访问冲突.
我正在使用VC++ 6,它有时可能意味着指向上面的行,所以可能在'pop esp'上.如果你能帮助我,那就太好了.谢谢,iDomo.
//quick inline asm statements performing the swap_byte for key_scheduling
inline void swap_byte(unsigned char *x, unsigned char *y)
{
unsigned char t;
asm("movl %1, %%eax;"
"movl %%eax, %0;"
:"=r"(t)
:"r"(*x)
:"%eax");
asm("movl %1, %%eax;"
"movl %%eax, %0;"
:"=r"(*x)
:"r"(*y)
:"%eax");
asm("movl %1, %%eax;"
"movl %%eax, %0;"
:"=r"(*y)
:"r"(t)
:"%eax");
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想从交换焦炭x和存储y,与同为y至x.我已经通过更改movl来编译这些指令mov但没有成功.编译/链接的问题在哪里?
这是cygwin中编译的输出:
$ gcc rc4_main.c -o rc4ex
/tmp/ccy0wo6H.s: Assembler messages:
/tmp/ccy0wo6H.s:18: Error: operand type mismatch for `mov'
/tmp/ccy0wo6H.s:18: Error: …Run Code Online (Sandbox Code Playgroud) 我想在我的C程序中预取某些地址(这是大型数组的某些元素的地址),并看到它对时间的影响.
关于PREFETCH的指令我在这里找到了PREFETCH0.但我不知道如何使用内联汇编在C中使用它.如果某个机构能够在C程序中如何使用该指令和地址作为参数,那将是非常有帮助的.