70 likes | 264 Views
ECE 425. Tables. Binary Search Routine. ;Registers: ;R0 – FIRST ;R1 – LAST ;R2 – MIDDLE ;R3 – INDEX ;R5 – KEY … SEARCHING FOR THIS ;R6 – BASE ADDRESS OF LIST ;R7 – TEMPORARY STORAGE REGISTER. Binary Search Routine (cont’d). ; INITIALIZE REGISTERS
E N D
ECE 425 Tables
Binary Search Routine ;Registers: ;R0 – FIRST ;R1 – LAST ;R2 – MIDDLE ;R3 – INDEX ;R5 – KEY … SEARCHING FOR THIS ;R6 – BASE ADDRESS OF LIST ;R7 – TEMPORARY STORAGE REGISTER
Binary Search Routine (cont’d) ;INITIALIZE REGISTERS LDR R5, =0x123 ;LOAD REGISTER WITH KEY ADR R6, TABLE_START ;LOAD BASE ADDRESS MOV R0, #0 ;SET FIRST TO 0 MOV R1, #NUM-1 ;SET LAST TO LENGTH OF LIST MINUS 1
Binary Search Routine (cont’d) ;TOP OF SEARCH LOOP LOOP CMP R0,R1 ;COMPARE FIRST AND LAST MOVGT R2,#0 ;FIRST>LAST NOT FOUND, PUT 0 IN MIDDLE BGT DONE ;BRANCH TO LOAD INDEX AND END ;MOVE ON WITH SEARCH ADD R2, R0, R1 ;SET UP MIDDLE TO BE IN MIDDLE MOV R2, R2, ASR #1 ;MIDDLE = (FIRST+LAST)/2 ;PULL KEY FROM TABLE ;EACH ENTRY 16 BYTES, KEY FIRST 4 LDR R7, [R6, R2, LSL #4] CMP R5, R7 ;COMPARE KEY WITH ONE SOUGHT ADDGT R0, R2, #1 ;MOVE FIRST TO MIDDLE +1 IF KEY HIGHER SUBLT R1, R2, #1 ;MOVE LAST TO MIDDLE – 1 IF KEY LOWER BNE LOOP ;LOOP BACK IF NOT EQUAL. ;DROP THROUGH IF EQUAL = WE FOUND IT!
Binary Search Routine (cont’d) ;WE’RE DONE. MIDDLE TO THE INDEX REGISTER DONE MOV R3, R2 ;WE WOULD RETURN FROM THIS OF GO ON WITH PROGRAM TABLE_START DCD 0X004 ;4 BYTES FOR KEY DCB “PEPPERONI “ ;12 BYTES FOR DATA
Jump Table MAXJUMP EQU 2;MAXIMUM NUMBER OF PLACES TO JUMP TO MINUS 1 …….. ;CODE COMES TO HERE WITH INDEX VALUE FOR JUMP IN R0 ; e.g.TEMPERATURE CONVERTED TO 0 OR 1 OR 2 BL FUNC_PICK ;JUMP TO FUNCTION TO DECIDE WHAT TO DO BACK…..;FUNCTION WILL RETURN HERE WHEN DONE ;REST OF PROGRAM….. ;BEGINNING OF PICK FUNCTION FUNC_PICK CMP R0, #MAXJUMP ;MAKE SURE VALUE IS LEGAL MOVHS PC, LR ;IF ILLEGAL, DO NOTHING, GO BACK ADR R3, JUMP_TABLE ;LOAD BASE ADDRESS OF JUMP TABLE LDR PC, [R3, R0, LSL #2] ;JUMP TO CHOSEN ROUTINE
Jump Table (cont’d) ;TABLE OF VECTORS OF WHERE TO JUMP JUMP_TABLE DCD ROUTINE0 DCD ROUTINE1 DCD ROUTINE2 ROUTINE0 ;DOES ROUTINE0 … MOV PC, LR ;JUMP BACK TO LINK STORED BEFORE ROUTINE1 ;DOES ROUTINE1 … MOV PC, LR ;JUMP BACK TO LINK STORED BEFORE ROUTINE2 ;DOES ROUTINE2 … MOV PC, LR ;JUMP BACK TO LINK STORED BEFORE ……