3

I want to access a C variable in inline assembly, using Microsoft compiler. My code looks like this:

#include <stdio.h>

int nCVar = 1234;

int main()
{
  // scanf("%u", &nCVar);
  _asm
  { 
     mov ebx, nCVar // incorrect
     ror ebx, 10
     mov nCVar, ebx // incorrect
  }
  printf("nCVar is: %u\n", nCVar);
  return 0;
}

My problem are the instructions marked as "incorrect" in the code. I want to have an instruction that loads the value of the variable "nCVar" into the register EBX so EBX has the value 1234 before the "ror" instruction.

Obviously the syntax I used is not correct.

How do I load the value of a C variable into a CPU register?

12
  • 3
    This is a poorly formed question. It is not clear what you are asking and shows no value. Commented Aug 16, 2014 at 3:25
  • @birdinsky: I edited your question so it is better understandable (I hope I didn't change the sense of your question). I have an answer to the question and hope someone can put the answer from "on hold" back to "normal". However the answer is more complex because it differs from compiler to compiler (e.g. Microsoft C behaves different to GNU C). Commented Aug 16, 2014 at 6:22
  • To have the question reopened: it should contain a complete program (not many more lines on top of what there is now), and say just how it does "not work as expected", in addition to how it is expected to work (if that is not clear from code). Commented Aug 16, 2014 at 6:55
  • 1
    @MartinRosenau thanks and as you can see key word _asm, so i used Microsoft Compiler . Commented Aug 16, 2014 at 8:29
  • 3
    @birdinsky "as you can see key word _asm, so i used Microsoft Compiler" Who tells you that other compiler don't have this keyword? Commented Aug 16, 2014 at 15:07

1 Answer 1

1

Some compilers are able to access variables directly in the inline assembler the way you tried to do it:

mov ebx, nCVar

However many compilers simply pass the assembler code to the assembler so there is no interaction with the C program.

In the case of such compilers you cannot access local or "static" variables because the information about the existance of these variables is often lost in the assembler code.

Some compilers with built-in assemblers do not allow accessing symbols in inline assembly. In such cases it would not be possible to access C variables from inline assembly at all.

However if accessing symbols is possible and the variable you want to access is global (not "static") it should be possible to access the variable like you tried to do it. However the symbol name of the variable has to be used.

Under Windows the symbol names of C variables are an underscore followed by the variable name:

mov ebx, _nCVar

However some assemblers would interpret this as "write the address of the variable to the ebx register"; such assemblers would require square brackets to indicate that the variable itself has to be read (instead of the address):

mov ebx, [_nCVar]

Please also note that using assembler it is up to you to care about the data types; the assembler code will not check the correctness of a data type. Using the ebx register with a 16-bit variable for example will lead to unexpected results or even to program crashes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.