Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

x86 - Hard coding regex in assembly (MASM 32)

What my code is supposed to do is to print a match for the regexp [ab][^abc]+c in a string received as a command line argument. In the find_index section I'm looking for a or b. Then, if either one is found, I'm iterating through the string until a, b or c is found to find the length of the match. And eventually the match is printed char by char. Now, obviously this code doesn't work (segmentation fault). Besides, I'm not sure what to do with the last c of the pattern, but it's not that important. I just really want my code to work, but I have no idea what I'm doing wrong. I guess it has something to do with pointers, values and the stack. Any help is greatly appreciated.

.intel_syntax noprefix
.global main

.text

main:
        pusha
        mov ebx, DWORD PTR [esp+8]
        mov eax, DWORD PTR [ebx+4]
        mov ebx, 0 
        mov ecx, [eax]
        call find_index
        call print_string
        call return

find_index:
        mov edx, DWORD PTR [ecx]
        cmp ecx, 97
        je find_length
        cmp ecx, 98
        je find_length
        cmp ecx, BYTE PTR 0 
        je return
        add ecx, DWORD PTR 1
        jmp find_index

find_length:
        add edx, DWORD PTR 1
        add ebx, 1
        cmp edx, 99
        jg find_length
        cmp edx, 32
        je find_length
        mov ebx, 0      
        jmp find_index

print_string:
        push ecx
        call offset msg
        call printf
        add esp, 4
        cmp ecx, ebx
        je main
        add ecx, DWORD PTR 1
        jmp print_string

return:
        popa
        mov eax, 0
        ret

.data

msg .asciz "%c"
question from:https://stackoverflow.com/questions/65924753/hard-coding-regex-in-assembly-masm-32

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...