请选择 进入手机版 | 继续访问电脑版

C#代码编译成机器指令码

电脑技术 电脑技术 1126 人阅读 | 1 人回复 | 2022-07-02

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
看一看这个C#源代码,呼叫Square()函数:

  1. using System;

  2. class Program
  3. {
  4.    static int Square(int num) => num * num;

  5.    static void Main(string[] args){
  6.        Console.WriteLine(Square(8));
  7.    }
  8. }   
复制代码

可是最终的机器指令码,编译器其实没有呼叫Square()函数,而是直接计算8x8的结果(64):
  1. Program:.ctor():this:
  2.       ret      

  3. Program:Main(System.String[]):
  4.       push     rax
  5.       mov      edi, 64
  6.       call     [System.Console:WriteLine(int)]
  7.       nop      
  8.       add      rsp, 8
  9.       ret      

  10. Program:Square(int):int:
  11.       mov      eax, edi
  12.       imul     eax, edi
  13.       ret   
复制代码


这是不是很有趣?编译器的优化使到这种结果变成有可能。

回答|共 1 个

孤星1

孤星1 发表于 2022-7-4 21:41:25| 字数 21 | 显示全部楼层

看来没多少人对这个课题感兴趣,还好我理解。  
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热门推荐