随笔 - 14,  文章 - 0,  评论 - 4,  阅读 - 439

在一些特定的使用中, i++ 可能将原值用中间量存起来以待使用,下面看相关程序的汇编代码(使用 gcc )。

  • i++ 源程序:
#include <stdio.h>

int main(){

    int i = 1;

    printf("%d\n", i++);

    return 0;

}
  • i++ 汇编:
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) //将i赋值为1.
    movl    -4(%rbp), %eax //将i的原值转移到中间变量
    leal    1(%rax), %edx //将%edx赋值为i+1
    movl    %edx, -4(%rbp)//i的值改变
    movl    %eax, %esi //对原来i的值进行打印操作
    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

再来看相同操作 ++i

  • ++i 的源程序:
#include <stdio.h>

int main(){

    int i = 1;

    printf("%d\n", ++i);

}
  • ++i 汇编:
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) //对i进行赋值。
    addl    $1, -4(%rbp) //i的值加一。
    movl    -4(%rbp), %eax //对i的值进行打印操作。
    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

笔者能力有限,如有失误,请多多见谅😊

posted on   嗯嗯好傅  阅读(88)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示