loop & CX register

132 views
Skip to first unread message

Vincent Blanchon

unread,
Jan 1, 2020, 10:24:44 PM1/1/20
to golang-nuts
Hi,

Here is a loop in Go: https://play.golang.org/p/G4wdLi26LZ4
With looking at the assembly, I can see that the loop counter uses AX register while the total (t) use the CX register:

0x004c 00076 (main.go:7) INCQ AX
0x004f 00079 (main.go:8) ADDQ DX, CX

I have a basic knowledge of assembly, but out of curiosity, why does the loop counter not use the counter register CX? Isn't it the purpose of it?
And should not "t" use AX that looks more designed for that?

I guess since they are historical registers and have a general-purpose, it does not matter the way the language uses them?

Ian Lance Taylor

unread,
Jan 1, 2020, 10:36:39 PM1/1/20
to Vincent Blanchon, golang-nuts
On modern x86 processors the various registers are essentially
interchangeable, except for a few special purpose instructions.

Ian

Vincent Blanchon

unread,
Jan 2, 2020, 12:03:12 AM1/2/20
to golang-nuts
I got it, Thank you Ian!
Reply all
Reply to author
Forward
0 new messages