Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;; Shreyas Vaidya
  2. ;;; 115415522
  3. ;;; shreyasv
  4.  
  5. ;;; Implementation that computes the nth fibonacci number.
  6.  
  7.     .global isqrt                 ; exposes fib as a label for the linker
  8. isqrt:                            ; the label: the next instruction starts isqrt()
  9.     ;; Add your code here.
  10.     cpi r24, 2          ; if(n < 2) branch to 2
  11.     cpc r25, r1
  12.     brlo 2f
  13.     movw r18, r24
  14.     lsr r24         ; shifts n two bytes to right
  15.     lsr r24
  16.     mov r22, r24        ; moves n>>2 passed as parameter to r22
  17.     mov r23, r25
  18.     push r23
  19.     push r22        ; recursive call for isqrt
  20.     call isqrt
  21.     pop r22         ; pops value from recursive call to r20
  22.     pop r23
  23.     lsl r24
  24.     mov r22, r24        ; moves n>>2 passed as parameter to r22
  25.     mov r23, r25        ; shifts small one byte to left
  26.     mov r20, r22        ; storage for large (for mult)
  27.     mov r21, r23
  28.     inc r20         ; increments small
  29.     mul r20, r20        ; large * large
  30.     mov r20, r0     ; mult gets stored in r0, moved to r16
  31.     clr r1
  32.     cp r18, r20     ; if(n < large * large)
  33.     brlo 3f
  34.     cp r18, r20
  35.     brge 4f
  36.    
  37. 2:              ; returns n
  38.     clr r25
  39.     jmp 1f
  40.  
  41. 3:  movw r24, r22       ; returns small
  42.     jmp 1f
  43.    
  44. 4:  inc r22         ; returns large
  45.     movw r24, r22       ; returns small
  46.     jmp 1f
  47.    
  48. 1: 
  49.     ret                     ; returns.  necessary.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement