Skip to content

Instantly share code, notes, and snippets.

@billglover
Last active September 18, 2018 15:25
Show Gist options
  • Save billglover/fc895b912314efa90a756d93f4827d94 to your computer and use it in GitHub Desktop.
Save billglover/fc895b912314efa90a756d93f4827d94 to your computer and use it in GitHub Desktop.
Increment an integer 1000 times (with a function call and no inlining)
; inc(n int) int
00000 TEXT "".inc(SB), $32-16 ; function definition
00000 MOVQ (TLS), CX ; move TLS to register CX
00009 CMPQ SP, 16(CX) ; compare SP with part of CX
00013 JLS 88 ; go to line 88 if 'less than'
00015 SUBQ $32, SP ; increase stack size by 32-bytes
00019 MOVQ BP, 24(SP) ; move BP to the stack
00024 LEAQ 24(SP), BP ; update BP to new location
; --- ---
00029 XORL AX, AX ; zero out register AX
00031 MOVQ "".n+40(SP), CX ; move parameter from stack to CX
00036 JMP 65 ; go to line 65
00038 MOVQ AX, "".i+16(SP) ; move AX to the stack
00043 MOVQ CX, (SP) ; move CX to the stack
00047 CALL "".add(SB) ; call the add(int) function
00052 MOVQ "".i+16(SP), AX ; move stack value to AX
00057 INCQ AX ; increment AX
00060 MOVQ 8(SP), CX ; move stack value to CX
00065 CMPQ AX, $1000 ; compare register AX with 1000
00071 JLT 38 ; go to line 38
00073 MOVQ CX, "".~r1+48(SP) ; move CX to stack return value
00078 MOVQ 24(SP), BP ; restore BP from the stack
00083 ADDQ $32, SP ; reduce stack size by 32-bytes
00087 RET ; return from function
; --- ---
00088 NOP ; do nothing
00088 CALL runtime.morestack_noctxt(SB) ; ask runtime for more stack
00093 JMP 0 ; go to line 0
; add(n int) int
00000 TEXT "".add(SB), NOSPLIT, $0-16 ; function definition
00000 MOVQ "".n+8(SP), AX ; move parameter from stack to AX
00005 INCQ AX ; increment register AX
00008 MOVQ AX, "".~r1+16(SP) ; move AX to stack return value
00013 RET ; return from function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment