-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibo_printf.asm
96 lines (86 loc) · 3.31 KB
/
fibo_printf.asm
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
; #############################################################################
; ##################### licensing stuff : nBSDL ##############################
; Copyright (c) 2016, netmonk.org
; All rights reserved.
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;
; Redistributions of source code must retain the above copyright notice, this
; list of conditions and the following disclaimer.
; Redistributions in binary form must reproduce the above copyright notice,
; this list of conditions and the following disclaimer in the documentation
; and/or other materials provided with the distribution.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
; THE POSSIBILITY OF SUCH DAMAGE.
; #############################################################################
; Im starting to play with nasm on 64bits linux
; this is just a working code which will compute the
; 100 firsts round of fibonaci starting with (1,1)
; The main difficulties is not to compute the number
; its about displaying them
; I find an elegant way there :
; http://tuttlem.github.io/2013/01/08/printing-a-register-s-value-in-hex.html
; also a dude (tefter) from #asm@freenode
; provided a nice version to print in decimal way !
;
; this code is for linux x86_64 only:
; how to compile :
; nasm -f elf64 -g -F stabs -o fibo.o fibo.asm
; gcc -o fibo fibo.o
;./fibo
;
; Courtesy of Netmonk, netmonk at netmonk dot org
; http://www.netmonk.org
; Defining syswrite macro
%macro sys_write 3
mov eax,1
mov rdi, %1
mov rsi, %2
mov rdx, %3
syscall
%endmacro
section .data
Stmsg: db "Starting computation",10 ; Starting msg
StmsgLen equ $-Stmsg ; length of Stmsg
Edmsg: db "Ending computation",10 ; Ending msg
EdmsgLen equ $-Edmsg ; length of Edmsg
fmt: db "%ld", 10,0
extern printf
global main
main:
nop
sys_write 1,Stmsg,StmsgLen; writing starting msg
xor r14,r14 ; r14 = 0
xor r12,r12 ; r12 = 0
inc r12; r12=0+1=1
Fibo:
add r14,r12 ; r14=r14+r12
jc Done; if overflow (CF=1) then jump to end
push rbp
mov rsi,r14
mov rdi,fmt
xor rax,rax
call printf
add r12,r14; r12=r14+r12 (r14 already have the new value from last add)
jc Done; if overflow (CF=1) then jump to end
push rbp
mov rsi,r12
mov rdi,fmt
xor rax,rax
call printf
jmp Fibo;
Done:
sys_write 1,Edmsg,EdmsgLen; printing end msg
mov rax,60 ; load the 60 (exit) syscall value into rax
mov rdi,0 ; load into rdi the return value of exit code
syscall; call the kernel syscall