string.asm (3789B)
1 get_string_length: ; rdi = pointer, ret rax 2 stackpush 3 cld 4 mov r10, -1 5 mov rsi, rdi 6 get_string_length_start: 7 inc r10 8 lodsb 9 cmp al, 0x00 10 jne get_string_length_start 11 mov rax, r10 12 stackpop 13 ret 14 15 string_copy: ; rdi = dest, rsi = source, rdx = bytes to copy 16 stackpush 17 mov rcx, rdx 18 inc rcx ; to get null 19 cld 20 rep movsb 21 stackpop 22 ret 23 24 string_atoi: ; rdi = string, rax = int 25 stackpush 26 27 mov r8, 0 ; ;return 28 29 call get_string_length 30 mov r10, rax ; length 31 cmp rax, 0 32 je string_atoi_ret_empty 33 34 mov r9, 1 ; multiplier 35 36 dec r10 37 string_atoi_loop: 38 xor rbx, rbx 39 mov bl, BYTE [rdi+r10] 40 sub bl, 0x30 ;get byte, subtract to get real from ascii value 41 mov rax, r9 42 mul rbx ; multiply value by multiplier 43 add r8, rax ; add result to running total 44 dec r10 ; next digit 45 mov rax, 10 ; multiply r9 ( multiplier ) by 10 46 mul r9 47 mov r9, rax 48 cmp r10, -1 49 jne string_atoi_loop 50 jmp string_atoi_ret 51 52 string_atoi_ret_empty: 53 mov rax, -1 54 stackpop 55 ret 56 57 string_atoi_ret: 58 mov rax, r8 59 stackpop 60 ret 61 62 string_contains: ;rdi = haystack, rsi = needle, ret = rax: location of string, else -1 63 stackpush 64 65 xor r10, r10 ; total length from beginning 66 xor r8, r8 ; count from offset 67 68 string_contains_start: 69 mov dl, BYTE [rdi] 70 cmp dl, 0x00 71 je string_contains_ret_no 72 cmp dl, BYTE [rsi] 73 je string_contains_check 74 inc rdi 75 inc r10 ; count from base ( total will be r10 + r8 ) 76 jmp string_contains_start 77 78 string_contains_check: 79 inc r8 ; already checked at pos 0 80 cmp BYTE [rsi+r8], 0x00 81 je string_contains_ret_ok 82 mov dl, [rdi+r8] 83 cmp dl ,0x00 84 je string_contains_ret_no 85 cmp dl, [rsi+r8] 86 je string_contains_check 87 88 inc rdi 89 inc r10 90 xor r8, r8 91 jmp string_contains_start 92 93 string_contains_ret_ok: 94 mov rax, r10 95 jmp string_contains_ret 96 97 string_contains_ret_no: 98 mov rax, -1 99 100 string_contains_ret: 101 stackpop 102 ret 103 104 ;Removes first instance of string 105 string_remove: ;rdi = source, rsi = string to remove, ret = 1 for removed, 0 for not found 106 stackpush 107 108 mov r9, 0 ; return flag 109 110 call get_string_length 111 mov r8, rax ; r8: source length 112 cmp r8, 0 113 mov rax, 0 114 jle string_remove_ret ; source string empty? 115 116 push rdi 117 mov rdi, rsi 118 call get_string_length 119 mov r10, rax ; r10: string to remove length 120 pop rdi 121 cmp r10, 0 122 mov rax, 0 123 jle string_remove_ret ; string to remove is blank? 124 125 string_remove_start: 126 127 call string_contains 128 129 cmp rax,-1 130 je string_remove_ret 131 132 ;Shift source string over 133 add rdi, rax 134 mov rsi, rdi 135 add rsi, r10 ; copying to itself sans found string 136 137 cld 138 string_remove_do_copy: 139 lodsb 140 stosb 141 cmp al, 0x00 142 jne string_remove_do_copy 143 144 mov r9, 1 145 146 string_remove_ret: 147 mov rax, r9 148 stackpop 149 ret 150 151 string_ends_with:;rdi = haystack, rsi = needle, ret = rax: 0 false, 1 true 152 stackpush 153 154 ;Get length of haystack, store in r8 155 call get_string_length 156 mov r8, rax 157 158 ;Get length of needle, store in r10 159 push rdi 160 mov rdi, rsi 161 call get_string_length 162 mov r10, rax 163 pop rdi 164 165 add rdi, r8 166 add rsi, r10 167 168 xor rax, rax 169 xor rdx, rdx 170 171 string_ends_with_loop: 172 ;Start from end, dec r10 till 0 173 mov dl, BYTE [rdi] 174 cmp dl, BYTE [rsi] 175 jne string_ends_with_ret 176 dec rdi 177 dec rsi 178 dec r10 179 cmp r10, 0 180 jne string_ends_with_loop 181 mov rax, 1 182 183 string_ends_with_ret: 184 stackpop 185 ret 186 187 print_line: ; rdi = pointer, rsi = length 188 stackpush 189 call sys_write 190 mov rdi, new_line 191 mov rsi, 1 192 call sys_write 193 stackpop 194 ret 195