SMAL32 (rev 9/03) MP2 solved by Douglas Jone 15:33:26 Page 1 Tue Oct 21 2003 1 TITLE MP2 solved by Douglas Jones 2 3 S START 4 USE "hawk.macs" 5 USE "monitor.h" +000000:+00000000 6 +00000000 +00000000 +00000000 +00000000 +00000000 +00000000 +00000000 +00000000 +00000000 +00000000 +00000000 7 COMMON STACK,#1000 +000030:+00000000 8 PSTACK: W STACK 9 10 ; activation record format for DSPNUM numeric output routine 11 RA = 0 ; the return address 12 REM = 4 ; the remainder 13 QUO = 8 ; the quotient 14 ARSIZE = 12 ; size of activation record in bytes 15 16 ; receiving sequence assumptions 17 DSPNUM: ; R3 holds the integer n to be printed 18 ; R4-7 hold nothing of value 19 ; R8-15 must be saved and restored if we use them 20 STORES R1,R2 ; save the return address +000034: F1A2 21 22 LIS R4,10 ; the divisior to divide R3 by 10 +000036: D40A 23 24 ADDI R2,R2,ARSIZE ; push the current activation record +000038: F262 000C 25 LOAD R1,PDIVU +00003C: F150 FFEC 26 JSRS R1,R1 ; call DIVU +000040: F1B1 27 ADDI R2,R2,-ARSIZE ; pop the current activation record +000042: F262 FFF4 28 29 STORE R3,R2,QUO ; quo = number / 10 +000046: F322 0008 30 STORE R4,R2,REM ; rem = number % 10 (remainder) +00004A: F422 0004 31 32 LOAD R1,R2,QUO ; get the quotient +00004E: F152 0008 33 CMPI R1,0 ; compare with zero +000052: F061 0000 34 BZS DSPNQT ; if (quo > 0) do the following +000056: 0208 35 36 ; output the more significant digits first 37 LOAD R3,R2,QUO ; get the quotient +000058: F352 0008 38 39 ADDI R2,R2,ARSIZE ; push the current activation record +00005C: F262 000C 40 JSR R1,DSPNUM ; call dspnum( quo ); +000060: F130 FFD0 41 ADDI R2,R2,-ARSIZE ; pop the current activation record +000064: F262 FFF4 42 43 DSPNQT: 44 45 ; finally display the least significant digit, the remainder SMAL32 (rev 9/03) MP2 solved by Douglas Jone 15:33:26 Page 2 Tue Oct 21 2003 46 LOAD R3,R2,REM ; get the remainder +000068: F352 0004 47 ADDI R3,R3,'0' ; convert it to a character +00006C: F363 0030 48 49 ADDI R2,R2,ARSIZE ; push the current activation record +000070: F262 000C 50 LOAD R1,PDSPCH +000074: F150 FF94 51 JSRS R1,R1 ; call dspch( rem + '0' ); +000078: F1B1 52 ADDI R2,R2,-ARSIZE ; pop the current activation record +00007A: F262 FFF4 53 54 ; the number has been printed. 55 56 ; return sequence assertions 57 ; R3-7 hold nothing of value 58 ; R8-15, if used, must have been restored 59 LOADS R1,R2 ; get the return address +00007E: F1D2 60 JUMPS R1 ; return +000080: F0B1 61 62 63 ; activation record format for the FIBONACCI function 64 RA = 0 ; the return address 65 VALUE = 4 ; the tentative unsigned integer return value 66 FIB1 = 8 ; fibonacci(value-1) 67 ARSIZE = 12 ; size of activation record in bytes 68 69 ; receiving sequence assumptions 70 FIBONACCI: ; R3 holds the unsigned integer parameter i 71 ; R4-7 hold nothing of value 72 ; R8-15 must be saved and restored if we use them 73 ; note that this code has been optimized a fair bit! 74 STORES R1,R2 ; save the return address +000082: F1A2 75 STORE R3,R2,VALUE ; value = i +000084: F322 0004 76 77 LOAD R1,PDSPCH +000088: F150 FF80 78 LIS R3,'(' +00008C: D328 79 JSRS R1,R1 ; dspch('('); /* MP2 requires this oddity */ +00008E: F1B1 80 81 LOAD R3,R2,VALUE +000090: F352 0004 82 CMPI R3,1 ; compare value with 1 +000094: F063 FFFF 83 BLEU FIBQT ; if (value in R3 > 1) do the following +000098: 0711 84 85 ADDSI R3,-1 ; compute value - 1 +00009A: 13CF 86 ADDI R2,R2,ARSIZE ; push the current activation record +00009C: F262 000C 87 JSR R1,FIBONACCI ; call fibonacci +0000A0: F130 FFDE 88 STORE R3,R2,FIB1-ARSIZE ; fib1 = fibonacci( value - 1 ) +0000A4: F322 FFFC 89 90 LOAD R3,R2,VALUE-ARSIZE ; get value +0000A8: F352 FFF8 91 ADDSI R3,-2 ; compute value - 2 +0000AC: 13CE 92 JSR R1,FIBONACCI ; call fibonacci +0000AE: F130 FFD0 93 ADDI R2,R2,-ARSIZE ; pop the current activation record +0000B2: F262 FFF4 94 95 ; R3 holds fib2 here 96 LOAD R4,R2,FIB1 ; get fib1 +0000B6: F452 0008 97 ADD R3,R3,R4 ; value (in R3) = fib1 + fib2 +0000BA: 3334 98 99 FIBQT: 100 ; R3 is return value on all paths to here 101 STORE R3,R2,VALUE ; value = i SMAL32 (rev 9/03) MP2 solved by Douglas Jone 15:33:26 Page 3 Tue Oct 21 2003 +0000BC: F322 0004 102 103 ADDI R2,FIB1 +0000C0: F262 0008 104 JSR R1,DSPNUM ; dspnum( value ); /* another oddity of MP2 */ +0000C4: F130 FF6C 105 ADDI R2,-FIB1 ; /* note we're using a short AR here! */ +0000C8: F262 FFF8 106 107 LOAD R1,PDSPCH +0000CC: F150 FF3C 108 LIS R3,')' +0000D0: D329 109 JSRS R1,R1 ; dspch(')'); /* MP2 requires this oddity */ +0000D2: F1B1 110 111 LOAD R3,R2,VALUE +0000D4: F352 0004 112 ; return sequence and assertions. 113 ; R4-7 may have been used 114 ; R8-15, if used, must have been restored 115 LOADS R1,R2 ; get the return address +0000D8: F1D2 116 JUMPS R1 ; return +0000DA: F0B1 117 118 119 START: LOAD R2,PSTACK ; set up the stack +0000DC: F250 FF50 120 121 LOAD R1,PDSPINI +0000E0: F150 FF20 122 JSRS R1,R1 ; initialize the display +0000E4: F1B1 123 124 LIS R3,6 +0000E6: D306 125 JSR R1,FIBONACCI ; fibonacci(6) +0000E8: F130 FF96 126 127 LOAD R1,PEXIT +0000EC: F150 FF10 128 JSRS R1,R1 ; call monitor routine to stop! +0000F0: F1B1 129 END no errors