0% found this document useful (0 votes)
34 views1 page

Binary Search

The document defines registers to store values and pointers used in a binary search algorithm. It initializes pointers to the first and last data items, loads a target value, and displays a prompt. The main loop calculates a midpoint, compares the target value, and either updates the start or end pointer before repeating. If found, the location is displayed, otherwise a not found message displays before restarting.

Uploaded by

pristo seller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views1 page

Binary Search

The document defines registers to store values and pointers used in a binary search algorithm. It initializes pointers to the first and last data items, loads a target value, and displays a prompt. The main loop calculates a midpoint, compares the target value, and either updates the start or end pointer before repeating. If found, the location is displayed, otherwise a not found message displays before restarting.

Uploaded by

pristo seller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Define registers

//R0 Target value


//R1 Pointer to first data item
//R2 Pointer to mid-point
//R3 Pointer to last data item
//R4 Temp data value
//R5 Temp use to display messages
start:
MOV R1, #first
MOV R3, #last
MOV R5, #msg1
STR R5, .WriteString
LDR R0, .InputNum
STR R0, .WriteUnsignedNum
loop:
ADD R2, R1, R3
LSR R2, R2, #3 //Divide by 8, then
LSL R2, R2, #2 //Multiply by 4. Net effect is divide by 2, but modulo 4.
LDR R4, [R2] //Get mid-point value
CMP R0,R4 //Compare target to mid value
BEQ found
BLT belowMid
//Must be above mid if here
MOV R1, R2
ADD R1, R1, #4 //start = mid + 4 (bytes)
B checkForOverlap
belowMid:
MOV R3, R2
SUB R3, R3, #4 //start = mid - 4 (bytes)
B checkForOverlap
checkForOverlap:
CMP R1, R3
BGT notFound
B loop
notFound:
MOV R5, #msg3
STR R5, .WriteString
B start
found:
MOV R5, #msg2
STR R5, .WriteString
STR R2, .WriteHex
B start

msg1: .ASCIZ "\nSearch for ?"


msg2: .ASCIZ "\nIs at memory location: "
msg3: .ASCIZ "\nNot found!"

.ALIGN 256 //Just to make data distinct from code in memory view
first: .WORD 3
.WORD 6
.WORD 7
.WORD 15
.WORD 22
.WORD 24
.WORD 31
.WORD 50
.WORD 79
last: .WORD 94

You might also like