After several days of working on this binary search problem, which needs to be done completely in Assembly, I'm not quite sure where my logic breaks when it comes to finding name matches [case sensitive] from a sorted array.
Any help would be greatly appreciated:
CC PROGRAM
/*
int b_search (char list[100][20], int count, char* token);
list – the starting address of the list of names to be searched
count – total number of names in the list
token – name to be searched in the list
*/
This is a list of names: Arturo Brian Chris David John Mark Shane SIMON Thomas Toney
Listed below are all the bullets, names to search in the list:
// checks elements with an exact match, for example: "Jon", "shane", "TONY"
// checks case insensitivity, for example: "Chris", "BryAN"
//, , : "" [ ], ""
// , : "DAVe", "Jona"
- ,
0 , " ".
, ; .
//============================================================================================ ====================================
:
int b_search (char list[100][20], int count, char* token)
{
__asm
{
mov eax, 0 ;
mov ebx, 0 ;
mov esi, list ;
mov edi, token ;
TOKEN_LOWERCASE:
mov eax, [edi + ebx] ;
or eax, 0x20 ;
mov [edi + ebx], eax ;
inc ebx ;
cmp [edi + ebx], 0x00 ;
jnz TOKEN_LOWERCASE ;
mov eax, 0 ;
mov ecx, count ;
mov edx, 0 ;
push eax ;
push ecx ;
BEGIN_BINARY_SEARCH:
mov eax, 0 ;
mov ecx, 0 ;
mov edx, 0 ;
pop ecx ;
pop eax ;
cmp ecx, eax ;
jl DONE_EXIT ;
mov edx, eax ;
add edx, ecx ;
sar edx, 0x01 ;
push eax ;
push ecx ;
mov eax, 0 ;
mov ebx, 0 ;
mov ecx, 0 ;
GO_LOWER:
mov ecx, edx ;
imul ecx, 0x14 ;
add ecx, ebx ;
mov eax, [esi + ecx] ;
mov ecx, 0 ;
mov ecx, [edi + ebx] ;
cmp eax, 0x00 ;
jz NULL_TERM_CHECK ;
jnz NOT_NULL ;
NULL_TERM_CHECK:
cmp ecx, eax ;
jz IS_MATCH ;
jl DONE_GO_LOWER ;
jg DONE_GO_HIGHER ;
NOT_NULL:
or eax, 0x20 ;
sub ecx, eax ;
jl DONE_GO_LOWER ;
jg DONE_GO_HIGHER ;
inc ebx ;
jmp GO_LOWER ;
DONE_GO_LOWER:
pop ecx ;
pop eax ;
mov ecx, edx ;
sub ecx, 0x01 ;
push eax ;
push ecx ;
jmp BEGIN_BINARY_SEARCH ;
DONE_GO_HIGHER:
pop ecx ;
pop eax ;
mov eax, edx ;
add eax, 0x01 ;
push eax ;
push ecx ;
jmp BEGIN_BINARY_SEARCH ;
DONE_EXIT:
mov eax, 0 ;
jmp DONE ;
IS_MATCH:
mov eax, edx ;
jmp DONE ;
DONE:
}
}