COT5310hw2key - COT 5310 Homework 2 Key (a) Present a...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COT 5310 Homework 2 Key (a) Present a Register Machine that computes FIB. Assume R1 = x ; at termination set R2 = 1 if x is a member of the Fibonacci sequence and 0 if not. 1. INC 4 [2] // set up R3 = 0 = l and R4 = 1 = h as the first two Fibonacci numbers 2. DEC 1 [3, 14] // move input R1 and R3 (= l ) to R5 , finish if cant decrement 3. DEC 3 [4, 5] 4. INC 5 [2] 5. INC 1 [6] // R1 is now x l 1, make it equal x l , this implies R3 =0 now 6. DEC 4 [7, 9] // move R4 (= h ) into R3 and copy to R6 7. INC 3 [8] 8. INC 6 [6] 9. DEC 5 [10, 12] // now R5 = l , move to R4 and R1 , thus restoring R1 to x 10. INC 1 [11] 11. INC 4 [9] 12. DEC 6 [13, 2] // move copy of h in R6 to R4 which contains h right now 13. INC 4 [12] 14. DEC 3 [16, 15] // if R3 = 0 then x FIB 15. INC 2 [16] 16. DEC 3 [16, 17] // begin cleanup to make things look pretty 17. DEC 4 [17, 18] 18. DEC 5 [18, 19] (b) Present a Factor Replacement System that computes FIB. Assume starting number is 3 x 5; at termi- nation, result is 2 = 2 1 if x is a member of the Fibonacci sequence; 1 = 2 otherwise. Actually, it can be done without the 5, but that may make it easier....
View Full Document

Page1 / 2

COT5310hw2key - COT 5310 Homework 2 Key (a) Present a...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online