Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 1.57 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 R0, [SP, #16] //SP[4] = R0; back up of *numbers
  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
  26.     BLE booleanOne
  27.    
  28.    
  29.     MOV R0, #-1 //returns -1 if startIndex > endIndex
  30.  
  31.     //restore
  32.     LDR R8, [SP, #20]  
  33.     LDR R6, [SP, #12]
  34.     LDR R5, [SP, #8]
  35.     LDR R4, [SP, #4]
  36.     LDR LR, [SP, #0]
  37.  
  38.     ADD SP, SP, #28 //Pop everything out of the stack
  39.     MOV PC, LR
  40.  
  41. booleanOne: //numbers[middleIndex] == key
  42.     CMP R6, R1
  43.     BNE booleanTwo
  44.     MOV R5, R8
  45.     B returnStep
  46.  
  47. booleanTwo: //numbers[middleIndex] > key
  48.     CMP R6, R1
  49.     BLE booleanThree   
  50.     SUB R3, R8, #1;    
  51.     BL binary_search;
  52.     MOV R5, R0;
  53.     B returnStep
  54.  
  55. booleanThree: //else
  56.     ADD R2, R8, #1;
  57.     BL binary_search;
  58.     MOV R5, R0;
  59.     B returnStep;
  60.  
  61. returnStep:
  62.     RSB R6, R4, #0 //-NumCalls
  63.     LDR R7, [SP, #16]
  64.     STR R6, [R7, R8, LSL#2] //assigning -NumCalls to number[middleIndex]
  65.     MOV R0, R5
  66.  
  67.     //restore
  68.     LDR R8, [SP, #20]
  69.     LDR R6, [SP, #12]
  70.     LDR R5, [SP, #8]
  71.     LDR R4, [SP, #4]
  72.     LDR LR, [SP, #0]
  73.    
  74.     ADD SP, SP, #28
  75.     MOV PC, LR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement