-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewrout.asm
174 lines (163 loc) · 6.86 KB
/
newrout.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
; 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
bindectable db 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 6,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 4,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 8,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 6,5,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 2,1,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 4,2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 8,4,0,2,0,0,0,0,0,0,0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,6,0,6,4,0,5,1,2,9,2,5,1,1,0
db 2,5,9,3,9,6,3,1,2,9,0,0,3,4,8,5,0,3,2,0
db 4,0,9,7,8,3,7,2,4,8,1,0,6,8,6,1,1,6,4,0
db 8,0,8,5,7,7,4,5,8,6,3,0,2,7,3,3,2,2,9,0
result db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
section .text
printbin2dec:
xor rcx, rcx; initialising counter to 0 going to 63 (0..63=64 bits)
xor r10b,r10b; carry value
xor rbx,rbx; initialising rbx which is the offset of byte in bindectable
xor r8b, r8b; initialising r8b for working
xor r9b, r9b; initialising r9b for working
begin:
xor rdx,rdx; setting rdx to 0 (counter to travel into result)
cmp rcx, 64 ; if general counter=64 then finish the whole work
je end; jump to the end for return
shr rax,1; shift to right one digit
jnc noop; if the carry flag is equal 0 nothing to do
addbig: ; else we have to add content of the related line of bindectable to result
mov r8b,[result+rdx]; loading into r8b the byte of result we manipulate
mov r9b,[bindectable+rbx]; loading into r9b the first byte of the line of bindectable
add r8b,r9b; add the value to r8b
add r8b,r10b; add the carry to r8b
cmp r8b,10 ; test is result >10
jb nocarry; if not
mov r10b,1; set the carry
sub r8b,10; subtract 10 to value to get the last digit
mov [result+rdx],r8b ; storing it to the good place in result
jmp continue; jump to end of loop
nocarry:
mov r10b,0 ;setting back the carry value to zero
mov [result+rdx], r8b; storing the byte into result
jmp continue; jump to continue
continue: ; ending part of the loop
inc rdx; increasing the offset of result
inc rbx; increasing the offset of bindectable rbx
cmp rdx,20; compare it to 20(is it end of result line)
je begin; if so , jump to begin for processing next general digit
jmp addbig; else jump to process the next byte in the line
noop:
inc rcx; in case the carry flag is equal 0 nothing to do, so
add rbx, 20; increase the general counter, and the offset of bindectable
jmp begin; and reentering into the loop
end:
ret
;; loop1 is used to transform the content of result into ascii
;; if a byte has the "1" value, the ascii code is 48+1=49
;; so we just need to add 48 to all byte of result and adding a \n at the end
;; total size is 21 chars.
loop1:
xor rcx, rcx
loop4:
add [result+rcx], byte 48
inc rcx
cmp rcx, 20
jnae loop4
mov byte[result+rcx],0xa
inc rcx
sys_write 1, result,rcx
ret
;; Init result is used to reinitiate result to '0' byte value
;; just a little loop of 20
initresult:
xor rcx,rcx
loop3:
mov byte[result+rcx],0x0
inc rcx
cmp rcx,20
jnae loop3
ret
;; start point of the program
;;
global _start
_start:
sys_write 1,Stmsg,StmsgLen; writing starting msg
xor rax, rax
xor r15, r15
inc r15
call printbin2dec
loop2:
mov rax, r15
call printbin2dec
call loop1
call initresult
shl r15,1
inc r15
jc Done
jmp loop2
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