Home > other >  how to load c code variable into assembly code?
how to load c code variable into assembly code?

Time:03-20

I'm a beginner in assembly code, just learning.

in C/C (x64) code there are variables a and variable b and variable c (to accommodate the results of a b), then how to call variables a and b in the assembly so that the addition operation can be occur?

CodePudding user response:

create a separate c function for this job pass your variables and use assembly in that function to add those numbers then return their sum.

Function to add 2 c variables in assembly :

 int add(int a,int b){
    int result;
    __asm{
       mov eax,a         ;eax will contain value of a
       add eax,b         ;eax will contain sum of a and b 
       mov result,eax    ;value of eax is now stored in c   variable result
    };
    return result;
 }

Use this function as:

int a=23;
int b=12;
int c=add(a,b);

CodePudding user response:

extern unsigned int a;
extern unsigned int b;

unsigned int fun ( void )
{
    return(a b);
}

compile to assembly

movl    b(%rip),            
  • Related