-1

I'm trying to figure out a way to implement the Fibonacci sequence using a 68HC11 IDE that uses a Motorolla as11 assembler.

I've done it using 2-byte unsigned in little-endian format, now I'm attempting to change it using 4-byte variables, using big-endian

My pseudo-code (which is written in c):

RESULT = 1;
PREV = 1;
COUNT = N;
WHILE(COUNT > 2){
    NEXT = RESULT + PREV;
    PREV = RESULT;
    RESULT = NEXT;
    COUNT--;
}

I'll include some of my current assembly code. Please note that count is set to unsigned int at 1-byte, and prev, next, and result are unsigned ints at 2 bytes. N is unsigned, set to 10.

        ORG     $C000
        LDD     #1
        STD     RESULT      
        STD     PREV        
        LDAA    N
        STAA    COUNT       
WHILE   LDAA    COUNT
        CMPA    #2      
         BLS    ENDWHILE
         LDD    RESULT      
        ADDD    PREV    
         STD    NEXT           
         LDD    RESULT  
         STD    PREV           
         LDD    NEXT
         STD    RESULT         
         DEC    COUNT          
         BRA    WHILE       
ENDWHILE
DONE     BRA    DONE
    END

The issue that I'm having is now altering this (other than the obvious variable changes/declarations) N will begin at 40 now, not 10. Would altering my pseudo-code to include pointers allow me to implement it 1 to 1 better with big-endian? Since this is in little-endian, I assume I have to alter some of the branches. Yes this is an assignment for class, I'm not looking for the code, just some guidance would be nice.

Thank you!

1 Answers1

1

(Your problem description is a bit vague as to what your actual problem is, so I may be guessing a bit.)

BTW, 68HC11 is big-endian.

The 68HC11 has a 16-bit accumulator, so as soon as your result overflows this, you need to do math operations in pieces.

I suppose you mean that by changing N from 10 to 40 your fibonacci number becomes too big to be stored in a 16-bit variable.

The use or not of pointers is irrelevant to your problem as you can solve it both with or without them. For example, you can use a pointer to tell your routine where to store the result.

Depending on your maximum expected result, you need to adjust your routine. I will assume you won't need to go over 32-bit result (N=47 => 2971215073).

Here's a partially tested but unoptimized possibility (using ASM11 assembler):

STACKTOP            equ       $1FF
RESET_VECTOR        equ       $FFFE

                    org       $100                ;RAM

result              rmb       4

                    org       $d000               ;ROM

;*******************************************************************************
; Purpose: Return the Nth fibonacci number in result
; Input  : HX -> 32-bit result
;        : A = Nth number to calculate
; Output : None
; Note(s):

GetFibonacci        proc
                    push                          ;macro to save D, X, Y

          ;--- define & initialize local variables

                    des:4                         ;allocate 4 bytes on stack
tmp@@               equ       5                   ;5,Y: temp number

                    ldab      #1
                    pshb
                    clrb
                    pshb:3
prev@@              equ       1                   ;1,Y: previous number (initialized to 1)

                    psha
n@@                 equ       0                   ;0,Y: N
          ;---
                    tsy                           ;Y -> local variables

                    clra
                    clrb
                    std       ,x
                    std       prev@@,y

                    ldd       #1
                    std       2,x
                    std       prev@@+2,y

Loop@@              ldaa      n@@,y
                    cmpa      #2
                    bls       Done@@

                    ldd       2,x
                    addd      prev@@+2,y
                    std       tmp@@+2,y

                    ldaa      1,x
                    adca      prev@@+1,y
                    staa      tmp@@+1,y

                    ldaa      ,x
                    adca      prev@@,y
                    staa      tmp@@,y

                    ldd       ,x
                    std       prev@@,y
                    ldd       2,x
                    std       prev@@+2,y

                    ldd       tmp@@,y
                    std       ,x
                    ldd       tmp@@+2,y
                    std       2,x

                    dec       n@@,y
                    bra       Loop@@

Done@@              ins:9                         ;de-allocate all locals from stack
                    pull                          ;macro to restore D, X, Y
                    rts

;*******************************************************************************
; Test code
;*******************************************************************************

Start               proc
                    ldx       #STACKTOP           ;setup our stack
                    txs

                    ldx       #result
                    ldaa      #40                 ;Nth fibonacci number to get
                    bsr       GetFibonacci
                    bra       *                   ;check 'result' for answer

                    org       RESET_VECTOR
                    dw        Start
tonypdmtr
  • 3,037
  • 2
  • 17
  • 29