-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicFunctions.asm
187 lines (185 loc) · 4.4 KB
/
basicFunctions.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
175
176
177
178
179
180
181
182
183
184
185
186
187
; A list of basic functions to print and scan
; numbers and strings
; how to use this file:
; %include 'basicFunctions.asm'
;==============================================================================
; Author : Rommel Samanez
;==============================================================================
section .data
newline: db 0ah,0
buffer: times 100 db 0
;==============================================================================
; strlen usage
; mov rsi,message
; call strlen
; RETURN rdx : string length
;------------------------------------------------------------------------------
strlen:
mov rdx,0
.stradd:
cmp byte[rsi+rdx],0
jz .strend
inc rdx
jmp .stradd
.strend:
ret
;------------------------------------------------------------------------------
; print usage
; mov rsi,message - message end in 0
; call print
;------------------------------------------------------------------------------
print:
push rax
push rdi
push rdx
call strlen
mov rax,1
mov rdi,1
syscall
pop rdx
pop rdi
pop rax
ret
;------------------------------------------------------------------------------
; println usage
; mov rsi,message - message end in 0
; call println
;------------------------------------------------------------------------------
println:
call print
push rsi
mov rsi,newline
call print
pop rsi
ret
;-----------------------------------------------------------------------------
printnewline:
push rsi
mov rsi,newline
call print
pop rsi
ret
;-----------------------------------------------------------------------------
; printnumber usage
; mov rax,number
; call printnumber
;------------------------------------------------------------------------------
printnumber:
push rbx
push rsi
mov rbx,21
call numbertobuff
mov rsi,buffer
.pnfl03:
cmp byte[rsi],32
jnz .pnfl04
inc rsi
jmp .pnfl03
.pnfl04:
call print
pop rsi
pop rbx
ret
;------------------------------------------------------------------------------
; printnumberf usage
; mov rax,number to print
; mov rbx,length length to print filled with spaces in the left
; call printnumberf
;------------------------------------------------------------------------------
printnumberf:
push rsi
call numbertobuff
mov rsi,buffer
call println
pop rsi
ret
;------------------------------------------------------------------------------
numbertobuff:
push rcx
push rdx
mov rcx,0ah
mov byte[buffer+rbx+1],0
.pnfl01:
dec rbx
mov rdx,0
div rcx ; RDX:RAX / RCX --> Q=RAX R=RDX
add rdx,48
cmp rax,0
jnz .pnfl02
cmp rdx,48
jnz .pnfl02
mov rdx,32
.pnfl02:
mov byte[buffer+rbx],dl
cmp rbx,0
jnz .pnfl01
pop rdx
pop rcx
ret
;------------------------------------------------------------------------------
; bufftonumber usage
; call scanf
; call bufftonumber
; RETURN readed number on RAX
;------------------------------------------------------------------------------
bufftonumber:
push rcx
push rbx
push rdx
push rdi
mov rbx,0 ; looking for the end
.buffx1:
mov cl,byte[buffer+rbx]
cmp cl,0ah
jz .buffx2
inc rbx
jmp .buffx1
.buffx2:
mov rdi,0
mov rcx,1
.buffx3:
mov rax,0
mov al,byte[buffer+rbx-1]
sub al,48 ; subtract ASCII of 0
mov rdx,0
mul rcx
add rdi,rax
mov rdx,0
mov rax,10
mul rcx
mov rcx,rax
dec rbx
jnz .buffx3
mov rax,rdi
pop rdi
pop rdx
pop rbx
pop rcx
ret
;------------------------------------------------------------------------------
; exit the Program
; call exit
;------------------------------------------------------------------------------
exit:
mov rax,60
mov rdi,0
syscall
;------------------------------------------------------------------------------
; scanf usage
; call scanf
; RETURN the readed string in the buffer memory variable
;------------------------------------------------------------------------------
scanf:
push rax
push rdi
push rdx
mov rax,0
mov rdi,1
mov rsi,buffer
mov rdx,100 ; max 100 characters string
syscall
pop rdx
pop rdi
pop rax
ret
;------------------------------------------------------------------------------