交换两个数字golang [英] Swap two numbers golang

查看:170
本文介绍了交换两个数字golang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试着去理解内部的东西。考虑下面的代码:

  a,b:= 10,5 
b,a = a,b

上面的代码完全交换了2个数字,a变成了5,b变成了10.我无法理解这是如何工作的。考虑到代码的第二行,如果a先被分配给b,那么b将为10.现在,如果我们将b赋值给a,那么也不应该是10。



请帮我理解这是如何工作的



感谢

解决方案

TL; DR :反汇编表明CPU必须足够聪明才能看到发生了什么,并使用寄存器来避免覆盖存储器中的现有值。




这个问题帮助我了解了更多关于Golang的知识,所以谢谢!



要弄清楚编译器生成本地代码,我们需要看看它生成的汇编代码,它由链接器转变为机器代码。



我写了一个Go程序来帮助这:

  package main 

importfmt

func main (){
fmt.Println(myfunction())
}

func myfunction()[] int {
a,b:= 10,5
b,a = a,b
return [] int {a,b}
}

使用 go tool compile -S> swap.s ,然后 CTRL-F '用于 myfunction (这是该名称的关键点:轻松搜索),并找到这四行,它们对应于Go代码中的 myfunction 的前两行:(注意这是用于我的64位机器;输出将会不同于其他架构,如32位)

  0x0028 00040(swap.go:10)MOVQ $ 10,CX; var a = 10 
0x002f 00047(swap.go:10)MOVQ $ 5,AX; var b = 5
0x0036 00054(swap.go:11)MOVQ CX,.b + 16(SP);复制a到* b + 16
0x003b 00059(swap.go:11)MOVQ AX,.a + 24(SP);复制b到* a + 24

Go的反汇编对调试非常有用:D



查看 Golang关于asm的文档,我们可以看到汇编程序当程序运行时,CPU足够聪明,可以看到发生了什么,并使用一个寄存器来避免覆盖现有的值。

>

如果您有兴趣,请完整反汇编


I am trying to understand the internals of go. Consider the following code

a,b := 10,5
b,a = a,b

The above code swaps 2 number perfectly and a becomes 5 and b becomes 10. I am not able to understand how this works. Considering in the second line of code, if a is assigned to b first, then b would be 10. Now if we assign b to a then shouldn't a be 10 too.

Please help me understand how this works

Thanks

解决方案

TL;DR: The disassembly shows that the CPU must be smart enough to see what's happening and use a register to avoid overwriting the existing value in memory.


This question helped me learn a little more about Golang, so thank you!

To figure out how the compiler makes native code, we need to look at the assembly code it generates, which is turned into machine code by the linker.

I wrote a little Go program to help with this:

package main

import "fmt"

func main() {
    fmt.Println(myfunction())
}

func myfunction() []int {
    a, b := 10, 5
    b, a = a, b
    return []int{a, b}
}

Using go tool compile -S > swap.s, I then CTRL - F'd for myfunction (which was the point of that name: easily searchable), and found these four lines, which correspond to the first two lines of myfunction in the Go code: (note this is for my 64-bit machine; the output will differ on other architechtures like 32-bit)

0x0028 00040 (swap.go:10)   MOVQ    $10, CX         ; var a = 10
0x002f 00047 (swap.go:10)   MOVQ    $5, AX          ; var b = 5
0x0036 00054 (swap.go:11)   MOVQ    CX, "".b+16(SP) ; copy a to *b+16
0x003b 00059 (swap.go:11)   MOVQ    AX, "".a+24(SP) ; copy b to *a+24 

Go's disassembly is so helpful to debugging :D

Looking at the Golang docs on asm, we can see the assembler uses indirection to juggle the values.

When the program runs, the CPU is smart enough to see what's happening and use a register to avoid overwriting the existing value.

Here's the full disassembly, if you're interested.

这篇关于交换两个数字golang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆