Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; Shreyas Vaidya
- ;;; 115415522
- ;;; shreyasv
- ;;; Implementation that computes the nth fibonacci number.
- .global isqrt ; exposes fib as a label for the linker
- isqrt: ; the label: the next instruction starts isqrt()
- ;; Add your code here.
- cpi r24, 2 ; if(n < 2) branch to 2
- cpc r25, r1
- brlo 2f
- movw r18, r24
- lsr r24 ; shifts n two bytes to right
- lsr r24
- mov r22, r24 ; moves n>>2 passed as parameter to r22
- mov r23, r25
- push r23
- push r22 ; recursive call for isqrt
- call isqrt
- pop r22 ; pops value from recursive call to r20
- pop r23
- lsl r24
- mov r22, r24 ; moves n>>2 passed as parameter to r22
- mov r23, r25 ; shifts small one byte to left
- mov r20, r22 ; storage for large (for mult)
- mov r21, r23
- inc r20 ; increments small
- mul r20, r20 ; large * large
- mov r20, r0 ; mult gets stored in r0, moved to r16
- clr r1
- cp r18, r20 ; if(n < large * large)
- brlo 3f
- cp r18, r20
- brge 4f
- 2: ; returns n
- clr r25
- jmp 1f
- 3: movw r24, r22 ; returns small
- jmp 1f
- 4: inc r22 ; returns large
- movw r24, r22 ; returns small
- jmp 1f
- 1:
- ret ; returns. necessary.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement