2

I keep getting error messages when I search for the numbers that are within the range of the top 1/4 of my array. For example, any numbers above 76 are not searchable. Plus, any numbers that are lower than my lowest number in the array or higher than my highest number are impossible, too. Any suggestions?

I've tried switching among memories so many times, but still no use.

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;
Caramiriel
  • 7,029
  • 3
  • 30
  • 50
john lee
  • 23
  • 4
  • You're posting in a niche tag, so you won't get too many views. Is there another _relevant_ tag you can add to this, to get more people to see it? I am not familiar with this area. (I will upvote for visibility, but please do not get into the habit of putting sad-face emoticons into your posts, or adding begging messages - questions should be written for posterity here). – halfer Nov 26 '17 at 11:07
  • Every time you use `div` to divide by 2 (instead of a shift), a kitten dies. What temporary register does `hla` use for `div(2, edx:eax)` anyway? Also, why are you reloading `searchItem` every iteration? You aren't using esi or edi. Also, the upper/lower condition can be branchless, only branch on found. – Peter Cordes Nov 26 '17 at 11:40
  • What error messages? Your own `"Not Found"`, or does your code crash? – Peter Cordes Nov 26 '17 at 11:41
  • Have you tried using a debugger to single-step through your code? That's usually a very good way to see where your ranges go wrong. – Peter Cordes Nov 26 '17 at 11:51
  • @PeterCordes : With HLA if you specify a constant to divide it creates the constant in memory (in the `.const` segment) and does the `div` with a memory operand. – Michael Petch Nov 26 '17 at 11:55
  • Thank you so much everyone for comments. When I try to search for any number below 14 or above 76, its gives me an error message saying 'Memory Access Violation'. At the top of the error message window, it says "HLA Exception (54) at line 20969916 in, edx=$013FFA...". – john lee Nov 28 '17 at 07:33
  • When I search for any number between 15 and 75, the program runs fine though. Everything works fine as I intended, so I think my div() is kind of working??? I am still not too comfortable with using esi, edi, and .const. Is there a debugger for HLA, too? In general, I have never used a debugger for C, C++, or java. – john lee Nov 28 '17 at 07:37

1 Answers1

0

ENVIRONMENT

  • HLA (High Level Assembler - HLABE back end, POLINK linker) Version 2.16 build 4413 (prototype)
  • Windows 10

SOLUTION

Moving the declaration/initialization of the storedData array from static to readonly will solve the memory access violation when accessing the higher indexes of the array.

Change:

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;                                                          

To:

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 ];

Adding an additional condition in the while loop for EAX > -1 will correct the issues when searching for numbers smaller than the smallest value in sortedData.

Change:

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

To:

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

EXAMPLE

This example also takes into account some of the previous comments on this question.

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;
Kuma
  • 427
  • 5
  • 17