This repository has been archived by the owner on Mar 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.fasm
103 lines (93 loc) · 1.72 KB
/
utils.fasm
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
; My awesome standard library...
; exit - syscall to exit.
; print(msg,msg_size) - print some message.
; println(msg,msg_size) - print some message with newline.
; itoa(number,dst,base) - convert number to null terminated string using specified base.
; Network stuff
; ...
segment readable writeable executable
; Declare a string + length var
macro decl_str var, str
{
var db str
var#.len = $-var
}
; Print a declared string
macro print_str var { print var,var#.len }
macro println_str var { println var,var#.len }
macro print msg, msg_size
{
push rdx
push rsi
mov rdx, msg_size
mov rsi, msg
call stdout
pop rsi
pop rdx
}
macro println msg, msg_size
{
print msg, msg_size
print lf, lf_size
}
macro itoa num,base,dst
{
push rax
push rbx
push rcx
mov eax, num
mov ebx, base
mov rcx, dst
call _itoa
pop rcx
pop rbx
pop rax
}
; eax = number to convert
; ebx = base
; ecx = dst adress to save
_itoa:
mov r8b, 0 ; counter
mov dx, 0
mov qword [dst_adr], rcx
.loop:
mov edx, 0
; mov ebx, 10 ; base 10
div ebx
add dl, 48 ; ascii 0
mov r9, itoa_buf ; load our temp buffer
add r9, r8 ; add offset
mov byte [r9], dl
inc r8b
cmp eax, 0 ; if eax = 0
jne .loop
dec r8b
; loop in reverse order and save to our specified dst adress
mov r10,qword [dst_adr]
.save_result:
mov r9,itoa_buf
add r9,r8
mov r12b,byte [r9] ; copy a digit from itoa_buf
mov byte [r10],r12b ; to dst_adr
inc r10
dec r8b
cmp r8b,-1
jne .save_result
; inc r10 ; TODO: add NUL ?
ret
stdout:
push rax
push rdi
mov edi,1
mov eax,1
syscall
pop rdi
pop rax
ret
exit:
xor edi,edi ; exit code 0
mov eax,60 ; sys_exit
syscall
lf db 0xA
dst_adr db ' '
itoa_buf db ' '