6 ms·
Go Assembly by Example
- julienfr112 9y agoWhat is the command line to assemble and run ???
- benesch 9y agoSimply drop the assembly as a .s file in a Go package and run `go build`. See Go's hash/crc32 package for a real example [0]. There's a lower level `go tool` command, of course, but I don't know it off the top of my head. [0]: https://github.com/golang/go/tree/master/src/hash/crc32 https://github.com/golang/go/tree/master/src/hash/crc32
- julienfr112 9y agoamazing
- TheAceOfHearts 9y agoA few days ago I was reading through a few Wikipedia article related to assembly, and I found out Go has its own assembler [0]: > Some others self-hosted native-targeted language implementations (like Golang, Free Pascal, SBCL) have their own assemblers with multiple targets. They may be used for inline assembly inside language, or even included as a library, but not always suitable for standalone application - no command-line tool exists, or only intermediate representation used as a source, or support for targets very limited. Despite receive so much criticism, Go seems to be doing pretty well at carving out its own place. It's nice to see a language pick certain categories of problems and just focus on that. I haven't had any chance to write something serious in Go, but I've had to dig through a few projects and it's always refreshingly simple to read and understand. Really wish I could say the same thing about C++. [0] https://en.wikipedia.org/wiki/Comparison_of_assemblers#As_part_of_a_compiler_suite https://en.wikipedia.org/wiki/Comparison_of_assemblers#As_pa...
- keldaris 9y ago> I've had to dig through a few projects and it's always refreshingly simple to read and understand. Really wish I could say the same thing about C++. That's just not a design goal of C++. Different languages have different priorities and appeal to different folks. Personally, while writing C++ can be frustrating at times and the language still has too many arbitrary restrictions for my taste, every time I go through a Go codebase I feel glad I haven't had to write any Go myself. The amount of mindless, repetitive boilerplate one has to write because the language tries to be "simple" at the expense of abstraction, genericity and expressiveness looks intolerable to me. But, clearly, there are people that enjoy writing Go and that's great. More diversity in language design can only be a good thing.
- weberc2 9y agoThere is certainly some of that. Personally, I find that the Go language can be quite limiting, but there are often workarounds for the problems. That said, workarounds aren't a good excuse for failing to have first-class support for various use cases, and sometimes Go simply doesn't have a suitable workaround. These are my criticisms for Go the language. Go the ecosystem is top-notch; there's virtually no learning curve, and almost everything Just Works. Testing, benchmarking, and profiling are all supported out of the box. No project metadata files or build script files to learn or maintain. Everyone's code looks the same (`gofmt`), and the constraints imposed by the language make for boring, super-easy-to-read code, even if it is tedious for the writer. No special documentation syntax to learn--docs are just comments. http://godoc.org http://godoc.org is bar-none the best documentation site for any language I've used (everything is in one place, clean layout, easy to navigate, etc). Even if code isn't well documented, documentation tools can use the static type structure of the program to give you enough information to piece together how to use a library. Depending on the application, the utility of the ecosystem often outweighs the constraints of the language. But inevitably, Go will have to improve as a language before some other language's ecosystem improves and eats Go's lunch.
- keldaris 9y ago> Depending on the application, the utility of the ecosystem often outweighs the constraints of the language. This is the one part where I generally tend to disagree. Basic language constraints are immutable, whereas ecosystem pain points can be fixed if people care enough. Everything you cited is true, the Go people seem to have done a great job at making the ecosystem convenient to use, but language issues are the one thing users can't fix on their own. The C++ ecosystem is fragmented, everyone has their own preferences and we'd all like to see it improved, but individual developers (as well as organizations, just look at Google's internal ecosystem) find a way to make it work well. If there's a dealbreaker in language design, on the other hand, there's just nothing you can do.
- chrissnell 9y agoNeat, but I don't quite get it. How do I pass values between Go code and assembly? The format of this site is a little strange: it looks like an example Go snippet, followed by what I assume is the equivalent assembly code--but where is the connection? Or, is the point simply to teach us Go devs assembly? Also, when is an appropriate time to use assembly in a Go program? After the failed trig experiment blog post that was featured here last week, the benefits aren't very clear to me. EDIT: ok, I see it now. The empty function declaration. What an odd syntax. I get the tutorial now, but still wondering about the benefits.
- Thaxll 9y agoLike in any programs to get better optimisation / performance on very specific functions.
- chrissnell 9y agoNot sure if you saw the blog post I referenced that was trending here last week. These types of optimizations often turn out to be underwhelming.
- eloff 9y agoNot true at all, there are 20-50x speedups for some of the functions that have been optimized into Go assembly. Specific cryptography routines for which CPUs have dedicated instructions. Those are the kinds of places where you'd use it. If you want to write assembly because you think you can generate better code than the compiler, you probably can, but it also almost certainly will be underwhelming as you say. It's only really worth it when you're using instructions the compiler won't emit, like specialized crypto, math, bit counting instructions, or SIMD instructions.
- dungle6 9y agoOften is very much not the same as always. Moreover, when these optimizations are useful (i.e. Not underwhelming) the payoff is significant (like multiple orders of magnitude significant). Additionally crypto is special due to side channel issues of course. Anyway most compilers are generally shit at vectorization generally.
- irfansharif 9y agoTangentially related, recently came across this: https://go.godbolt.org/ https://go.godbolt.org/ It's an interactive compiler allowing you fiddle around with the generated assembly.
- iainmerrick 9y agoIs Go's assembly intended to be mostly platform-independent or platform-specific? I clicked through to the AES example, and found that it "makes use of Go Assembly to leverage Intel's hardware support for AES, calling the AES-NI CPU instructions." So does that only work on Intel CPUs? I'm confused about why Go has its own assembly syntax if you're going to be writing platform-specific code anyway. Or can you write Go assembly that works well on both x86 and ARM, say? EDIT to put the question a different way: what are some use cases where you'd use Go assembly? In C, you'd typically drop down to platform-specific assembly for two reasons: 1) to muck about with the stack, for example in a threading library, or 2) to use platform-specific instructions, like NEON or SSE. (In that second case you can often get away with intrinsics rather than assembly, though.) Does Go assembly have the same uses, or different ones?
- to3m 9y agoFor another view: http://dtrace.org/blogs/wesolows/2014/12/29/golang-is-trash/ http://dtrace.org/blogs/wesolows/2014/12/29/golang-is-trash/ I'm partly with the dtrace guy, but it is nice to see somebody not propagating that gcc inline asm shite... so I'm prepared to forgive it a certain amount.
- iainmerrick 9y agoI'm not a Go fan, but I'm not ready to dismiss it as "trash" -- I mean, it works! And plenty of people like it. But I can't quite figure out whether this Go assembly stuff is a real feature that makes sense and is well-designed, or if it's just a quirk of the implementation, stemming from its background in Plan 9. The assembly section in the Go docs literally says "go read the Plan 9 docs first". That's kind of weird.
- iainmerrick 9y agoOK, another commenter linked to crc32 in the Go standard library: https://github.com/golang/go/tree/master/src/hash/crc32 https://github.com/golang/go/tree/master/src/hash/crc32 That has different .s files for different architectures, and a .go file with the platform-independent version, so the assembly clearly isn't platform-independent. It seems a strange decision to disguise e.g. arm64 assembly behind an x86-like syntax, so I'm going to go with "quirk of the implementation."
- flavio81 9y agoDoes Go support macro assembler programming? It is a bit harsh to have to program assembler in a fully "write all the instructions" way, particularly in 2017... Additionally, i see that the opcodes are not machine language (target) machine opcodes... It feels a bit ackward to have to manually program into an assembler that is not really targeting the actual CPU; kind of defeats one of the reasons to write in assembler in the first place. As for Go, i always recall this article, which I found interesting: http://cowlark.com/2009-11-15-go/ http://cowlark.com/2009-11-15-go/
- pkaye 9y agoIts a pretty basic assembler built in. Most of the Go library is written in Go itself with assembly for a few critical functions so that is the intended use. You can still use a proper macro assembler for full blown assembly and link Go code against that.
- honzajde 9y agoThanks alot!