%include "stud_io.inc"
global _start

section .data
string 	dd 'hello',0		; double word because
pattern	dd 'he*o',0		;   we are working with
				;   4-byte stack

section .text

match:
	push ebp
	mov ebp, esp

	sub esp, 4		; local variable I

	push edi		; save edi and esi
	push esi

	mov esi, [ebp+8]	; use esi for string
	mov edi, [ebp+12]	;   and edi for patter

.again:
	cmp byte [edi], 0	; empty pattern?
	jne .non_end_pattern
	cmp byte [esi], 0	; only empty string
	jne .false		;   can match empty pattern
	jmp .true

.non_end_pattern:
	cmp byte [edi], '*'
	jne .not_star
	mov dword [ebp-4], 0	; I=0
.star_loop:
	mov eax, edi
	inc eax			; substring after '*'
	push eax		;   use for recursive call

	mov eax, esi
	add eax, [ebp-4]	; esi+I
	push eax

	call match
	add esp, 8

	cmp eax, 1		; substring matches
	je .true		;   so we are done

	mov eax, esi		; string already empty?
	add eax, [ebp-4]
	cmp byte [eax], 0
	je .false

	inc dword [ebp-4]	; I=I+1
	jmp .star_loop

.not_star:
	cmp byte [esi], 0	; is the string empty?
	jne .not_end_string
	jmp .false
.not_end_string:
	mov al, [edi]		; '?' character in patter?
	cmp al, '?'
	je .next_char

	cmp al, [esi]		; characters are the same?
	jne .false

.next_char:
	inc esi			; reduce substring
	inc edi			; by a character and let's see
	jmp .again		; if the next substring match

.true:
	mov eax, 1
	jmp .end
.false:
	mov eax, 0
.end:
	pop esi			; restore esi and edi
	pop edi			;   and edi in reverse order

	mov esp, ebp
	pop ebp
	ret

_start:
	push pattern		; argument 2
	push string		; argument 1
	call match
	add esp, 8

	cmp eax, 1		; 1 means true
	jne false
true:
	PRINT 'yes'
	jmp end
false:
	PRINT 'no'
end:
	PUTCHAR 10
	FINISH
