What assembly instruction corresponds to a c language local variable assignment like this?

Viewed 38

I'm studying computer architecture and I was thinking about what assembly instruction corresponds to this simple assignment :


int main () {

 int local_test = 10;

}

Considering that the STORE instruction store something from a register to RAM and that the LOAD instruction load something from RAM into a register what is the assembly instruction used for local_test ?

  • I know that it could depend on the CPU, so feel free to give a specific example for a specific machine
1 Answers

Firstly, there isn't any assignment in the code you have shown. = in a declaration indicates an initializer.

Second, since you don't do anything with the value that produces program output, any decent compiler will eliminate it, and no output at all will be produced.

However, suppose you instead of not using the value you used it as the return value of your function. In that case, on most architectures RAM will never be used because function return values are in registers. On ARM, the output would simply be movs r0, 10 to put the value in the return register.

Alternatively suppose you added a line to copy the value to an external variable. The output would still be the same on many architectures, just whatever is necessary to get an immediate value into a register. After that there would be a store instruction to write to the external variable, but that would equate to a later line of code not the one you have asked about.

Related