I already asked similar question here, but I still get some errors, so I hope you could tell me what am I doing wrong. Just know that I know assembler, and I have done several projects in 8051 assembler, and even it is not the same, is close to x86 asm.
There is block of code I tried in VC++ 2010 Express (I am trying to get information from CPUID instruction): `
int main()
{
char a[17]; //containing array for the CPUID string
a[16] = '\0'; //null termination for the std::cout
void *b=&a[0];
int c=0; //predefined value which need to be loaded into eax before cpuid
_asm
{
mov eax,c;
cpuid;
mov [b],eax;
mov [b+4],ebx;
mov [b+8],ecx;
mov [b+12],edx;
}
std::cout<<a;
}`
So, to quick summarize, I tried to create void pointer to the first element of array, and than using indirect addressing just move values from registers. But this approach gives me “stack around b variable is corrupted run-time error” but I don´t know why.
Please help. Thanks. And this is just for study purposes, i know there are functions for CPUID….
EDIT: Also, how can you use direct addressing in x86 VC++ 2010 inline assembler? I mean common syntax for immediate number load in 8051 is mov src,#number but in VC++ asm its mov dest,number without # sign. So how to tell the compiler you want to access memory cell adress x directly?
The reason your stack is corrupted is because you’re storing the value of
eaxinb. Then storing the value ofebxat the memory locationb+4, etc. The inline assembler syntax[b+4]is equivalent to the C++ expression&(b+4), ifbwere a byte pointer.You can see this if you watch
band single-step. As soon as you executemov [b],eax, the value ofbchanges.One way to fix the problem is to load the value of
binto an index register and use indexed addressing:You don’t really need
bat all, to hold a pointer toa. You can load the index register directly with thelea(load effective address) instruction:If you’re fiddling with inline assembler, it’s a good idea to open the Registers window in the debugger and watch how things change when you’re single stepping.
You can also directly address the memory:
However, direct addressing like that required more code bytes than the indexed addressing in the previous example.
I think the above, with the
lea(Load Effective Address) instruction and the direct addressing I showed answers your final question.