<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: SnowballSH</title>
    <description>The latest articles on DEV Community by SnowballSH (@snowballsh).</description>
    <link>https://dev.to/snowballsh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F567403%2F1ef43774-3cf9-4289-8cdd-9467fd14ac8c.png</url>
      <title>DEV Community: SnowballSH</title>
      <link>https://dev.to/snowballsh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/snowballsh"/>
    <language>en</language>
    <item>
      <title>Avalanche Chess Engine... Part 1</title>
      <dc:creator>SnowballSH</dc:creator>
      <pubDate>Thu, 27 Jan 2022 03:11:22 +0000</pubDate>
      <link>https://dev.to/snowballsh/avalanche-chess-engine-part-1-320j</link>
      <guid>https://dev.to/snowballsh/avalanche-chess-engine-part-1-320j</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;After successfully building &lt;a href="https://github.com/SnowballSH/GoneuraOu"&gt;a 5x5 shogi engine&lt;/a&gt; in C#, I thought I would make my own chess engine, totally from scratch. This would enhance my skills with low-level instructions, and give me something to constantly improve and challenge myself.&lt;/p&gt;

&lt;p&gt;In this series of posts, I will talk about steps to create a chess engine as well as my progress in making one.&lt;/p&gt;

&lt;p&gt;The source code of Avalanche Chess Engine will be here: &lt;a href="https://github.com/SnowballSH/Avalanche"&gt;github.com/SnowballSH/Avalanche&lt;/a&gt;. Feel free to follow along!&lt;/p&gt;

&lt;h2&gt;
  
  
  Choice of Programming Language
&lt;/h2&gt;

&lt;p&gt;I was originally going to use Rust. However, when making a chess-playing program, you don't need safety -- or rather you don't &lt;strong&gt;want&lt;/strong&gt; safety -- you only care about speed and all the safety checks should be done at debug time. Sure, you can disable checks in Rust, but the code would be bloated by &lt;code&gt;unsafe&lt;/code&gt; keywords.&lt;/p&gt;

&lt;p&gt;I choose Zig because it's an interesting C alternative. GCC works badly on my machine, so C wouldn't work. Zig uses LLVM to compile code that optimizes for different machines and is able to cross-compile to different platforms. So, here is my first Zig project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phases of developing a chess (or any other board game) engine
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Board representation
&lt;/h3&gt;

&lt;p&gt;For any game, you will need a &lt;strong&gt;state&lt;/strong&gt; to represent the current position of the game. This is usually a struct containing board(s), turn, move count, and other information.&lt;/p&gt;

&lt;p&gt;A chessboard has 8x8 = 64 squares, which is exactly the number of bits in a 64-bit unsigned integer! We can use the &lt;strong&gt;bitboard&lt;/strong&gt; board representation for chess. There's 12 kinds of pieces in chess (6 for each color), so we only need 12 &lt;code&gt;u64&lt;/code&gt; to represent the entire board. each set bit (1) at index &lt;code&gt;k&lt;/code&gt; of board &lt;code&gt;p&lt;/code&gt; means there is &lt;code&gt;p&lt;/code&gt; at the &lt;code&gt;k&lt;/code&gt;th square on the board. We can also store the combination of pieces of each color to 2 separate &lt;code&gt;u64&lt;/code&gt;s -- so we don't need to &lt;code&gt;OR&lt;/code&gt; 6 numbers everytime we need the full occupancy.&lt;/p&gt;

&lt;p&gt;Example of bitboard:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iPoKyWG2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yfzkhfb4mwo45xzibe7o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iPoKyWG2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yfzkhfb4mwo45xzibe7o.png" alt="Bitboard Example" width="215" height="196"&gt;&lt;/a&gt;&lt;br&gt;
This will be &lt;code&gt;position.bitboards.white_pawns&lt;/code&gt; after the move &lt;code&gt;1. e4&lt;/code&gt;. This bitboard can be represented as &lt;code&gt;0x1000ef00&lt;/code&gt; as a single number -- or &lt;code&gt;0b10000000000001110111100000000&lt;/code&gt;. Credits to Tearth's &lt;a href="https://tearth.dev/bitboard-viewer/"&gt;great bitboard debugger&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MPBmdFqp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5z7dgmkkjnlqft94ms28.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MPBmdFqp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5z7dgmkkjnlqft94ms28.png" alt="Layout" width="217" height="196"&gt;&lt;/a&gt;&lt;br&gt;
This is the layout of squares in my engine (and most engines). This is convenient, because &lt;code&gt;&amp;gt;&amp;gt; 1&lt;/code&gt; moves right, &lt;code&gt;&amp;lt;&amp;lt; 1&lt;/code&gt; moves left, &lt;code&gt;&amp;gt;&amp;gt; 8&lt;/code&gt; moves down, &lt;code&gt;&amp;lt;&amp;lt; 8&lt;/code&gt; moves up, very intuitive.&lt;/p&gt;
&lt;h4&gt;
  
  
  Btw, Bit twiddling
&lt;/h4&gt;

&lt;p&gt;To check if a square is set, we can do &lt;code&gt;@truncate(u1, bb &amp;gt;&amp;gt; sq)&lt;/code&gt; in Zig.&lt;/p&gt;

&lt;p&gt;To toggle a square, we can do &lt;code&gt;bb ^= 1 &amp;lt;&amp;lt; sq&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To set a square, we can do &lt;code&gt;bb |= 1 &amp;lt;&amp;lt; sq&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To clear a square, we can do &lt;code&gt;bb &amp;amp;= ~(1 &amp;lt;&amp;lt; sq)&lt;/code&gt;, which means parts of &lt;code&gt;bb&lt;/code&gt; that does not intersect with &lt;code&gt;1 &amp;lt;&amp;lt; sq&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We also need a turn (0 for white and 1 for black), &lt;a href="https://en.wikipedia.org/wiki/En_passant"&gt;En Passant&lt;/a&gt; square (null or 0-63), Castling rights (4-bit bitflag, WhiteKing = 1, WhiteQueen = 2, BlackKing = 4, BlackQueen = 8, we can AND and OR them to get different combinations).&lt;/p&gt;
&lt;h3&gt;
  
  
  Move generation &amp;amp; attacks
&lt;/h3&gt;

&lt;p&gt;You don't just play with a board. The program needs knowledge about which moves are legal.&lt;/p&gt;

&lt;p&gt;With bitboards, it is easy to pre-compute attack tables for leapers (Pawn, King, Knight). With Zig, it is even easier to generate code at compile-time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight zig"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;KingDelta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;i8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;KingPatterns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;u64&lt;/span&gt; &lt;span class="k"&gt;align&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;@setEvalBranchQuota&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c"&gt;// force compiler to compute this&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;patterns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;u64&lt;/span&gt; &lt;span class="k"&gt;align&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;patterns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;i8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rank_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;i8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;file_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;bb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;u64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KingDelta&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;bb&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rank_file_to_bb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;pt&lt;/span&gt;&lt;span class="o"&gt;.*&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bb&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;init&lt;/span&gt; &lt;span class="n"&gt;patterns&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And... Done! We just finished the move generator for Kings.&lt;br&gt;
&lt;code&gt;KingPatterns[sq]&lt;/code&gt; will be a bitboard containing all legal moves of the king.&lt;/p&gt;

&lt;p&gt;Let's say &lt;code&gt;sq&lt;/code&gt; is 20. (Refer to the image above for location)&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GPHsxge_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sz2br69faua30g43dpqk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GPHsxge_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sz2br69faua30g43dpqk.png" alt="KingPatterns 20" width="354" height="286"&gt;&lt;/a&gt;&lt;br&gt;
This is &lt;code&gt;KingPatterns[20]&lt;/code&gt;, in which we can use &lt;code&gt;bitscan&lt;/code&gt; to find the first square that is set and send it to the search function. &lt;code&gt;bitscan&lt;/code&gt; can also be replaced with a single CPU instruction on most modern machines - &lt;code&gt;ctz&lt;/code&gt;, or least-significant bit, or "count trailing zeroes". This makes sure bitboard is faster than representing the board with 2-dimensional arrays.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ending
&lt;/h2&gt;

&lt;p&gt;I will talk about some different techniques to generate slider (bishop, rook, queen) moves in the next post, since I barely figured out how to use Magic Bitboards myself. Normal algorithms would definitely work (which is what Avalanche has now) -- but Magic Bitboard signifies the true &lt;strong&gt;magic&lt;/strong&gt; of bitboards -- computing all attacks with a single multiplication!&lt;/p&gt;

&lt;p&gt;Avalanche can now complete basic &lt;strong&gt;perft&lt;/strong&gt; positions (perft stands for Performance Test, testing the speed and accuracy of move generation) and is correct 99.9% of the time. There's some small bugs I'll fix while it is playing games and some performance issues (currently 2x slower than a C# Magic-bitboard generator).&lt;/p&gt;

&lt;p&gt;Thank you for reading, and I'll see you in the next post.&lt;br&gt;
&lt;a href="https://github.com/SnowballSH/Avalanche"&gt;github.com/SnowballSH/Avalanche&lt;/a&gt;&lt;/p&gt;

</description>
      <category>chess</category>
      <category>zig</category>
    </item>
    <item>
      <title>Go With Assembly</title>
      <dc:creator>SnowballSH</dc:creator>
      <pubDate>Mon, 19 Apr 2021 02:40:21 +0000</pubDate>
      <link>https://dev.to/snowballsh/go-with-assembly-499p</link>
      <guid>https://dev.to/snowballsh/go-with-assembly-499p</guid>
      <description>&lt;p&gt;Recently I just found out that you can use Go with Assembly. The Assembly isn't x86_64 or ARM, it is Go's special Assembly format, using syntax inspired by the Plan 9 Assembler.&lt;/p&gt;

&lt;p&gt;This post is a short introduction for it.&lt;/p&gt;

&lt;p&gt;Lets say we have some Go code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="kt"&gt;int64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int64&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://goplay.space/#trWiW4KyoNe"&gt;try online&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty simple, right? Prints &lt;code&gt;"5\n"&lt;/code&gt; to stderr.&lt;/p&gt;

&lt;p&gt;Now we want to power it up with Go's Assembler.&lt;br&gt;
Create a file called &lt;code&gt;add.s&lt;/code&gt; in the same directory with &lt;code&gt;main.go&lt;/code&gt;. Use this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight r"&gt;&lt;code&gt;&lt;span class="n"&gt;TEXT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;·&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SB&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="m"&gt;0-24&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="m"&gt;+0&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FP&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BX&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="m"&gt;+8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FP&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BP&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;ADDQ&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BX&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="m"&gt;+16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;RET&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me explain the code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;TEXT&lt;/code&gt; tells the assembler that this is a function.&lt;br&gt;
The syntax to declare a function is&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;TEXT package_name·function_name(SB), $frame_size-argument_size&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Note that the dot is &lt;code&gt;·&lt;/code&gt;(Unicode 0xB7), not &lt;code&gt;.&lt;/code&gt;.&lt;br&gt;
In this example, &lt;code&gt;TEXT ·add(SB), $0-24&lt;/code&gt; declares a function called &lt;code&gt;main·add&lt;/code&gt; with frame size of 0 (registers are enough) and argument size of 24 (3 * 8).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;MOVQ x+0(FP), BX&lt;/code&gt; moves &lt;code&gt;*x&lt;/code&gt; to the &lt;code&gt;BX&lt;/code&gt; register.&lt;br&gt;
&lt;code&gt;MOVQ a, b&lt;/code&gt; moves a &lt;strong&gt;64-bit&lt;/strong&gt; value &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;b&lt;/code&gt;. &lt;code&gt;Q&lt;/code&gt; stands for quadword.&lt;br&gt;
&lt;code&gt;x+0(FP)&lt;/code&gt; accesses the argument &lt;code&gt;x&lt;/code&gt;. &lt;code&gt;symbol+offset(register)&lt;/code&gt; accesses the symbol from register using the offset. &lt;code&gt;FP&lt;/code&gt; is the register for the function arguments.&lt;/p&gt;

&lt;p&gt;Similarly, &lt;code&gt;MOVQ y+8(FP), BP&lt;/code&gt; moves &lt;code&gt;*y&lt;/code&gt; to the &lt;code&gt;BP&lt;/code&gt; register.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ADDQ BP, BX&lt;/code&gt; adds BP and BX, storing the result to &lt;code&gt;BX&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;MOVQ BX, ret+16(FP)&lt;/code&gt; moves the result to the return register. +16 because we have a size of 24 and each argument used 8, so ret needs to use 16-23.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;RET&lt;/code&gt; simply returns the last result &lt;code&gt;ret&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With all those complex assembly explained, we can use it now.&lt;/p&gt;

&lt;p&gt;Change the &lt;code&gt;main.go&lt;/code&gt; to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="kt"&gt;int64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int64&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure &lt;code&gt;add.s&lt;/code&gt; is in the same directory as &lt;code&gt;main.go&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Compile using &lt;code&gt;go build&lt;/code&gt; or run with &lt;code&gt;go run&lt;/code&gt;. You will still see 5 for the result!&lt;/p&gt;




&lt;p&gt;In case you are wondering, you can print hello world with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight r"&gt;&lt;code&gt;&lt;span class="c1"&gt;#include "textflag.h"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;DATA&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;world&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;+0&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="s2"&gt;"hello wo"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;DATA&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;world&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;+8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="s2"&gt;"rld "&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;GLOBL&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;world&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;+0&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SB&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;RODATA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="m"&gt;12&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;TEXT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;·&lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SB&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="m"&gt;88-0&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;SUBQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="m"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;BP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;80&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;LEAQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="m"&gt;80&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BP&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="n"&gt;LEAQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;world&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;+0&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SB&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AX&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;AX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="m"&gt;+48&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;        
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="m"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="m"&gt;+56&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;autotmp_0&lt;/span&gt;&lt;span class="m"&gt;+64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;autotmp_0&lt;/span&gt;&lt;span class="m"&gt;+72&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;LEAQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="err"&gt;·&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SB&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AX&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;AX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;LEAQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="m"&gt;+48&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AX&lt;/span&gt;&lt;span class="w"&gt;        
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;AX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="n"&gt;CALL&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="err"&gt;·&lt;/span&gt;&lt;span class="n"&gt;convT2E&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;           
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="m"&gt;24&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AX&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="m"&gt;16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;CX&lt;/span&gt;&lt;span class="w"&gt;                    
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;CX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;autotmp_0&lt;/span&gt;&lt;span class="m"&gt;+64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;        
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;AX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;autotmp_0&lt;/span&gt;&lt;span class="m"&gt;+72&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;LEAQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;autotmp_0&lt;/span&gt;&lt;span class="m"&gt;+64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AX&lt;/span&gt;&lt;span class="w"&gt;        
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;AX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;                      
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;                      
    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="n"&gt;CALL&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="err"&gt;·&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="n"&gt;MOVQ&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;80&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;BP&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;ADDQ&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="m"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SP&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;RET&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
 (Credit: &lt;a href="https://davidwong.fr/goasm/hello"&gt;davidwong.fr/goasm/hello&lt;/a&gt;)&lt;/p&gt;




&lt;p&gt;According to some benchmark, using Go's assembler makes Go faster.&lt;br&gt;
However, I don't recommend using it in real projects.&lt;/p&gt;

&lt;p&gt;First, you lose Go's automatic garbage collecting. It will result in heavy memory usage if you do a lot of arrays using Assembly.&lt;/p&gt;

&lt;p&gt;Second, you lose Go's simplicity. Go is aimed for simple and efficient, but Assembly is clearly not simple at all. A small add function turns into 6 lines of assembly code... You won't be able to complete a project using it.&lt;/p&gt;

&lt;p&gt;Third, you lose the ability to debug and manage your project. Go's assembler provides bad error messages and the worst error I have encountered is &lt;code&gt;unexpected return pc&lt;/code&gt; followed by 30 binary numbers. If you encounter a bug, you will probably spend 1 hour to fix it!&lt;/p&gt;

&lt;p&gt;Despite all of that, Assembly in Go is still very interesting! :)&lt;/p&gt;




&lt;p&gt;Resources:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://davidwong.fr/goasm"&gt;davidwong.fr/goasm&lt;/a&gt;&lt;br&gt;
&lt;a href="https://golang.org/doc/asm"&gt;Go doc for asm&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>assembly</category>
    </item>
    <item>
      <title>[Part 0] Create your own programming language, in Python</title>
      <dc:creator>SnowballSH</dc:creator>
      <pubDate>Tue, 26 Jan 2021 05:02:38 +0000</pubDate>
      <link>https://dev.to/snowballsh/part-0-create-your-own-programming-language-in-python-5hgh</link>
      <guid>https://dev.to/snowballsh/part-0-create-your-own-programming-language-in-python-5hgh</guid>
      <description>&lt;h2&gt;
  
  
  So, you think you have enough programming skills?
&lt;/h2&gt;

&lt;p&gt;If so, why not do something really fun -- create your own programming language!&lt;/p&gt;

&lt;h3&gt;
  
  
  Who am I?
&lt;/h3&gt;

&lt;p&gt;I am SnowballSH, a programmer and music producer. I have a decent experience in lexical analysing, parsing, and language designing. &lt;a href="https://github.com/SnowballSH/Gorilla"&gt;Gorilla&lt;/a&gt; is the best programming language I have made, in golang. Feel free to play it &lt;a href="https://snowballsh.me/Gorilla-Playground/"&gt;here&lt;/a&gt; online!&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Python?
&lt;/h3&gt;

&lt;p&gt;To be clear, I definitely do not suggest making programming languages in Python.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is slow&lt;/li&gt;
&lt;li&gt;It is dynamic -- more random errors will occur to the user&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, I chose Python to show you the basic &lt;strong&gt;principles&lt;/strong&gt; and &lt;strong&gt;terms&lt;/strong&gt; in language designing. Therefore, I think Python is clean and very good to explain basics.&lt;br&gt;
I will document the types of almost every argument of functions so you can catch on with any static-typed programming languages too!&lt;/p&gt;

&lt;p&gt;If you are wondering what is Python...&lt;/p&gt;

&lt;p&gt;This is Python:&lt;/p&gt;


&lt;div class="ltag__replit"&gt;
  &lt;iframe height="550px" src="https://repl.it/@SnowballSH/LangTutorial-Python?lite=true"&gt;&lt;/iframe&gt;
&lt;/div&gt;


&lt;p&gt;(Just kidding)&lt;/p&gt;

&lt;h3&gt;
  
  
  Uhh, OK, but I have absolutely no idea how a programming language like Python is made!
&lt;/h3&gt;

&lt;p&gt;Well, if you know how one is made, why are you even here? We are here exactly talking about the fundamentals and basics.&lt;/p&gt;

&lt;h4&gt;
  
  
  Here is the process of an interpreted programming language:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f2RFZnIN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/WvJ7boD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f2RFZnIN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/WvJ7boD.png" alt="Process of language" width="448" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Credit: &lt;em&gt;Marc-André Cournoyer&lt;/em&gt;
&lt;/h6&gt;

&lt;h4&gt;
  
  
  The process of a compiled programming language:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mq19zUFZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/GXJHyaZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mq19zUFZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/GXJHyaZ.png" alt="Process Compiled Language" width="452" height="527"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Differences between "Compiled" and "Interpreted"
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Compiled_language"&gt;Compiled Language&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Contains Lexer and Parser&lt;/li&gt;
&lt;li&gt;Has a compiler that compiles user's code into a low-level machine code. It can either be Web Assembly, Machine Code, Assembly, JVM, or even your custom byte code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pro&lt;/strong&gt;: It is fast due to its runtime is based on the native machine&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Con&lt;/strong&gt;: Extremely hard to manage. The native commands and opcodes differ with the system/OS, therefore making it harder to add more features.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Interpreted_language"&gt;Interpreted Language&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Conatins Lexer and Parser&lt;/li&gt;
&lt;li&gt;Has a special interpreter that accepts Abstract Syntax Trees (Code outline) and runs through it. By doing this way, there is no native runtime. Everything is ran by higher-level languages (e.g. Python, C++) directly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pro&lt;/strong&gt;: Easy to manage. The language you are writing your language with has done the compliation for you -- you only have to manage one single case -- The language you are written in. This way you can add features pretty easily and fast by just using one function!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Con&lt;/strong&gt;: It is slow due to it is ran on a high-level programming language. How slow is it? Well, you will see right below.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of the times, language makers create their own Virtual Machines and Bytecode (a series of instructions similar to assembly) too.&lt;br&gt;
For example, Python is compiled to "Python Bytecode" using C, and "Python Bytecode" executes the bytecode using C too. This way the Bytecode is flattened and makes the language about 3x faster than normal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real World Examples
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Interpreted
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Compiles to Bytecode

&lt;ul&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;li&gt;Ruby&lt;/li&gt;
&lt;li&gt;Java (Compiles to JVM Bytecode)&lt;/li&gt;
&lt;li&gt;C# (Compiles to &lt;a href="https://en.wikipedia.org/wiki/Common_Intermediate_Language"&gt;CIL&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Fully Interpreted

&lt;ul&gt;
&lt;li&gt;JavaScript&lt;/li&gt;
&lt;li&gt;R&lt;/li&gt;
&lt;li&gt;PHP&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Compiled
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Compiles to Machine Code

&lt;ul&gt;
&lt;li&gt;C&lt;/li&gt;
&lt;li&gt;C++&lt;/li&gt;
&lt;li&gt;Ada&lt;/li&gt;
&lt;li&gt;Go/Golang&lt;/li&gt;
&lt;li&gt;Crystal (LLVM)&lt;/li&gt;
&lt;li&gt;Lisp&lt;/li&gt;
&lt;li&gt;Pascal&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Compiles to Bytecode

&lt;ul&gt;
&lt;li&gt;Java (as said, to JVM)&lt;/li&gt;
&lt;li&gt;C# (to CIL)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What we are going to make
&lt;/h3&gt;

&lt;p&gt;For the sake of this tutorial (my first tutorial ever), we will be building a dynamic, object-oriented, fully-interpreted programming language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As a said, an interpreted language is easy to manage.&lt;/p&gt;

&lt;p&gt;Although it is slow, it can really show you guys the process of making a language easily!&lt;/p&gt;

&lt;p&gt;If you have a strong opinion making a compiled language, I won't disagree -- in fact I love compiled languages. I chose it to be interpreted just to make everyone clear!&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Thanks for reading until here! We will talk about the syntax of our language, and start up with &lt;strong&gt;lexical analyzer&lt;/strong&gt;! &lt;/p&gt;

&lt;h3&gt;
  
  
  See you later :&amp;gt;
&lt;/h3&gt;

</description>
    </item>
  </channel>
</rss>
