Creating nested if statements in an ARM assembly

I am interested in converting the Fibonacci sequence code in C ++ to the ARM assembly language. The code in C ++ is as follows:

#include <iostream> 
using namespace std; 
int main()
{
    int range, first = 0 , second = 1, fibonacci; 
    cout << "Enter range for the Fibonacci Sequence" << endl; 
    cin >> range; 

    for (int i = 0; i < range; i++)
    {
        if (i <=1) 
            {
                fibonacci = i; 
            }
        else 
            {
                fibonacci = first and second; 
                first = second; 
                second = fibonacci; 
            }
    }
cout << fibonacci << endl; 

return 0; 
}

My attempt to convert this to an assembly is as follows:

    ldr r0, =0x00000000 ;loads 0 in r0
    ldr r1, =0x00000001 ;loads 1 into r1
    ldr r2, =0x00000002 ;loads 2 into r2, this will be the equivalent of 'n' in C++ code, 
                         but I will force the value of 'n' when writing this code 
    ldr r3, =0x00000000 ;r3 will be used as a counter in the loop 

    ;r4 will be used as 'fibonacci'

loop:
    cmp r3, #2 ;Compares r3 with a value of 0
    it lt 
    movlt r4, r3 ;If r3 is less than #0, r4 will equal r3. This means r4 will only ever be
                  0 or 1. 

    it eq ;If r3 is equal to 2, run through these instructions
    addeq r4, r0, r1
    moveq r0,r1
    mov r1, r4
    adds r3, r3, #1 ;Increases the counter by one 

    it gt ;Similarly, if r3 is greater than 2, run though these instructions
    addgt r4, r0, r1
    movgt r0, r1
    mov r1, r4
    adds r3, r3, #1

I am not entirely sure that this will be done if you make statements in the Assembly, but for me this will be a secondary problem. I'm more interested in how I can include an if statement to check the initial condition in which the โ€œcounterโ€ is compared to the โ€œrangeโ€. If the counter is <range, then it should go to the main part of the code where the Fibonacci operator will be iterated. Then it will continue the cycle to the end = range.

I am not sure how to do the following:

cmp r3, r2 
;If r3 < r2
    {
        <code>
    }

;else, stop

Also, for the correct loop, can I add:

cmp r3, r2
bne loop

, , r3 = r2?

:)

+4
2

if-statements . .

() :

unsigned int fib(unsigned int n)
{
  unsigned int first = 0;
  unsigned int second = 1;
  unsigned int temp;

  if (n > 47) return 0xffffffff; // overflow check
  if (n < 2) return n;

  n -= 1;

  while (1)
  {
    n -= 1;
    if (n == 0) return second;
    temp = first + second;
    first = second;
    second = temp
  }
}

, , , 32- : 12 47 .

, .

: https://www.nayuki.io/page/fast-fibonacci-algorithms

, : :

cmp r0, #47     // r0 is n
movhi   r0, #-1     // overflow check
bxhi    lr
cmp r0, #2
bxlo    lr

sub r2, r0, #1  // r2 is the counter now
mov r1, #0      // r1 is first
mov r0, #1      // r0 is second

loop:
subs    r2, r2, #1  // n -= 1   
add r12, r0, r1 // temp = first + second
mov r1, r0      // first = second
bxeq    lr      // return second when condition is met
mov r0, r12     // second = temp
b   loop

, bxeq lr subs, , Cortex .

, , : if - .

. .

+1

:

if (condition)
  ..iftrue..
else
  ..iffalse..

   eval condition
   conditional_jump_if_true truelabel
   ..iffalse..
   unconditional_jump endlabel
truelabel:
   ..iftrue..
endlabel:

( false true).

ARM : http://www.davespace.co.uk/arm/introduction-to-arm/conditional.html

IT... - Thumb-2: http://en.wikipedia.org/wiki/ARM_architecture#Thumb-2 . . http://www.keil.com/support/man/docs/armasm/armasm_BABJGFDD.htm.

(cmp bne) .

, goto else . else . .

+1

All Articles