<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Chapter 2 Assembly and Calling Conventions on Go: Under the Hood</title><link>https://golang.design/under-the-hood/en/part1overview/ch02asm/</link><description>Recent content in Chapter 2 Assembly and Calling Conventions on Go: Under the Hood</description><generator>Hugo</generator><language>en</language><atom:link href="https://golang.design/under-the-hood/en/part1overview/ch02asm/index.xml" rel="self" type="application/rss+xml"/><item><title>2.1 The Plan 9 Assembly Language</title><link>https://golang.design/under-the-hood/en/part1overview/ch02asm/asm/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part1overview/ch02asm/asm/</guid><description>&lt;h1 id="21-the-plan-9-assembly-language"&gt;2.1 The Plan 9 Assembly Language&lt;/h1&gt;
&lt;p&gt;Read the source of the Go runtime long enough, and sooner or later you run into code that looks
like assembly yet not quite like any assembly you are familiar with. That is Go&amp;rsquo;s
&lt;strong&gt;Plan 9 style assembly&lt;/strong&gt;. We will keep returning to it as we dissect the scheduler, stack
switching, and atomic operations: &lt;code&gt;gogo&lt;/code&gt;, &lt;code&gt;mcall&lt;/code&gt;, &lt;code&gt;morestack&lt;/code&gt;, and &lt;code&gt;asyncPreempt&lt;/code&gt; are all assembly
routines. This section explains what it is, why it exists, and the few key concepts you need in
order to read it. We do not aim to teach the reader to write Plan 9 assembly, which is the subject
of another book; we aim to turn it into a &lt;strong&gt;reading vocabulary&lt;/strong&gt;: when a later section mentions some
assembly routine, the reader knows what kind of abstraction layer it lives in and what each symbol
points to.&lt;/p&gt;</description></item><item><title>2.2 Stack Frames and Symbols in Assembly</title><link>https://golang.design/under-the-hood/en/part1overview/ch02asm/frame/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part1overview/ch02asm/frame/</guid><description>&lt;h1 id="22-stack-frames-and-symbols-in-assembly"&gt;2.2 Stack Frames and Symbols in Assembly&lt;/h1&gt;
&lt;p&gt;&lt;a href=".././asm"&gt;2.1&lt;/a&gt; gave the pseudo-registers and addressing syntax of Plan 9 assembly, which is about &amp;ldquo;how to write one line of assembly&amp;rdquo;. This section raises our view to a complete routine: how a hand-written assembly function declares its own symbol with &lt;code&gt;TEXT ·name(SB)&lt;/code&gt;, how it states its stack frame size with &lt;code&gt;$framesize-argsize&lt;/code&gt;, and what &lt;code&gt;NOSPLIT&lt;/code&gt; means. We will match these conventions point by point against three real routines in the runtime (&lt;code&gt;Cas&lt;/code&gt;, &lt;code&gt;gogo&lt;/code&gt;, &lt;code&gt;morestack&lt;/code&gt;), and once you finish reading, the assembly symbols in the chapters on the scheduler and stack management turn from gibberish into readable, on-the-spot operations.&lt;/p&gt;</description></item><item><title>2.3 Calling Convention and the Register ABI</title><link>https://golang.design/under-the-hood/en/part1overview/ch02asm/callconv/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part1overview/ch02asm/callconv/</guid><description>&lt;h1 id="23-calling-convention-and-the-register-abi"&gt;2.3 Calling Convention and the Register ABI&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;The register names, stack-frame layout, and prologue code in this text use amd64 as the example.
Other architectures (arm64, riscv64, and so on) share the same structure but differ in register names; you can
cross-reference the &amp;ldquo;Architecture specifics&amp;rdquo; section of &lt;code&gt;src/cmd/compile/abi-internal&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;A single function call, at the machine level, has to answer a string of concrete questions: where do the arguments go, where does the return value go, who is responsible for saving which registers, how is the stack frame laid out, where is the return address pushed. Pinning down the answers to these questions is the calling convention, often also called the ABI (application binary interface). The ABI is not a piece of code but a &lt;strong&gt;contract&lt;/strong&gt;: the code generated by the compiler, the hand-written Plan 9 assembly (&lt;a href=".././asm"&gt;2.1&lt;/a&gt;), and the low-level routines inside the runtime are written independently of one another, yet they have to mesh exactly at the moment of the call. Once the contract is fixed, all three sides arrange their data according to it, and none of them needs to know the internal details of the others.&lt;/p&gt;</description></item><item><title>2.4 Argument Passing and Stack Frame Layout</title><link>https://golang.design/under-the-hood/en/part1overview/ch02asm/args/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://golang.design/under-the-hood/en/part1overview/ch02asm/args/</guid><description>&lt;h1 id="24-argument-passing-and-stack-frame-layout"&gt;2.4 Argument Passing and Stack Frame Layout&lt;/h1&gt;
&lt;p&gt;&lt;a href=".././callconv"&gt;2.3&lt;/a&gt; laid out the division of labor between the two ABIs and the recursive algorithm for assigning arguments. That was the &amp;ldquo;rule&amp;rdquo;. This section grounds the rule in a concrete example: given a signature that mixes scalars and arrays, how exactly are the arguments arranged in the stack frame, why does a register argument still reserve a spill slot on the stack, and how does the stack growth check that nearly every function pays for in its prologue hitch a ride on preemption. Finally we return to the overall question of &amp;ldquo;why a custom ABI&amp;rdquo; and combine these two sections into a complete answer.&lt;/p&gt;</description></item></channel></rss>