-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.asm
125 lines (106 loc) · 2.33 KB
/
string.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
;;
;; MC404ABCD - 2008s2
;; Projeto 2
;; decompify - COM files disassembler for Linux.
;;
;; 071294 - Jorge Augusto Hongo
;; 072201 - Raphael Kubo da Costa
;;
;; Copyright (C) 2008 Jorge Augusto Hongo
;; Copyright (C) 2008 Raphael Kubo da Costa
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
%include "string.h"
%include "syscalls.h"
%include "util.h"
section .text
global strlen, write_hex, write_string
;; int strlen(char *s)
;; Gets the length of a null-terminated string.
;;
;; Out: eax (length of the string)
;; Destroys: eax
;;
strlen:
push ebp
mov ebp, esp
push ecx
push edi
mov edi, [ebp+8]
xor ecx, ecx
not ecx
xor al, al
cld
repne scasb
not ecx
lea eax, [ecx-1]
pop edi
pop ecx
pop ebp
ret
;; write_hex (int fd, char *s, size_t elemsize)
;; Writes a hexadecimal byte/word/dword to fd.
;; This procedure was written by Christian Fowelin <[email protected]>
;; for libASM - http://www.fowelin.de/christian/computer/libASM
write_hex:
push ebp
mov ebp, esp
push eax
push ebx
push ecx
push edx
push esi
mov edx, [ebp+16]
mov esi, [ebp+12]
mov ebx, [ebp+8]
and edx, 0xFF
cmp edx, 32
jnae .j0
mov edx, 32
.j0:
mov ecx, 32
sub ecx, edx
shl esi, cl
.j1:
shld ecx, esi, 4
shl esi, 4
and ecx, 0x0F
add ecx, dword HexCharacters
sys_write ebx, ecx, 1
sub edx, byte 4
jnz .j1
pop esi
pop edx
pop ecx
pop ebx
pop eax
pop ebp
ret
;; write_string(int fd, char *s)
;; Writes s to file descriptor fd.
;;
write_string:
push ebp
mov ebp, esp
push edi
push eax
exec strlen, [ebp+12]
mov edi, eax
sys_write [ebp+8], [ebp+12], edi
pop eax
pop edi
pop ebp
ret
; vim:syntax=nasm: