Skip to content

Instantly share code, notes, and snippets.

@pstiasny
Created December 18, 2011 22:37
Show Gist options
  • Save pstiasny/1494696 to your computer and use it in GitHub Desktop.
Save pstiasny/1494696 to your computer and use it in GitHub Desktop.
Remove all occurences of charactes from string 2 in string 1
CFLAGS = -m32
text: main.o text.o
gcc $(CFLAGS) main.o text.o -o text
main.o: text.c
gcc $(CFLAGS) -c text.c -o main.o
text.o: text.s
nasm -f elf text.s
clean:
rm -f text text.o main.o
#include <stdio.h>
void subs(char* str, char* str2);
int main(int argc, char** argv)
{
char buf[2048], buf2[2048];
scanf("%s ", buf);
scanf("%s ", buf2);
printf("searching %s in %s\n", buf2, buf);
/*long r =*/ subs(buf, buf2);
printf("%s\n", buf);
return 0;
}
bits 32
section .text
global subs
remove:
push edi
push eax
remove_cont:
movzx eax, byte[edi+1]
cmp eax, 0
jz remove_end
mov byte[edi], al
inc edi
jmp remove_cont
remove_end:
mov byte[edi], 0
pop eax
pop edi
ret
subs:
;ramka stosu
push ebp
mov ebp, esp
sub esp, 257
%idefine str [ebp+8]
%idefine str2 [ebp+12]
push ebx
push edi
push esi
mov edx, str2
mov edi, str
;zero fill
mov ecx, -257
zfloop:
jz read_chars
mov byte [ebp+ecx], 0
inc ecx
jmp zfloop
;chars in str2
read_chars:
movzx ebx, byte[edx] ; char of str2
inc edx
mov ecx, -1
sub ecx, ebx
mov byte [ebp+ecx], 1
cmp ebx, 0
jnz read_chars
stringloop:
movzx esi, byte[edi] ; char of str
cmp esi, 0
jz end_subs
mov ecx, -1
sub ecx, esi
movzx ebx, byte[ebp+ecx]
cmp ebx, 0
jz next_cmp
call remove
jmp stringloop
next_cmp:
inc edi
jmp stringloop
end_subs:
pop esi
pop edi
pop ebx
leave
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment