将 C/C++ 代码转换为汇编语言

将 c/c++ 代码转换为汇编语言

在这里,我们将了解如何使用 gcc 从 C 或 C++ 源代码生成汇编语言输出。

gcc 提供了一个很棒的功能,可以在执行时从源代码获取所有中间输出。为了获得汇编器输出,我们可以使用 gcc 的选项“-S”。此选项显示编译后但发送到汇编器之前的输出。该命令的语法如下。

gcc –S program.cpp

登录后复制

现在,让我们看看输出会是什么样子。这里我们使用一个简单的程序。在这个程序中,两个数字被存储到变量 x 和 y 中,然后将和存储到另一个变量中,然后打印结果。

示例

#include using namespace std;main() {   int x, y, sum;   x = 50;   y = 60;   sum = x + y;   cout 

输出

.file "test_cpp.cpp".text.section .rodata.type _ZStL19piecewise_construct, @object.size _ZStL19piecewise_construct, 1_ZStL19piecewise_construct:.zero 1.local _ZStL8__ioinit.comm _ZStL8__ioinit,1,1.LC0:.string "Sum is: ".text.globl main.type main, @functionmain:.LFB1493:.cfi_startprocpushq %rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq %rsp, %rbp.cfi_def_cfa_register 6subq $16, %rspmovl $50, -12(%rbp)movl $60, -8(%rbp)movl -12(%rbp), %edxmovl -8(%rbp), %eaxaddl %edx, %eaxmovl %eax, -4(%rbp)leaq .LC0(%rip), %rsileaq _ZSt4cout(%rip), %rdicall _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLTmovq %rax, %rdxmovl -4(%rbp), %eaxmovl %eax, %esimovq %rdx, %rdicall _ZNSolsEi@PLTmovq %rax, %rdxmovq_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip),%raxmovq %rax, %rsimovq %rdx, %rdicall _ZNSolsEPFRSoS_E@PLTmovl $0, %eaxleave.cfi_def_cfa 7, 8ret.cfi_endproc.LFE1493:.size main, .-main.type _Z41__static_initialization_and_destruction_0ii, @function_Z41__static_initialization_and_destruction_0ii:.LFB1982:.cfi_startprocpushq %rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq %rsp, %rbp.cfi_def_cfa_register 6subq $16, %rspmovl %edi, -4(%rbp)movl %esi, -8(%rbp)cmpl $1, -4(%rbp)jne .L5cmpl $65535, -8(%rbp)jne .L5leaq _ZStL8__ioinit(%rip), %rdicall _ZNSt8ios_base4InitC1Ev@PLTleaq __dso_handle(%rip), %rdxleaq _ZStL8__ioinit(%rip), %rsimovq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %raxmovq %rax, %rdicall __cxa_atexit@PLT.L5:nopleave.cfi_def_cfa 7, 8ret.cfi_endproc.LFE1982:.size _Z41__static_initialization_and_destruction_0ii,.-_Z41__static_initialization_and_destruction_0ii.type _GLOBAL__sub_I_main, @function_GLOBAL__sub_I_main:.LFB1983:.cfi_startprocpushq %rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq %rsp, %rbp.cfi_def_cfa_register 6movl $65535, %esimovl $1, %edicall _Z41__static_initialization_and_destruction_0iipopq %rbp.cfi_def_cfa 7, 8ret.cfi_endproc.LFE1983:.size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main.section .init_array,"aw".align 8.quad _GLOBAL__sub_I_main.hidden __dso_handle.ident "GCC: (Ubuntu 7.3.0-16ubuntu3) 7.3.0".section .note.GNU-stack,"",@progbits

登录后复制

以上就是将 C/C++ 代码转换为汇编语言的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2582720.html

(0)
上一篇 2025年3月6日 14:23:07
下一篇 2025年3月6日 14:23:15

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论