My Golang source code is as follows.
package main
func add(x, y int) int {
return x + y
}
func main() {
_ = add(1, 2)
}
The assembly code I obtained using go tool compile -N -l -S main.go > file1.s
is as follows(part of it).
;file1.s
"".main STEXT size=54 args=0x0 locals=0x18 funcid=0x0
0x0000 00000 (main.go:7) TEXT "".main(SB), ABIInternal, $24-0
0x0000 00000 (main.go:7) CMPQ SP, 16(R14)
0x0004 00004 (main.go:7) PCDATA $0, $-2
0x0004 00004 (main.go:7) JLS 47
……
0x002f 00047 (main.go:7) CALL runtime.morestack_noctxt(SB)
0x0034 00052 (main.go:7) PCDATA $0, $-1
0x0034 00052 (main.go:7) JMP 0
And the assembly code I obtained using go tool compile -N -l main.go
and go tool objdump -S -gnu main.o > file2.s
is as follows(part of it).
;file2.s
TEXT "".main(SB) gofile..D:/code/Test/025_go/007_ass/main.go
func main() {
0x5b6 493b6610 CMPQ 0x10(R14), SP // cmp 0x10(%r14),%rsp
0x5ba 7629 JBE 0x5e5 // jbe 0x5e5
……
func main() {
0x5e5 e800000000 CALL 0x5ea // callq 0x5ea [1:5]R_CALL:runtime.morestack_noctxt
0x5ea ebca JMP "".main(SB) // jmp 0x5b6
My questions are:
- Why are the source and destination of the
CMPQ
instructions in file1.s and file2.s opposite, as inCMPQ SP, 16(R14)
vsCMPQ 0x10(R14), SP
? - For the above two code, my understanding is: when
SP <= R14 + 16
, callruntime.morestack_noctxt
to extend stack. But what I don't understand is: why isSP <= R14 + 16
, what is the logic behind? R14 is link register? - Is the code in file2.s a dead loop? Why is it so? Why is the code in file1.s not a dead loop?
- What is the meaning of
[1:5]
in[1:5]R_CALL:runtime.morestack_noctxt
in file2.s?
I have a basic knowledge of c++/golang as well as assembly, and I understand the memory layout of programs, but I am really confused about the above questions. Can anyone help me, or what material should I read?
Thank you to everyone who helps me.
op src, dst
.cmp
can usefully be used with operands in either order, depending on how you want FLAGS to be set.cmp reg,mem
/jbe
jumps ifmem <= reg
(unsigned). The semantic meaning withbe
=<=
is backwards because it uses an operand-order other than Intel's. Like other ALU instructions that data back to 8086, it has two opcodes for each operand-size,cmp r/m, r
andcmp r, r/m
. felixcloutier.com/x86/cmp. Is any of that what you're asking? – Stearin