-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibo.asm
More file actions
141 lines (126 loc) · 4.57 KB
/
Copy pathfibo.asm
File metadata and controls
141 lines (126 loc) · 4.57 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
; #############################################################################
; ##################### licensing stuff : nBSDL ##############################
; Copyright (c) 2013, 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://cogsandlevers.blogspot.fr/2013/01/printing-registers-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
; ld -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
a: dq 1; first step equal 1
b: dq 1; second step equal 1
table db '0123456789'
section .bss
output resb 256
section .text
; the special code to transform the content of rax into integer
_qw2a:
lea r10 , [rsp-1]
xor rcx,rcx
mov r8,10
_L0:
xor rdx,rdx
div r8
mov r9b,[table+rdx]
mov [r10],r9b
dec r10
inc rcx
test rax,rax
jnz _L0
lea rsi,[r10+1]
cld
_L1:
movsb
cmp rsi,rsp
jne _L1
ret
; end of this code
global _start
_start:
nop
sys_write 1,Stmsg,StmsgLen; writing starting msg
; initialisation of first two steps.
xor r11,r11 ; r11 = 0
xor r12,r12 ; r12 = 0
mov r11,[a] ; r11 =1
mov r12,[b] ; r12 =1
xor r13,r13 ; initialising counter
Fibo:
add r11,r12 ; r11=r11+r12
push r11 ; saving r11
push r12 ; saving r12
mov rax, r11 ; loading value of r11 into rax
mov rdi, output; loading address of output to rdi
call _qw2a ; calling the reg2dec function
mov byte[rcx+output],0xa; adding \n at the end of buffer
inc rcx ; incrementing the size of the value to consider the \n
sys_write 1,output,rcx; call the sys_write macro
pop r12 ; restoring r12
pop r11; restoring r11
; almost same as above we compute two numbers for each round in loop
add r12,r11; r12=r11+r12 (r11 already have the new value from last add)
push r11 ; saving r11
push r12 ; saving r12
mov rax,r12 ; loading value of r12 to rax
mov rdi, output ; loading buffer address into rdi
call _qw2a; calling the reg2dec
mov byte[rcx+output],0xa; adding \n for pretty printing
inc rcx; incrementing size value
sys_write 1,output,rcx; printing
pop r12; restoring r12
pop r11; restoring r11
inc r13 ; incrementing general counter
cmp r13,45; compare is < to 45
jna Fibo; if so then loop to Fibo
sys_write 1,Edmsg,EdmsgLen; printing end msg
Done:
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