forked from u3forge/AES-Encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIO.inc
247 lines (230 loc) · 3.74 KB
/
IO.inc
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
section .text
; Takes:
; al: char
WriteChar:
push ecx
push edx
push ebx
push eax ;Store parameter on the stack
mov ecx, esp ;Get address of the parameter
mov eax, 0x4 ;Specifying the write syscall
mov ebx, 0x1 ;STDOUT file descriptor
mov edx, 1 ;Printing one byte
int 0x80 ;Invoke the syscall
pop eax ;Clean up the stack
pop ebx
pop edx
pop ecx
ret
; Returns:
; al: value of the char
ReadChar:
sub esp, 4 ;allocate a dword on the stack
mov eax, 3 ;sys_read systemcall
mov ebx, 0 ;STDIN file descriptor
mov ecx, esp ;move the allocated dword address to ecx
mov edx, 2 ;Reading one byte
int 80h ;Invoke the syscall
mov al, BYTE [esp] ;Move the read value into al
add esp, 4 ;Clean up the stack
ret
; Takes:
; ecx: String maximum length
; esi: address of the string
; Returns:
; eax: number of bytes read
ReadString:
push esi
push ecx
mov eax, 3 ;sys_read systemcall
mov ebx, 0 ;STDIN file descriptor
mov edx, ecx ;Reading n bytes
mov ecx, esi ;move the allocated dword address to ecx
int 80h ;Invoke the syscall
pop ecx
pop esi
mov eax, 0
.count:
inc eax
inc esi
cmp eax, ecx
je .equal
cmp BYTE [esi], 0xA
je .done
jmp .count
.equal:
mov eax, ecx
.done:
ret
WriteLine:
mov al, 0xA
call WriteChar
ret
; Takes:
; esi: address of the string
; ecx: length of the string
WriteString:
.print:
mov eax, [esi]
call WriteChar
inc esi
loop .print
ret
; Takes:
; eax: value
; Prints the value in decimal (base-10)
WriteDec:
push 0xf1a9
.next:
mov ecx, 10
mov edx, 0
div ecx
add dl, 0x30
mov edi, eax
xor eax, eax
mov al, dl
push eax
mov eax, edi
cmp eax, 0
jne .next
.print:
pop eax
cmp eax, 0xf1a9
je .done
call WriteChar
jmp .print
.done:
ret
; Returns:
; al: value of the hex byte
ReadHexByte:
push ecx
push esi
sub esp, 4 ;allocate a dword on the stack
mov eax, 3 ;sys_read systemcall
mov ebx, 0 ;STDIN file descriptor
mov ecx, esp ;move the allocated dword address to ecx
mov edx, 3 ;Reading 2 bytes
int 80h ;Invoke the syscall
mov esi, esp
mov ecx, 2
.nextbyte:
cmp BYTE[esi], '9'
jle .zeroto9
sub BYTE[esi], 0x27
.zeroto9:
sub BYTE[esi], 0x30
inc esi
loop .nextbyte
movzx ax, BYTE [esp] ;Move the read value into ax
shl ax, 8
mov al, BYTE [esp+1]
shl al, 4
shr ax, 4
add esp, 4 ;Clean up the stack
pop esi
pop ecx
ret
; Takes:
; eax: value
; Prints the value in hexa (base-16)
WriteHex:
push edx
push ecx
push edi
push eax
mov al, '0'
call WriteChar
mov al, 'x'
call WriteChar
pop eax
push 0xf1a9
.next:
mov ecx, 16
mov edx, 0
div ecx
cmp dl, 10
jl .zeroto9
add dl, 0x7
.zeroto9:
add dl, 0x30
mov edi, eax
xor eax, eax
mov al, dl
push eax
mov eax, edi
cmp eax, 0
jne .next
.print:
pop eax
cmp eax, 0xf1a9
je .done
call WriteChar
jmp .print
.done:
pop edi
pop ecx
pop edx
ret
; Takes:
; esi: array of length 16
; Prints the array as a 4x4 grid
Print4x4Matrix:
mov bl, 0
mov ecx, 16
.print:
xor eax, eax
mov al, [esi]
call WriteHex
inc esi
inc bl
cmp bl, 4
jne .next
mov al, 0xA
call WriteChar
mov bl, 0
jmp .done
.next:
mov al, ' '
call WriteChar
.done:
loop .print
ret
sprintLF:
call sprint
push eax
mov eax, 0xA
push eax
mov eax, esp
call sprint
pop eax
pop eax
ret
sprint:
push edx
push ecx
push ebx
push eax
call strlen
mov edx, eax
pop eax
mov ecx, eax
mov ebx, 1
mov eax, 4
int 80h
pop ebx
pop ecx
pop edx
ret
strlen:
push ebx
mov ebx, eax
.nextchar:
cmp BYTE [eax], 0
jz .finished
inc eax
jmp .nextchar
.finished:
sub eax, ebx
pop ebx
ret