How to get assembly output from a small Go program?

polaris · · 300 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I recently posted a question on stack overflow, but haven&#39;t gotten an answer.</p> <p>The question is, given a simple Go program like this:</p> <pre><code>package main func add(a, b int) int { return a + b } func main () { add(1,2) } </code></pre> <p>How would I translate it to assembly using the command line tools?</p> <p>So far, I found two possible commands. Namely:</p> <pre><code>go tool compile -S file.go &gt; file.S </code></pre> <p>And</p> <pre><code>go tool objdump executable &gt; disassembly </code></pre> <p>But both of them produce different outputs so I&#39;m not sure If I&#39;m doing this right.</p> <p>Is there a better way to do this?</p> <p>EDIT:</p> <p>I found this command on the Go website which prints out assembly when compiling (for the same architecture):</p> <pre><code>go build -gcflags -S file.go </code></pre> <p>This seems to do the trick.</p> <hr/>**评论:**<br/><br/>Shepsauce: <pre><p>My understanding of why the outputs are different would be: 1. The compile -S will show you the assembly of that particular file all by itself. 2. The objdump tool shows references to the line numbers of the original code for what is executing. 3. You have to create an object file for objdump to work on, and that has everything you&#39;ve imported linked into the file, so the total assembly should be longer.</p></pre>Ptcdln: <pre><p>Thanks, it does seem like using the <code>objdump</code> command on an executable prints out everything that was compiled. Including the built-in functions and libraries used.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

300 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传