【问题标题】:Binary search for sequential search顺序搜索的二分搜索
【发布时间】:2017-11-24 13:48:13
【问题描述】:

当我搜索数组前 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;

【问题讨论】:

  • 你在一个小众标签中发帖,所以你不会得到太多的浏览量。是否可以添加另一个 relevant 标签,让更多人看到它?我不熟悉这个领域。 (我会为知名度投票,但请不要养成在帖子中添加悲伤表情或添加乞求信息的习惯 - 应该在此处为后代写问题)。
  • 每次你用div除以2(而不是换档),一只小猫就会死去。 hla 用于 div(2, edx:eax) 的临时寄存器是什么?另外,为什么每次迭代都重新加载searchItem?您没有使用 esi 或 edi。此外,上/下条件可以是无分支的,只有找到分支。
  • 什么错误信息?你自己的"Not Found",还是你的代码崩溃了?
  • 您是否尝试过使用调试器单步执行您的代码?这通常是查看您的范围出错的好方法。
  • @PeterCordes :使用 HLA,如果您指定一个常量来划分,它会在内存中创建常量(在 .const 段中)并使用内存操作数执行 div

标签: assembly x86 hla


【解决方案1】:

环境

  • HLA(高级汇编器 - HLABE 后端,POLINK 链接器) 版本 2.16 build 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 ];

EAX &gt; -1 的 while 循环中添加附加条件将更正搜索小于 sortedData 中最小值的数字时出现的问题。

变化:

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

收件人:

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

示例

这个例子也考虑了之前关于这个问题的一些cmet。

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 2013-03-06
    相关资源
    最近更新 更多