Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
314
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          ; comparing n with 2
  11.     brlo 2f         ; if n < 2, branch to 2
  12.     movw r18, r24       ; r18 contains n (storage)
  13.     lsr r24         ; shifts n one byte right
  14.     lsr r24         ; shifts n one byte right
  15.     mov r22, r24        ; moves n>>2 passed as parameter to r22
  16.     mov r23, r25        ; ^same but with high byte
  17.     push r23        ; push high byte, which contains n>>2
  18.     push r22        ; push low byte, which contains n>>2
  19.     call isqrt      ; recursive call for isqrt
  20.     pop r22         ; pops value from recursive call to r22
  21.     pop r23         ; ^same but with high byte
  22.     lsl r24         ; shifts n one byte left
  23.     mov r22, r24        ; moves isqrt(n>>2)<<1 passed to r22
  24.     mov r23, r25        ; ^same but with high byte
  25.     mov r20, r22        ; storage for large (for mult)
  26.     mov r21, r23        ; ^same but with high byte
  27.     inc r20         ; increments small & stores in large
  28.     mul r20, r20        ; large * large
  29.     mov r20, r0     ; mult gets stored in r0, moved to r20
  30.     clr r1          ; clears r1 register (stored mult)
  31.     cp r18, r20     ; if(n < large * large)
  32.     brlo 3f         ; jumps to 3f
  33.     cp r18, r20     ; if(n < large * large)
  34.     brge 4f         ; jumps to 4f
  35.    
  36. 2:  clr r25         ; returns n & clears
  37.     jmp 1f 
  38.  
  39. 3:  movw r24, r22       ; returns small
  40.     jmp 1f
  41.    
  42. 4:  inc r22         ; increments small by 1, which is large
  43.     movw r24, r22       ; returns large
  44.     jmp 1f
  45.    
  46. 1: 
  47.     ret                     ; returns.  necessary.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement