Assembly 顺序搜索的二进制搜索

Assembly 顺序搜索的二进制搜索,assembly,x86,hla,Assembly,X86,Hla,当我搜索数组前1/4范围内的数字时,会不断收到错误消息。例如,76以上的任何数字都不可搜索。另外,任何低于数组中最低数字或高于最高数字的数字也是不可能的。有什么建议吗 我试过多次在记忆之间切换,但还是没有用 program BinarySearch; #include("stdlib.hhf") const ArraySize := 17;

当我搜索数组前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;
程序二进制搜索;
#包括(“stdlib.hhf”)
常量数组化:=17;
静止的
SortedData:uns32[ArraySize]:=[15,20,25,30,35,40,45,50,
55, 60, 65, 70, 75, 80, 85, 90, 95 ];
发现:布尔:=false;
搜索项目:uns32;
开始二进制搜索;
输入(nl,“输入两位数进行搜索:”);
stdin.get(searchItem);
mov(0,ecx);
mov(阵列化-1,eax);
而(ecx排序数据[ebx*4])则
添加(1,ebx);
mov(ebx,ecx);
其他的
mov(正确,发现);
endif;
结束时;
标准输出(nl,“二进制搜索:”,nl);
对于(mov(0,ecx);ecx
环境

  • HLA(高级汇编程序-HLABE后端,POLINK链接器) 版本2.16构建4413(原型)
  • 视窗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;                                                          
致:

EAX>-1
在while循环中添加一个附加条件将纠正在
sortedData
中搜索小于最小值的数字时出现的问题

更改:

while(ecx <= eax && found == false) do                                      
while(ecx-SortedData[EBX*4])然后
公司(EBX);
mov(EBX,ECX);
其他的
异或(EBX,EBX);
打破
endif;
结束时;
标准输出(nl,“二进制搜索:”,nl);
对于(mov(0,ECX);ECX
您发布的是利基标签,因此您不会获得太多的视图。有没有其他相关的标签可以添加到这个,让更多的人看到它?我对这个地区不熟悉。(我将投票支持可见性,但请不要养成在帖子中添加悲伤表情符号或添加乞讨信息的习惯-这里应该为后代写问题)。每次使用
div
除以2(而不是移位),小猫就会死亡。
hla
div(2,edx:eax)
使用什么临时寄存器?另外,为什么每次迭代都要重新加载
searchItem
?您没有使用esi或edi。此外,上限/下限条件可以是无分支的,只能在上找到分支。有什么错误消息?您自己的
“找不到”
,或者您的代码崩溃了吗?您是否尝试过使用调试器单步执行代码?这通常是一个很好的方法,可以看出您的范围哪里出了问题。@PeterCordes:HLA如果您指定一个常数进行除法,它将在内存中创建一个常数(在
.const
段中),并使用内存操作数执行
div
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;