# About Julia's inline assembly usage

**URL:** https://discourse.julialang.org/t/about-julias-inline-assembly-usage/30352
**Category:** General Usage
**Tags:** llvm
**Created:** [October 27, 2019, 3:48am UTC](https://discourse.julialang.org/t/about-julias-inline-assembly-usage/30352 "2019-10-27T03:48:30Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Sinai\_Pearl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sinai_pearl/32/10057_2.png) [@Sinai\_Pearl](https://discourse.julialang.org/u/Sinai_Pearl)
#### Post date: [October 27, 2019, 3:48am UTC](https://discourse.julialang.org/t/about-julias-inline-assembly-usage/30352/1 "2019-10-27T03:48:30Z")

</div>

I tried to use @asmcall in LLVM.jl. Is there way to copy register’s value to variable in julia code?

---

<div class="post-metadata">

### Author: ![jasonk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jasonk/32/29288_2.png) [@jasonk](https://discourse.julialang.org/u/jasonk)
#### Post date: [September 18, 2021, 2:34pm UTC](https://discourse.julialang.org/t/about-julias-inline-assembly-usage/30352/2 "2021-09-18T14:34:04Z")

</div>

Did you ever get an answer? I’m starting to take an interest in in-line assembly within Julia and the register conventions and figuring out where in either registers or memory variables are at when in-line assembly is called should be documented. Unfortunately, the first Google search result is your query, rather than the answer.

---

<div class="post-metadata">

### Author: ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)
#### Post date: [September 18, 2021, 7:45pm UTC](https://discourse.julialang.org/t/about-julias-inline-assembly-usage/30352/3 "2021-09-18T19:45:31Z")

</div>

If in doubt, first check the tests: [https://github.com/maleadt/LLVM.jl/blob/9155a8c30d6743197728e63c167d078167f34725/test/interop.jl#L87-L93](https://github.com/maleadt/LLVM.jl/blob/9155a8c30d6743197728e63c167d078167f34725/test/interop.jl#L87-L93)

---

<div class="post-metadata">

### Author: ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)
#### Post date: [September 18, 2021, 7:46pm UTC](https://discourse.julialang.org/t/about-julias-inline-assembly-usage/30352/4 "2021-09-18T19:46:38Z")

</div>

I may also have misread the question; if you’re asking, “where did Julia put variable `x`”, I don’t think there’s an easy or reliable way to figure that out.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [September 18, 2021, 8:36pm UTC](https://discourse.julialang.org/t/about-julias-inline-assembly-usage/30352/5 "2021-09-18T20:36:14Z")

</div>

I’d just use `Base.llvmcall`, e.g.

```julia
const _Vec{W,T} = NTuple{W,Core.VecElement{T}}
# requires AVX2
@generated function vfmadd231(a::_Vec{4,Float64}, b::_Vec{4,Float64}, c::_Vec{4,Float64})
    vfmadd_str = """%res = call <4 x double> asm "vfmadd231pd \$3, \$2, \$1", "=v,0,v,v"(<4 x double> %2, <4 x double> %1, <4 x double> %0)
                        ret <4 x double> %res"""
    quote
        $(Expr(:meta, :inline))
        Base.llvmcall($vfmadd_str, _Vec{4,Float64}, Tuple{_Vec{4,Float64},_Vec{4,Float64},_Vec{4,Float64}}, a, b, c)
    end
end

```

The `call` syntax returns an llvm variable, that you can then return as a Julia variable.

However, why do you need asm call?  
Normally, LLVM also provides target specific intrinsic functions matching whatever you need.  
So what I do is look through the appropriate [llvm tests](https://github.com/llvm/llvm-project/blob/main/llvm/test/CodeGen/X86/avx-intrinsics-x86.ll#L620) to find the instruction and calling syntax I need, and then avoid using asm.  
It’s still target specific, but easier to work with.

---

<div class="post-metadata">

### Author: ![jasonk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jasonk/32/29288_2.png) [@jasonk](https://discourse.julialang.org/u/jasonk)
#### Post date: [October 12, 2021, 6:42pm UTC](https://discourse.julialang.org/t/about-julias-inline-assembly-usage/30352/6 "2021-10-12T18:42:32Z")

</div>

For me, it is more academic than anything–teaching someone how functions are implemented in assembly. I think understanding how compilers use the stack and registers is important to understanding how computers work.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [October 12, 2021, 8:02pm UTC](https://discourse.julialang.org/t/about-julias-inline-assembly-usage/30352/7 "2021-10-12T20:02:15Z")

</div>

For that I think that `@code_lowered`, `@code_typed`, `@code_llvm` and `@code_native` are really amazing tools that let you see how high-level code gets translated into successively lower level forms, ending with assembly code. I’m not sure that writing inline assembly is as helpful for teaching.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [October 12, 2021, 8:06pm UTC](https://discourse.julialang.org/t/about-julias-inline-assembly-usage/30352/8 "2021-10-12T20:06:02Z")

</div>

> [@StefanKarpinski](#):
>
> For that I think that `@code_lowered` , `@code_typed` , `@code_llvm` and `@code_native` are really amazing tools that let you see how high-level code gets translated into successively lower level forms, ending with assembly code. I’m not sure that writing inline assembly is as helpful for teaching.

These (and later [Cthulhu](https://github.com/JuliaDebug/Cthulhu.jl)) were invaluable for me.
