Skip to content

Instantly share code, notes, and snippets.

@billglover
Last active September 18, 2018 15:20
Show Gist options
  • Save billglover/9523c457a15bea5cef7201314062cdb7 to your computer and use it in GitHub Desktop.
Save billglover/9523c457a15bea5cef7201314062cdb7 to your computer and use it in GitHub Desktop.
Increment an integer 1000 times (with a function call)
; inc(n int) int
00000 TEXT "".inc(SB), NOSPLIT, $0-16 ; function definition
00000 MOVQ "".n+8(SP), AX ; move parameter from stack to AX
00005 XORL CX, CX ; zero out register CX
00007 JMP 15 ; go to line 15
00009 INCQ CX ; increment register CX
00012 INCQ AX ; increment register AX
00015 CMPQ CX, $1000 ; compare register CX with 1000
00022 JLT 9 ; go to line 9
00024 MOVQ AX, "".~r1+16(SP) ; move AX to stack return value
00029 RET ; return from function
; 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