In some specific uses, thei++
It may be possible to store the original value in an intermediate amount to be used, see below the assembly code of the program in question (using thegcc
)。
-
i++
Source program:
#include <>
int main(){
int i = 1;
printf("%d\n", i++);
return 0;
}
-
i++
Compilation:
Main:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $1, -4(%rbp) //commander-in-chief (military)iassign1.
movl -4(%rbp), %eax //commander-in-chief (military)iThe original value of is transferred to the intermediate variable
leal 1(%rax), %edx //commander-in-chief (military)%edxassigni+1
movl %edx, -4(%rbp)//iA change in the value of
movl %eax, %esi //verticalizationiThe print operation is performed on the value of the
leaq .LC0(%rip), %rax
movq %rax, %rdi
movl $0, %eax
call printf@PLT
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
Look at the same operation again++i
-
++i
of the source program:
#include <>
int main(){
int i = 1;
printf("%d\n", ++i);
}
-
++i
Compilation:
Main:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $1, -4(%rbp) //treat (sb a certain way)iassign。
addl $1, -4(%rbp) //iThe value of plus one。
movl -4(%rbp), %eax //treat (sb a certain way)iThe print operation is performed on the value of the。
movl %eax, %esi
leaq .LC0(%rip), %rax
movq %rax, %rdi
movl $0, %eax
call printf@PLT
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc