在调用之前Jit asm-sub并添加到RSP



在这个代码中有一些不理解的地方

using System;
namespace hello
{
public class Program
{
public static void Main(string[] args)
{

string a = "hi";
Console.WriteLine(a);
a = "bye";
Console.WriteLine(a);
}

}
}

这段代码在JIT asm中看起来像这个

你好.Program.ctor((

L0000: ret

hello.Program.Main(System.String[](

L0000: sub rsp, 0x28
L0004: mov rcx, 0x28cfff6fd60
L000e: mov rcx, [rcx]
L0011: call System.Console.WriteLine(System.String)
L0016: mov rcx, 0x28cfff6fd58
L0020: mov rcx, [rcx]
L0023: add rsp, 0x28
L0027: jmp System.Console.WriteLine(System.String)

为什么L0000L0023中有sub/add rsp, 0x28

sub-rsp,0x28

这意味着从堆栈指针寄存器(r=寄存器,sp=堆栈指针(减去0x28(40十进制(。

您需要这个,因为您有一个栈基变量(string a(。40字节似乎是相当多的字节,但还需要一些其他内务处理。

函数末尾的add rsp,28是从函数返回之前所需的"整理"(基本上将堆栈指针放回函数开始时的位置(。为什么是在最后一次通话之前?我想编译器知道重新排序指令没有其他副作用,按照这个顺序可能会"更有效率"。

最新更新