Last active
October 16, 2022 22:44
-
-
Save Iainmon/cf997c9e3c17d7d233b1c952bd0f3ea9 to your computer and use it in GitHub Desktop.
Fibonacci numbers in MASM
This file contains hidden or 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
INCLUDE Irvine32.inc | |
; #define MAX_ITER 45 | |
; int main() { | |
; int n; | |
; int m; | |
; int tmp; | |
; int i; | |
; i = 0; | |
; n = 0; | |
; m = 1; | |
; while (i < MAX_ITER) { | |
; printf("%d\n",n); | |
; tmp = m; | |
; m += n; | |
; n = tmp; | |
; ++i; | |
; } | |
; } | |
MAX_ITER = 45 | |
.data | |
n DWORD 0 | |
m DWORD 1 | |
tmp DWORD 0 | |
i BYTE 0 | |
.code | |
main PROC | |
CMP i, MAX_ITER | |
JGE pau | |
; printf("%d\n", n); | |
mov eax, n | |
call WriteDec | |
call CrLf | |
; tmp = m; | |
mov eax, m | |
mov tmp, eax | |
; m += n; | |
mov eax, n | |
add m, eax | |
; n = tmp | |
mov eax, tmp | |
mov n, eax | |
inc i | |
jmp main | |
pau: | |
invoke ExitProcess, 0 | |
main ENDP | |
END main |
This file contains hidden or 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
INCLUDE Irvine32.inc | |
; #define MAX_ITER 45 | |
; int main() { | |
; int n; | |
; int m; | |
; int tmp; | |
; int i; | |
; i = 0; | |
; n = 0; | |
; m = 1; | |
; while (i < MAX_ITER) { | |
; printf("fib %d -> %d\n",i,n); | |
; tmp = m; | |
; m += n; | |
; n = tmp; | |
; ++i; | |
; } | |
; } | |
MAX_ITER = 45 | |
.data | |
fibName BYTE "fib ",0 | |
arrow BYTE " -> ",0 | |
n DWORD 0 | |
m DWORD 1 | |
tmp DWORD 0 | |
i DWORD 0 | |
.code | |
main PROC | |
CMP i, MAX_ITER | |
JGE pau | |
; printf("fib %d -> %d\n", n); | |
mov edx, offset fibName | |
call WriteString ; write "fib " | |
mov eax, i | |
call WriteDec ; write "{i}" | |
mov edx, offset arrow | |
call WriteString ; write " -> " | |
mov eax, n | |
call WriteDec ; write "{n}" | |
call CrLf | |
; tmp = m; | |
mov eax, m | |
mov tmp, eax | |
; m += n; | |
mov eax, n | |
add m, eax | |
; n = tmp | |
mov eax, tmp | |
mov n, eax | |
inc i | |
jmp main | |
pau: | |
invoke ExitProcess, 0 | |
main ENDP | |
END main | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment