Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 1.28 KB | None | 0 0
  1. .include "address_map_arm.s"
  2. .text
  3. .global binary_search
  4.  
  5. _mystart:
  6.  
  7. binary_search: //R0 *numbers; R1 key; R2 startIndex; R3 endIndex
  8.     SUB SP, SP, #28 //making space for 7 other registers
  9.     STR R8, [SP, #20] //SP[5] = R8; middleIndex
  10.     STR R7, [SP, #16] //SP[4] = R7;
  11.     STR R6, [SP, #12] //SP[3] = R6; numbers[middleIndex]
  12.     STR R5, [SP, #8]  //SP[2] = R5; keyIndex
  13.     STR R4, [SP, #4]  //SP[1] = R4; NumCalls
  14.     STR LR, [SP, #0]  //SP[0] = LR
  15.  
  16.     SUB R8, R3, R2; //
  17.     ADD R8, R2, R8, LSR#1;
  18.  
  19.     LDR R4, [SP, #24]; //loading NumCalls onto R4
  20.     ADD R4, R4, #1;    //NumCalls++
  21.     STR R4, [SP, #24]; //storing R4 onto NumCalls
  22.    
  23.     LDR R6, [R0, R8, LSL#2]; //R6 = numbers[middleIndex]
  24.  
  25.     CMP R2, R3; //compare startIndex and endIndex
  26.     BLE booleanOne;
  27.     MOV R0, #-1; //returns -1 if startIndex > endIndex
  28.  
  29.     LDR LR, [SP, #0];
  30.     ADD SP, SP, #28; //Pop everything out of the stack
  31.     MOV PC, LR; //I dont understand this shit
  32.  
  33. booleanOne: //numbers[middleIndex] == key
  34.     CMP R6, R1
  35.     BNE booleanTwo
  36.     MOV R5, R8
  37.     B returnStep
  38.  
  39. booleanTwo: //numbers[middleIndex] > key
  40.     CMP R6, R1
  41.     BLE booleanThree
  42.     SUB R3, R8, #1;
  43.     BL binary_search;
  44.     MOV R5, R0;
  45.     B returnStep;
  46.    
  47. booleanThree: //else
  48.     ADD R2, R8, #1;
  49.     BL binary_search;
  50.     MOV R5, R0;
  51.     B returnStep;
  52.    
  53.  
  54. returnStep: //finally returning
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement