Last active
January 11, 2017 20:54
-
-
Save byxor/63fa257992f2a3e0643acff14e2ffa5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.586 | |
.model flat,stdcall | |
.stack 4096 | |
option casemap:none | |
include p:\masm32\include\windows.inc | |
include p:\masm32\include\kernel32.inc | |
include p:\masm32\include\user32.inc | |
include p:\masm32\include\msvcrt.inc | |
include p:\masm32\include\ca296.inc | |
includelib p:\masm32\lib\ca296.lib | |
includelib kernel32.lib | |
includelib user32.lib | |
includelib msvcrt.lib | |
.data | |
DWORD_input DWORD 0 | |
STR_fibonacci BYTE 'fib(%d) is %d', 10, 0 | |
.code | |
fibonacci: | |
push ebp | |
mov ebp, esp | |
; Parameters | |
; n -> [ebp + 8] | |
mov eax, [ebp + 8] | |
cmp eax, 1 | |
jle is_base_case | |
jmp is_other | |
is_base_case: | |
mov eax, [ebp + 8] | |
pop ebp | |
ret 4 | |
is_other: | |
; Create local variables (for sub-scope) | |
sub esp, 8 | |
mov eax, 0 | |
mov [ebp - 4], eax ; fib n-1 | |
mov [ebp - 8], eax ; fib n-2 | |
; Calculate fibonacci(n-1) | |
mov eax, [ebp + 8] | |
sub eax, 1 | |
push eax | |
call fibonacci | |
mov [ebp - 4], eax | |
; Calculate fibonacci(n-2) | |
mov eax, [ebp + 8] | |
sub eax, 2 | |
push eax | |
call fibonacci | |
mov [ebp - 8], eax | |
; Add them and put in eax | |
mov eax, [ebp - 4] | |
add eax, [ebp - 8] | |
; Return the result | |
mov esp, ebp | |
pop ebp | |
ret 4 | |
main:nop | |
invoke version | |
start: | |
invoke readInteger | |
mov DWORD_input, eax | |
mov eax, DWORD_input | |
; add 1 to the input because David started | |
; the fibonacci sequence at 1 instead of 0. | |
add eax, 1 | |
push eax | |
call fibonacci | |
invoke crt_printf, addr STR_fibonacci, DWORD_input, eax | |
jmp start | |
fin: | |
invoke ExitProcess,0 | |
end main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment