-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10power.asm
82 lines (77 loc) · 1.63 KB
/
10power.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
; Defining syswrite macro
%macro sys_write 3
mov rdi, %1
mov rsi, %2
mov rdx, %3
mov eax,1
syscall
%endmacro
section .data
Stmsg: db "Starting computation",10 ; Starting msg
StmsgLen equ $-Stmsg ; length of Stmsg
bindectable:
dq 10
dq 100
dq 1000
dq 10000
dq 100000
dq 1000000
dq 10000000
dq 100000000
dq 1000000000
dq 10000000000
dq 100000000000
dq 1000000000000
dq 10000000000000
dq 100000000000000
dq 1000000000000000
dq 10000000000000000
dq 100000000000000000
dq 1000000000000000000
dq 10000000000000000000
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:
mov esi, 18 ; 19 powers of 10
mov edi, 4 ; optimize for < 1000000
cmp rax, [bindectable+rdi*8+8]
cmovb esi, edi
xor edx, edx ; output index
.nextdigit:
mov cl, -1
mov rdi, [bindectable+rsi*8]
.loop:
add cl, 1
sub rax, rdi
jnc .loop
add rax, rdi ; undo last step
mov [result+rdx], cl
; if cl==0 && dl==0 we have a leading zero, skip it
or cl, dl
setnz cl
add dl, cl
sub esi, 1
jnc .nextdigit
mov byte [result+rdx], al
lea rax, [rdx+1] ; return length
ret
global _start
_start:
sys_write 1,Stmsg,StmsgLen; writing starting msg
xor rax, rax
;inc rax
mov rax, 18446744073709551615
call printbin2dec
xor rcx, rcx
loop1:
add [result+rcx], byte 48
inc rcx
cmp rcx, 20
jnae loop1
mov byte[result+rax],0xa
inc rax
sys_write 1, result,rax
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