You are to design, write, assemble, and simulate an assembly language program wh
ID: 3723653 • Letter: Y
Question
You are to design, write, assemble, and simulate an assembly language program which will generate the Nth number in the Fibonacci sequence. This is a motorola 68HC11 microcontroller
For this lab, N again is a 1 byte unsigned integer. However, the result is stored as a 4-byte number in RESULT in BIG ENDIAN format
For example, if N = 40, your answer should be $06197ECB.
This is what I have so far and I seem to be getting only a 2 byte variable, help please
* Pseudocode:
* unsigned int N = 10; (1 byte variable)
* unsigned int RESULT; (2-byte variable)
*
* unsigned int COUNT; (1 byte variable)
* unsigned int PREV; (2-byte variable)
* unsigned int NEXT; (2-byte variable)
*
*
* RESULT=1;
* PREV=1;
* COUNT = N;
* while(COUNT>2){
* NEXT = RESULT + PREV;
* PREV = RESULT;
* RESULT = NEXT;
* COUNT--;
* }
*
* start of data section
ORG $B000
N FCB 25
ORG $B010
RESULT RMB 2
* define any other variables that you might need here
COUNT RMB 1
PREV RMB 2
NEXT RMB 2
* start of your program
ORG $C000
LDD #1
STD RESULT RESULT = 1
STD PREV PREV = 1
LDAA N
STAA COUNT COUNT = N
WHILE LDAA COUNT
CMPA #2 while(COUNT>2){
BLS ENDWHILE
LDD RESULT
ADDD PREV
STD NEXT NEXT = RESULT + PREV;
LDD RESULT
STD PREV PREV = RESULT;
LDD NEXT
STD RESULT RESULT = NEXT;
DEC COUNT COUNT--;
BRA WHILE }
ENDWHILE
DONE BRA DONE
END
Explanation / Answer
Hi,
Please find below the pseudo code and assembly code for generating fibonacci sequence with 4 bytes in motorola 68HC11.
Pseudocode:
unsigned int N = 40; // 1 byte variable
unsigned int result[] = {0,0,0,0}
unsigned int countF; // 1 byte counter
unsigned int numA[] = {0,0,0,0};
unsigned int numB[] = {0,0,0,0};
ptA = &numA[0];
ptB = &numB[0];
*(ptA) = 0;
*(ptA+1) = 0;
*(ptA+2) = 0;
*(ptA+3) = 0;
*(ptB) = 0;
*(ptB+1) = 0;
*(ptB+2) = 0;
*(ptB+3) = 0;
result[0] = 0;
result[1] = 0;
result[2] = 0;
result[3] = 0;
int CF;
*(ptA+3)+=1;
while( countF != N){
count = 0;
int tempcount = 4;
ptA+3
ptB+3;
CF = 0;
do{
*ptA = *ptA + *ptB + CF;
ptA--;
ptB--;
count--;
}while( tempcount != 0)
ptA = &numA[0];
ptB = &numB[0];
result[0] = *(ptA);
result[1] = *(ptA+1);
result[2] = *(ptA+2);
result[3] = *(ptA+3);
*ptB = *ptA;
*(ptB+1) = *(ptA+1);
*(ptB+2) = *(ptA+2);
*(ptB+3) = *(ptA+3);
*ptA = result[0];
*(ptA+1) = result[1];
*(ptA+2) = result[2];
*(ptA+3) = result[3];
countF++;
}
**************************************
* start of data section
ORG $B000
N FCB 40
ORG $B010
RESULT RMB 4
countF RMB 1 * counter variable for fib sequence
Adata RMB 4
Bdata RMB 4
* start of your program
ORG $C000
LDX #Adata
LDY #Bdata
CLR countF * set count to zero
CLR 0,X * clear out x, and y - useful when running multiple times
CLR 1,X
CLR 2,X
CLR 3,X
CLR 0,Y
CLR 1,Y
CLR 2,Y
CLR 3,Y
CLR RESULT
CLR RESULT+3
INC 3,X * debug, checking if add works
WHILE LDAA countF * -- fibonacci loop -- *
CMPA N
BEQ ENDWHILE * break when n is equal to countF
LDAB #4 * load 4 into B, as counter
LDX #Adata+$3 * set pointer to left most
LDY #Bdata+$3
CLC
DO LDAA 0,X * - ripple carry loop - *
ADCA 0,Y * add c, y to a
STAA 0,X * store result in x
DEX *decrement pointers
DEY
UNTIL DECB *- end ripple carry loop - *
BNE DO
ENDDO
CLRA
LDX #Adata
LDY #Bdata
LDD 0,X * store first 2 bytes
STD RESULT
LDD 2,X * load last 2 bytes, store them
STD RESULT+2
LDD Bdata * store B in A
STD Adata
LDD Bdata+2
STD Adata+2
LDD RESULT * store result in B
STD Bdata
LDD RESULT+2
STD Bdata+2
INC countF * increment count
BRA WHILE
ENDWHILE * -- end fibonacci loop -- *
DONE BRA DONE
END
This 4 byte version performs calculations using ripple carry mode. This model will be necessary as the processor has only 2 byte registers.
Hope this helps.
Pseudocode:
unsigned int N = 40; // 1 byte variable
unsigned int result[] = {0,0,0,0}
unsigned int countF; // 1 byte counter
unsigned int numA[] = {0,0,0,0};
unsigned int numB[] = {0,0,0,0};
ptA = &numA[0];
ptB = &numB[0];
*(ptA) = 0;
*(ptA+1) = 0;
*(ptA+2) = 0;
*(ptA+3) = 0;
*(ptB) = 0;
*(ptB+1) = 0;
*(ptB+2) = 0;
*(ptB+3) = 0;
result[0] = 0;
result[1] = 0;
result[2] = 0;
result[3] = 0;
int CF;
*(ptA+3)+=1;
while( countF != N){
count = 0;
int tempcount = 4;
ptA+3
ptB+3;
CF = 0;
do{
*ptA = *ptA + *ptB + CF;
ptA--;
ptB--;
count--;
}while( tempcount != 0)
ptA = &numA[0];
ptB = &numB[0];
result[0] = *(ptA);
result[1] = *(ptA+1);
result[2] = *(ptA+2);
result[3] = *(ptA+3);
*ptB = *ptA;
*(ptB+1) = *(ptA+1);
*(ptB+2) = *(ptA+2);
*(ptB+3) = *(ptA+3);
*ptA = result[0];
*(ptA+1) = result[1];
*(ptA+2) = result[2];
*(ptA+3) = result[3];
countF++;
}
**************************************
* start of data section
ORG $B000
N FCB 40
ORG $B010
RESULT RMB 4
countF RMB 1 * counter variable for fib sequence
Adata RMB 4
Bdata RMB 4
* start of your program
ORG $C000
LDX #Adata
LDY #Bdata
CLR countF * set count to zero
CLR 0,X * clear out x, and y - useful when running multiple times
CLR 1,X
CLR 2,X
CLR 3,X
CLR 0,Y
CLR 1,Y
CLR 2,Y
CLR 3,Y
CLR RESULT
CLR RESULT+3
INC 3,X * debug, checking if add works
WHILE LDAA countF * -- fibonacci loop -- *
CMPA N
BEQ ENDWHILE * break when n is equal to countF
LDAB #4 * load 4 into B, as counter
LDX #Adata+$3 * set pointer to left most
LDY #Bdata+$3
CLC
DO LDAA 0,X * - ripple carry loop - *
ADCA 0,Y * add c, y to a
STAA 0,X * store result in x
DEX *decrement pointers
DEY
UNTIL DECB *- end ripple carry loop - *
BNE DO
ENDDO
CLRA
LDX #Adata
LDY #Bdata
LDD 0,X * store first 2 bytes
STD RESULT
LDD 2,X * load last 2 bytes, store them
STD RESULT+2
LDD Bdata * store B in A
STD Adata
LDD Bdata+2
STD Adata+2
LDD RESULT * store result in B
STD Bdata
LDD RESULT+2
STD Bdata+2
INC countF * increment count
BRA WHILE
ENDWHILE * -- end fibonacci loop -- *
DONE BRA DONE
END
This 4 byte version performs calculations using ripple carry mode. This model will be necessary as the processor has only 2 byte registers.
Hope this helps.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.