+1 480 409 0818 

Searching a Number in an Array Address

Binary search in a sorted array using CUSP assembly assignment help

CUSP Assembly Code for Binary Search .EQU NUMBER, $100 .EQU N, $101 .EQU SORTED, $102 .EQU @,$000 ; program start address LDS# $E00 ; initialize stack to $E00 BinSearch: LDA# 0 ; start left index in 0 STA LEFT ; update variable LDA N ; load number of elements in acc SBA# 1 ; start right index in n-1 = 5 STA RIGHT ; update variable Search: LDA LEFT ; load LEFT index value in ACC CMA RIGHT ; see if LEFT <=RIGHT JGT fail ; if not, end search, we didn't find the number ADA RIGHT ; Add LEFT+RIGHT SHRA ; divide by 2 using a shift right to get the middle TAX ; save middle index in X LDA NUMBER ; load number to search in ACC CMA+ SORTED ; compare number with SORTED[middle] JEQ found ; if they are equal, we found it JLT setright ; if the number is < middle, adjust right index setleft: ; else, number > middle, adjust left index ADX# 1 ; calculate middle+1 STX LEFT ; set LEFT = middle+1 JMP Search ; search again setright: SBX# 1 ; calculate middle-1 STX RIGHT ; set RIGHT = middle-1 JMP Search ; search again found: LDA# 1 ; set acc to 1 to indicate found JMP done ; terminate program fail: LDA# 0 ; set acc to 0 to indicate not found done: HLT ; Variables for the search: LEFT: .WORD 0 RIGHT: .WORD 5 .END