
A Foray into Go Assembly Programming - sgmansfield
https://blog.sgmansfield.com/2017/04/a-foray-into-go-assembly-programming/
======
bkeroack
Here's a good talk about the Go assembler backend:
[https://www.youtube.com/watch?v=KINIAgRpkDA](https://www.youtube.com/watch?v=KINIAgRpkDA)

Spoiler: they're nearly to the point (maybe now they've achieved it fully) of
autogenerating a new language backend from ISA documentation PDFs! Bring that
up next time someone complains about Go not using LLVM.

~~~
shouldbworking
Does this make targeting new ISA's trivial?

~~~
AstralStorm
Not really, writing a good enough formal ISA spec is quite hard. More so if
the architecture is quirky.

------
Marat_Dukhan
PeachPy, an x86-64 assembler in Python, is often used to target Golang
assembler without dealing with its weird syntax. PeachPy uses Intel assembly
syntax, but can generate assembly listing for Golang toolchain as its output.
Damian Gryski wrote a tutorial about using PeachPy for Golang assembly:
[https://blog.gopheracademy.com/advent-2016/peachpy/](https://blog.gopheracademy.com/advent-2016/peachpy/)

Disclosure: I'm the author of PeachPy

------
webhat
"I literally copy-paste the middot every time. Who has a middot key?"

I did too, until I found.

[http://middot.net/](http://middot.net/)

~~~
zeveb
As that site indicates, in X.org with the defaults, '·' is easily typed with
'Compose . -', 'Compose . ^' or 'Compose ^ .' (this should also work from the
console in Debian, at least, which uses xkb bindings throughout).

I _love_ the Compose key, and don't think I'll ever use a desktop without it.

------
dlsniper
Excellent read about Go Assembly and how to use it. I'd love to see more on
this, especially since assembly isn't something that often used, but after
reading your post I can appreciate the overhead of having to maintain it in
the future.

Thank you for the time put in the post about this.

------
Animats
If writing assembly code to run in the Go environment, and you're changing
pointers, you have to write garbage-collector aware code.[1][2] Go has
concurrent garbage collection, which means there are some unusual constraints
on code that manipulates pointers.

This is an area where proofs of correctness are considered necessary, because
intuition fails. See [3]

[1] [https://blog.golang.org/go15gc](https://blog.golang.org/go15gc) [2]
[https://golang.org/src/runtime/mbarrier.go](https://golang.org/src/runtime/mbarrier.go)
[3]
[https://github.com/golang/proposal/blob/master/design/17503-...](https://github.com/golang/proposal/blob/master/design/17503-eliminate-
rescan.md)

------
Karliss
Tried writing a version with no divisions. At least in my tests it was ~5x
faster. Correctness not guaranteed.
[https://gist.github.com/karliss/6cc343f7359c2de9166f1551f884...](https://gist.github.com/karliss/6cc343f7359c2de9166f1551f88482d9)

------
rcarmo
FYI, blog layout breaks down completely when trying to zoom text on an iPad,
making it impossible to read.

~~~
sgmansfield
Thanks for letting me know, I'll see if I can fix it. If I could trouble you
to do so, can you provide a screenshot?

~~~
shurcooL
I'm not the OP, but I have an iPad too, and I can provide screenshots.

[http://instantshare.virtivia.com:27080/1eikl92n5jtyc.PNG](http://instantshare.virtivia.com:27080/1eikl92n5jtyc.PNG)
\- default view

[http://instantshare.virtivia.com:27080/o1wn6nbthvuu.PNG](http://instantshare.virtivia.com:27080/o1wn6nbthvuu.PNG)
\- after zooming in unsuccessfully, OR just scrolling to the right

[http://instantshare.virtivia.com:27080/eiw3z2u1n52h.PNG](http://instantshare.virtivia.com:27080/eiw3z2u1n52h.PNG)
\- fully zoomed out

[http://instantshare.virtivia.com:27080/1jsmbx8km815h.PNG](http://instantshare.virtivia.com:27080/1jsmbx8km815h.PNG)
\- the culprit causing this, a long URL which isn't being wrapped by your CSS
rules

To be honest, the same issue happens on desktop browsers, it's just not as
noticeable because the default zoom kinda matches what you'd expect. But you
can still scroll to the side, in a desktop browser. See:

[http://instantshare.virtivia.com:27080/b69zdjyzbvfo.png](http://instantshare.virtivia.com:27080/b69zdjyzbvfo.png)

I think the CSS fix can be something like this:

    
    
        word-wrap: break-word;
    

[http://instantshare.virtivia.com:27080/1l8rbeisu9a2a.png](http://instantshare.virtivia.com:27080/1l8rbeisu9a2a.png)

However, different browsers behave differently™, so you may need to also add:

    
    
        word-wrap: break-word;
        white-space: pre-wrap;
    

To make it work in Firefox (but you might need to adjust other things, also
play with word-break CSS property, and test in all browsers). I've dealt with
this previously at
[https://github.com/shurcooL/play/issues/4](https://github.com/shurcooL/play/issues/4)
and other places that deal with displaying user generated content.

------
libeclipse
> In this case, a divide by 3 is equivalent to multiplying by 0x55555556 and
> then taking the top half of the output.

What the fuck. That's amazing!

~~~
chris_overseas
In more general terms, take a look at Montgomery multiplication[1] which is
where this trick originates. It's a common technique used by compilers to
optimise away divisions.

[1]
[https://en.wikipedia.org/wiki/Montgomery_modular_multiplicat...](https://en.wikipedia.org/wiki/Montgomery_modular_multiplication)

------
kristianp
Couldn't he have compiled his go code with assembly output and edited the
resulting file? Wouldn't that have saved a lot of struggle with the format?

go tool compile -S -S file.go

~~~
sgmansfield
Sure, but that ruins the fun :)

------
andreiw
Wait, so the Go assembler allows generating invalid opcodes?

Any way of using a more normal assembler with a non-GNU Go compiler?

~~~
sgmansfield
This was probably not explained well. The compilation failed with an error
saying that I was basically trying to do something that was not allowed.

For your second question: I don't believe there is a way to use a separate
assembler.

