#include <stdio.h>
void main()
{
int n=10;
int a,b;
a=--n;
b=n--;
printf("%d\n",n);
}
下面是由上面程序生成的汇编程序,从汇编程序可以看出再用n--进行操作时会用到两个寄存器,一个用于更新n的值,一个用于保存原来n的值用于其他操作,相比较而言--n只用到一个寄存器,开销更小
.arch armv6
.eabi_attribute 27, 3
.eabi_attribute 28, 1
.fpu vfp
.eabi_attribute 20, 1
.eabi_attribute 21, 1
.eabi_attribute 23, 3
.eabi_attribute 24, 1
.eabi_attribute 25, 1
.eabi_attribute 26, 2
.eabi_attribute 30, 6
.eabi_attribute 34, 1
.eabi_attribute 18, 4
.file "code.c"
.section .rodata
.align 2
.LC0:
.ascii "%d\012\000"
.text
.align 2
.global main
.type main, %function
main:
@ args = 0, pretend = 0, frame = 16
@ frame_needed = 1, uses_anonymous_args = 0
stmfd sp!, {fp, lr}
add fp, sp, #4
sub sp, sp, #16
;给n赋值
mov r3, #10
str r3, [fp, #-8]
;n减一,将减一后的n赋值给a,这里只需要一个寄存器,将原来的n值存在该寄存器中,然后减一,再将减一后的结果写入n
ldr r3, [fp, #-8]
sub r3, r3, #1
str r3, [fp, #-8]
ldr r3, [fp, #-8]
str r3, [fp, #-12]
;这里需要两个寄存器,r3用来保存n原来的值,以便后续操作用到原来的n,r2用来将n中原来的值减一并写回n
;--n只用到一个寄存器,而n--用到两个寄存器,开销较大
ldr r3, [fp, #-8]
sub r2, r3, #1
str r2, [fp, #-8]
str r3, [fp, #-16]
ldr r0, .L2
ldr r1, [fp, #-8]
bl printf
sub sp, fp, #4
@ sp needed
ldmfd sp!, {fp, pc}
.L3:
.align 2
.L2:
.word .LC0
.size main, .-main
.ident "GCC: (Raspbian 4.9.2-10) 4.9.2"
.section .note.GNU-stack,"",%progbits