当前位置: 首页 > news >正文

【C編程】彙編語言文件

C編程時,我們可能需要做一些彙編語言編程,以增加granularity。另外,在Xcode debug時,會跳入彙編語言程序。

彙編語言的程序結構如下。[1]


# PURPOSE:
#          Simple program that exits and returns a 
#          status code back to the Linux kernel# INPUT:   none# OUTPUT:  returns a status code.This can be viewed by typing 'echo $'?#          after running the program# VARIABLES:
#          %eax holds the system call number
#          %ebx holds the return status#
#.section _data
#.section _text
.globl _start
_start:movl $1, %eax         # this is the linux kernel command# number (system call) for exiting# a programmovl $0, %ebx         # this is the status number we will# return to the operating system.# Change this around and it will# return different things to# echo $?int $0x80             # this wakes up the kernel to run# the exit command

這是C exit(0)對應的彙編語言,eax存儲system call number,ebx存儲返回值。可用echo $?查看返回值。

在MacOS 10.14.5,以上代碼存為exit.s。先經assembler編譯為.o文件,再經linker生成可執行程序。

本例我的實際操作是:

as exit.s -o exit.o

gcc exit.o -o exit

echo $?

使用彙編語言編譯器as編譯為.o文件。再用gcc將.o編譯為可執行程序。使用gcc而不是ld,是因為我嘗試了,不成功,有的程序可以ld成功。

<br >

每個機器不同的處理器,彙編語言不同。如何適應?

我想從C語言編譯的彙編文件學習。

比如hello.c程序:


#include <stdio.h>int main() {printf("您好,世界。");return 0;
} 

gcc -save-temps hello.c

gcc編譯依次經過1)預處理器(preprocessor)生成的.i文件;2)C編譯器,生成彙編語言程序.s文件;3)經彙編編譯器,生成目標代碼.o文件,這是代碼;4)經Linker,在操作系統註冊,生成可執行程序。-save-temps參數可讓我們看到過程文檔。[^Tiffany]

打開hello.s。

    .section    __TEXT,__text,regular,pure_instructions.build_version macos, 10, 14    sdk_version 10, 15.globl    _main                   ## -- Begin function main.p2align    4, 0x90
_main:                                  ## @main.cfi_startproc
## %bb.0:pushq    %rbp.cfi_def_cfa_offset 16.cfi_offset %rbp, -16movq    %rsp, %rbp.cfi_def_cfa_register %rbpsubq    $16, %rspmovl    $0, -4(%rbp)leaq    L_.str(%rip), %rdimovb    $0, %alcallq    _printfxorl    %ecx, %ecxmovl    %eax, -8(%rbp)          ## 4-byte Spillmovl    %ecx, %eaxaddq    $16, %rsppopq    %rbpretq.cfi_endproc## -- End function.section    __TEXT,__cstring,cstring_literals
L_.str:                                 ## @.str.asciz    "\346\202\250\345\245\275\357\274\214\344\270\226\347\225\214\343\200\202".subsections_via_symbols

這裏exit部分看起來用到了eax,ecx。中文UTF編碼用八進制顯示。

<br >

另一個Hello World程序的代碼是這樣的:[2]


.global start
.intel_syntax noprefixstart:# Write "Hello World"mov rax, 0x2000004                # system call 4 (write code)mov rdi, 1lea rsi, hello_world[rip]mov rdx, 12                       # Set register rdx to 12syscall                           # envoke syscall# Exit programmov rax, 0x2000001                # system call 1 (exit code)mov rdi, 99                       # set the exit code to 99syscallhello_world:                          # Definition of hello_world.asciz "Hello World\n"

為什麼是rax,rdi?這些寄存器好像有包含和被包含的關係。但我也還不清楚。我學習彙編語言的思路是這樣的。

這裏是彙編語言整體的命令查詢。[3]



參考:

[^Tiffany]Tiffany的世界,[GCC]GCC常用命令和参数,2025-07-08,https://zhuanlan.zhihu.com/p/1925840990817740428


  1. Jason Note,如何在linux下編譯彙編程序,內功心法是彙編程序的結構,https://jasonblog.github.io/note/assembly/ru_he_zai_linux_xia_bian_yi_hui_bian_cheng_xu_ff0c.html ↩︎

  2. Getting Started with Assembly on x86-64 MacOS: Hello World, https://zerodayarcade.com/tutorials/hello-world-assembly-macos ↩︎

  3. 彙編,https://www.newton.com.tw/wiki/彙編 ↩︎

http://www.jsqmd.com/news/78405/

相关文章:

  • 国内/高速/855/1160/卧式/桥式五轴/龙门/立式加工中心那个厂家刚性好?稳定性好? - 品牌推荐大师
  • 3大核心技术揭秘:Puerts如何让UE5程序化生成效率提升500%
  • 这三种人当黑客早晚会出事:别让技术毁了前途,其实走合法路径收入更高
  • 自动化缺陷修复的建议生成:赋能软件测试新范式
  • CTF网络安全大赛_网安战队简历,零基础入门到精通,收藏这一篇就够了
  • 设计院为何陷入“三高困境”?高负荷、高流失、高风险
  • GPT-5.2大模型全方位解析:从技术优势到实战应用(建议收藏)
  • JS正则验证QQ邮箱格式,5-11位数字核心规则详解
  • Spring Boot项目优化和JVM调优 - 教程
  • Elasticsearch Bulk 写入性能问题排查实录
  • AI中的函数调用是什么?
  • AI重塑API测试数据生成的时代背景
  • YYEVA动效播放器完整使用指南:从入门到精通
  • MIT和魏茨曼研究所首次用AI大规模解密视觉皮层的秘密语言
  • 字节跳动发布GAR:让AI能像人类一样精准理解图像任何区域的技术
  • 高通量多肽合成仪安全操作及保养规程 - 品牌推荐大师
  • 物流排班到底该自研还是采购SaaS?4大核心维度揭示最佳解法
  • 基于zigbee的广告牌安全监测系统设计与实现(论文+源码)
  • 多肽药物开发:兼具双重优势的医药研发热门领域
  • 天塌了!pmp考纲再次改版,费用上涨并且新增题型?
  • FP8量化训练终极指南:重塑大模型效率边界
  • 视觉检测设备的生产及应用软件开发
  • 工程师必看的45条FPC设计技巧(板厚说明)
  • 测试管理工具:TestRail与JIRA集成
  • 根据这个标题帮我生成一篇新的文章,我的用户群体是软件测试从业者。
  • 香港科大推出BiCo:让AI像艺术家自由组合图片和视频中的任意元素
  • 探索-JavaScript-ES2025-版--四-
  • 16种大模型主流微调方法
  • AH808A:内置PFM,170V/800mA固定输出5V降压DCDC转换器
  • 超网和路由概述