How to understand runtime.gogo?

204 views
Skip to first unread message

j2gg0s

unread,
Jan 16, 2023, 11:43:22 AM1/16/23
to golang-nuts
As a newbie of golang's assembly, i cant understand why we MOVQ DX CX twice in runtime.gogo. WHY?

Source code:
```
   255  // func gogo(buf *gobuf)
   256  // restore state from Gobuf; longjmp
   257  TEXT runtime·gogo(SB), NOSPLIT, $0-8
   258          MOVQ    buf+0(FP), BX           // gobuf
   259          MOVQ    gobuf_g(BX), DX
   260          MOVQ    0(DX), CX               // make sure g != nil
   261          JMP     gogo<>(SB)
   262
   263  TEXT gogo<>(SB), NOSPLIT, $0
   264          get_tls(CX)
   265          MOVQ    DX, g(CX)
   266          MOVQ    DX, R14         // set the g register
   267          MOVQ    gobuf_sp(BX), SP        // restore SP
   268          MOVQ    gobuf_ret(BX), AX
   269          MOVQ    gobuf_ctxt(BX), DX
   270          MOVQ    gobuf_bp(BX), BP
   271          MOVQ    $0, gobuf_sp(BX)        // clear to help garbage collector
   272          MOVQ    $0, gobuf_ret(BX)
   273          MOVQ    $0, gobuf_ctxt(BX)
   274          MOVQ    $0, gobuf_bp(BX)
   275          MOVQ    gobuf_pc(BX), BX
   276          JMP     BX
```



Ian Lance Taylor

unread,
Jan 16, 2023, 11:49:03 AM1/16/23
to j2gg0s, golang-nuts
On Mon, Jan 16, 2023 at 8:43 AM j2gg0s <feys...@gmail.com> wrote:
>
> As a newbie of golang's assembly, i cant understand why we MOVQ DX CX twice in runtime.gogo. WHY?

I don't see any MOVQ DX, CX instructions here. Can you clarify by
saying exactly which instructions you are asking about?

Note that MOVQ 0(DX), CX treats DX as a memory address, and moves the
8-byte value at that address into CX. And MOVQ DX, g(CX) treats CX as
a memory address, and moves the value in DX into the address as "g"
bytes offset from the address in CX.

Ian
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5fafeb38-1bc7-4a96-b386-41e14c431273n%40googlegroups.com.

j2gg0s

unread,
Jan 18, 2023, 11:22:59 AM1/18/23
to golang-nuts
Thanks, solved my problem
Reply all
Reply to author
Forward
0 new messages