EASY for most

OP (Company: SMU) #2607600
; I am horrible with ARM and I am having problems this is the program I 
wrote
; but it isn't working the right way it is supposed to do the fibonacci 
seq
; using indirect addressing it keeps looping back to when i LDR r0
; what do i do to fix this? thanks

  AREA  FIBSEQ,CODE
SRAM_BASE EQU 0x04000000    ;static ram location

  ENTRY

MAIN  MOV r1,#0      ;first number used in sequence
  MOV r2,#1      ;second number used in sequence
  MOV r3,#0      ;third number used in sequence
  MOV r4,#1      ;counter
  LDR r5,=SRAM_BASE
;  STR r3,[r5],#0      ;these are supposed to be the first numbers in 
the           ;fib seq
;  STR r4,[r5],#1


FIBO          ;method that solves for fib up to 18th seq
  ADD r3,r2,r1
  ADD r1,r1,r2
  SUB r2,r2,r2
  ADD r2,r3,r2      ;also having problems here with logic
  ADD r4,r4,#1
  STR r3, [r5]      ;here is where it loops back
  CMP r4,#18
  BNE FIBO


stop  B  stop
  END
Guest
Your Code doesn't compute the Fibonacci numbers because there are 
logical errors. Code for first 18 Fibonacci numbers:
1
int SRAM_BASE[18];
2
int main(void)
3
{
4
  int r1 = 0;          // MOV r1,#0
5
  int r2 = 1;          // MOV r2,#1
6
  int r3 = 0;          // MOV r3,#0
7
  int r4 = 0;          // MOV r4,#0
8
  int *r5 = SRAM_BASE; // LDR r5,=SRAM_BASE
9

10
  // Init 1st and 2nd Fibonacci numbers
11
  *r5++ = 0;           // STR r3,[r5]
12
                       // ADD r5,r5,#1
13
  r4++;                // ADD r4,r4,#1
14
  *r5++ = 1;           // STR r3,[r5]
15
                       // ADD r5,r5,#1
16
  r4++;                // ADD r4,r4,#1
17

18
  // Compute following Fibonacci numbers
19
  do {
20
  // FIBO:
21
    r3 = r2 + r1;      // ADD r3,r2,r1
22
    *r5++ = r3;        // STR r3,[r5]
23
                       // ADD r5,r5,#1
24
    r4++;              // ADD r4,r4,#1
25
    r1 = r2;           // MOV r1,r2
26
    r2 = r3;           // MOV r2,r3
27
  } while( r4 != 18 ); // CMP r4,#18
28
                       // BNE FIBO                         
29
// STOP:
30
  while(1){}           // B STOP
31
}

Reply

A question of your own? This thread is over 14 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.