二进制搜索顺序搜索

时间:2017-11-24 13:48:13

标签: assembly x86 hla

当我搜索数组前1/4范围内的数字时,我不断收到错误消息。例如,任何高于76的数字都不可搜索。另外,任何低于阵列中最低数字或高于最高数字的数字也是不可能的。有什么建议?

我曾多次尝试在记忆中切换,但仍无用。

program BinarySearch;

#include("stdlib.hhf")                                                          

const ArraySize := 17;                                                          

static
    SortedData: uns32 [ArraySize] := [ 15, 20, 25, 30, 35, 40, 45, 50,          
                                     55, 60, 65, 70, 75, 80, 85, 90, 95 ];
    found:      boolean := false;                                               
    searchItem: uns32;                                                          

begin BinarySearch;

    stdout.put(nl, "Enter a two-digit number for search: ");                    
    stdin.get(searchItem);

    mov(0, ecx);                                                                
    mov(ArraySize - 1, eax);
    while(ecx <= eax && found == false) do                                      

        add(ecx, eax);
        mov(eax, ebx);
        mov(0, edx);
        div(2, edx:eax);                                                        
        mov(eax, edx);
        mov(ebx, eax);
        mov(edx, ebx);
        mov(searchItem, edx);

        if(edx < SortedData[ebx * 4]) then                                      
            sub(1, ebx);
            mov(ebx, eax);
        elseif(edx > SortedData[ebx * 4]) then                                  
            add(1, ebx);
            mov(ebx, ecx);
        else
            mov(true, found);                                                   
        endif;

    endwhile;

    stdout.put(nl, "Binary Search : ", nl);
    for(mov(0, ecx); ecx < ArraySize; inc(ecx)) do                              
        stdout.puti32Size(SortedData[ecx * 4], 4, ' ');
    endfor;
    stdout.newln();                                                             

    if(found) then                                                              
        stdout.put("Found, Search item: ", searchItem, nl);
    else
        stdout.put("Not Found, Search item: ", searchItem, nl);
    endif;

end BinarySearch;

1 个答案:

答案 0 :(得分:0)

环境

  • HLA(高级汇编程序-HLABE后端,POLINK链接器) 版本2.16内部版本4413(原型)
  • Windows 10

解决方案

storedData数组的声明/初始化从static移到readonly将解决访问数组较高索引时的内存访问冲突。

更改:

static
    sortedData: uns32 [ArraySize] := [ 15, 20, 25, 30, 35, 40, 45, 50,          
                                     55, 60, 65, 70, 75, 80, 85, 90, 95 ];
    found:      boolean := false;                                               
    searchItem: uns32;                                                          

收件人:

static
    found:      boolean := false;                                               
    searchItem: uns32;                                                          

readonly
    sortedData: uns32 [ArraySize] := [ 15, 20, 25, 30, 35, 40, 45, 50,          
                                     55, 60, 65, 70, 75, 80, 85, 90, 95 ];

在while循环中为EAX > -1添加一个附加条件可以解决在搜索小于sortedData中最小值的数字时出现的问题。

更改:

while(ecx <= eax && found == false) do                                      

收件人:

while(ecx <= eax && found == false && eax > -1) do                                      

示例

此示例还考虑了之前对该问题的一些评论。

program BinarySearch;
#include("stdlib.hhf");

const
    ArraySize:= 17;

readonly
    SortedData: uns32 [ArraySize]:= 
        [15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95];

begin BinarySearch;
    stdout.put(nl, "Enter a two-digit number for search: ");                    
    stdin.getu32();

    mov(EAX, EDI);
    xor(ECX, ECX);                                                                
    mov(ArraySize, EAX);
    while(ECX <= EAX && EAX > -1) do                                      

        add(ECX, EAX);
        mov(EAX, ESI);
        shr(1, EAX);                                                        
        mov(EAX, EBX);
        mov(ESI, EAX);

        if(EDI < SortedData[EBX * 4]) then                                      
            dec(EBX);
            mov(EBX, EAX);
        elseif(EDI > SortedData[EBX * 4]) then                                  
            inc(EBX);
            mov(EBX, ECX);
        else
            xor(EBX, EBX);  
            break;                                                 
        endif;

    endwhile;

    stdout.put(nl, "Binary Search: ", nl);
    for(mov(0, ECX); ECX < ArraySize; inc(ECX)) do                              
        stdout.puti32Size(SortedData[ECX * 4], 4, ' ');
    endfor;

    if(EBX) then                                                              
        stdout.put(nl, "Not Found, Search item: ", (type uns32 EDI), nl);
    else
        stdout.put(nl, "Found, Search item: ", (type uns32 EDI), nl);
    endif;

end BinarySearch;