eval.in

Paste #6666

x86 Assembly — NASM 2.07, pasted 4 years ago

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
extern printf
global main

; int fib(int n)
fib:
  ; save caller registers
  push ebx
  push ecx
  ; eax = n
  mov eax, [esp+12]
  ; if eax < 2 goto lt_2
  cmp eax, 2
  jl .lt_2
  ; ebx = eax - 1
  lea ebx, [eax-1]
  ; fib(ebx)
  push ebx
  call fib
  add esp, 4
  ; ecx = eax
  mov ecx, eax
  ; fib(n - 2)
  dec ebx
  push ebx
  call fib
  add esp, 4
  ; eax += ecx
  add eax, ecx
.lt_2:
  ; restore caller registers
  pop ecx
  pop ebx
  ret

main:
  push dword 30
  call fib
  add esp, 4
  push eax
  push fmt
  call printf
  add esp, 8
  xor eax, eax
  ret

fmt db "%d", 10, 0

Program Output

832040

OK (0.008 sec real, 0.006 sec wall, 2 MB, 26 syscalls)

Fork