Skip to content

Instantly share code, notes, and snippets.

@masatoi
Created April 10, 2021 09:02
Show Gist options
  • Save masatoi/8b777fe8486b78e7227181e08cf12254 to your computer and use it in GitHub Desktop.
Save masatoi/8b777fe8486b78e7227181e08cf12254 to your computer and use it in GitHub Desktop.
;;; 1.2 手続きとそれが生成するプロセス
;;; 1.2.1 線形再帰と反復
(defun factorial (n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
(disassemble 'factorial)
; disassembly for FACTORIAL
; Size: 90 bytes. Origin: #x52BA5711 ; FACTORIAL
; 11: 498B7510 MOV RSI, [R13+16] ; thread.binding-stack-pointer
; 15: 488975F8 MOV [RBP-8], RSI
; 19: 488B55F0 MOV RDX, [RBP-16]
; 1D: BF02000000 MOV EDI, 2
; 22: E8B9C145FF CALL #x520018E0 ; GENERIC-=
; 27: 7439 JEQ L1
; 29: 488B55F0 MOV RDX, [RBP-16]
; 2D: BF02000000 MOV EDI, 2
; 32: E8D9BF45FF CALL #x52001710 ; GENERIC--
; 37: 4883EC10 SUB RSP, 16
; 3B: B902000000 MOV ECX, 2
; 40: 48892C24 MOV [RSP], RBP
; 44: 488BEC MOV RBP, RSP
; 47: E8F6049DFD CALL #x50575C42 ; #<FDEFN FACTORIAL>
; 4C: 480F42E3 CMOVB RSP, RBX
; 50: 488BFA MOV RDI, RDX
; 53: 488B55F0 MOV RDX, [RBP-16]
; 57: E814C045FF CALL #x52001770 ; GENERIC-*
; 5C: L0: 488BE5 MOV RSP, RBP
; 5F: F8 CLC
; 60: 5D POP RBP
; 61: C3 RET
; 62: L1: BA02000000 MOV EDX, 2
; 67: EBF3 JMP L0
; 69: CC10 INT3 16 ; Invalid argument count trap
(defun factorial-loop (n)
(labels ((iter (product counter)
(if (> counter n)
product
(iter (* counter product)
(+ counter 1)))))
(iter 1 1)))
(disassemble 'factorial-loop)
; disassembly for FACTORIAL-LOOP
; Size: 119 bytes. Origin: #x52B89948 ; FACTORIAL-LOOP
; 48: 498B5D10 MOV RBX, [R13+16] ; thread.binding-stack-pointer
; 4C: 48895DF8 MOV [RBP-8], RBX
; 50: BE02000000 MOV ESI, 2
; 55: 488BDE MOV RBX, RSI
; 58: 0F1F840000000000 NOP
; 60: L0: 488975E8 MOV [RBP-24], RSI
; 64: 48895DE0 MOV [RBP-32], RBX
; 68: 488B7DF0 MOV RDI, [RBP-16]
; 6C: 488BD3 MOV RDX, RBX
; 6F: FF142520010052 CALL QWORD PTR [#x52000120] ; GENERIC->
; 76: 488B5DE0 MOV RBX, [RBP-32]
; 7A: 488B75E8 MOV RSI, [RBP-24]
; 7E: 7F34 JNLE L1
; 80: 48895DE0 MOV [RBP-32], RBX
; 84: 488BD3 MOV RDX, RBX
; 87: 488BFE MOV RDI, RSI
; 8A: FF142508010052 CALL QWORD PTR [#x52000108] ; GENERIC-*
; 91: 488BF2 MOV RSI, RDX
; 94: 488975E8 MOV [RBP-24], RSI
; 98: 488B5DE0 MOV RBX, [RBP-32]
; 9C: BF02000000 MOV EDI, 2
; A1: 488BD3 MOV RDX, RBX
; A4: FF1425F8000052 CALL QWORD PTR [#x520000F8] ; GENERIC-+
; AB: 488B75E8 MOV RSI, [RBP-24]
; AF: 488BDA MOV RBX, RDX
; B2: EBAC JMP L0
; B4: L1: 488BD6 MOV RDX, RSI
; B7: 488BE5 MOV RSP, RBP
; BA: F8 CLC
; BB: 5D POP RBP
; BC: C3 RET
; BD: CC10 INT3 16 ; Invalid argument count trap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment