Elizabeth
Engineer
Engineer
  • UID625
  • Fans4
  • Follows1
  • Posts68
Reads:1997Replies:0

[Share]Use Add Command to Implement Addition Operations for Memory

Created#
More Posted time:Sep 20, 2016 15:32 PM
I always thought that a simple i++ operation can change into three assembly commands:
1. Load memory content to the register
2. Implement an addition operation for the register
3. Write back to the memory
But actually, memory content can be the direct operation object of add commands. Here are relevant instructions:http://x86.renejeschke.de/html/file_module_x86_id_5.html
The sample code is as follows:
int count = 20;

    asm(
        "addl $80, %0 \n"
        : : "m" (count) :);

    printf("%d\n", count);


The results after assembly:
movl    $20, -4(%rbp)
    addl    $80, -4(%rbp)


movl $20, -4(%rbp) is to assign a value to count, and addl is to add 80 to it.
According to the writing of loading to the register before the operation:
asm(
        "addl $80, %0 \n"
        : "+r"(count) : :);
    printf("%d\n", count);


After assembly:
movl    -4(%rbp), %eax
    addl    $80, %eax
    movl    %eax, -4(%rbp)


This is the common form of three commands.
Guest