How do I print all even Fibonacci numbers in ARM assembly? -


in arm assembly partially able following code work.

it supposed print out of fibonacci numbers 1 10,946 inclusive. able print out sum of numbers. however, having problem getting print out '2' 1 of fibonacci numbers - starts of @ '8'. i've tried changing stuff around messes up.

here code have :

.equ swi_prstr, 0x69  .equ swi_print,0x6b  .equ stdout, 1  .equ swi_exit, 0x11  .global _start .text _start:  mov r2,#0 @starting value of 0 register 1 mov r3,#1 @starting val of 1 register 2 ldr r4,=10946 @register gets value  loop:   cmp r5,r4 bge after add r5,r3,r2 @adding check if odd mov r2,r3   @ answer 0 mov r3,r5   @result in r5 adds r2 0=1 mov r7, r5 , r8, r7, #1 cmp r8, #1 bge loop mov r1, r5 swi swi_print ldr r1, =nl  swi swi_prstr  add r6,r6,r5 @sum of numbers mov r0,#stdout ldr r1, =nl  swi swi_prstr  b loop      after:     mov r1,r6   swi swi_print     swi swi_exit .data nl: .asciz"\n" 

how rewrite code print out '2', along other numbers?

i suspect troublesome line:

mov r0,#stdout 

because of is, gets executed after number printed - if r0 contains wrong value initially, "2" being printed wrong place. time next value ("8"), r0 has been set (and presumably nothing else touches stays way) later output goes stdout correctly.

moving line 'setup' code above loop: ought fix it.


while i'm @ it, there's optimisation make using test instead of compare - instead of this:

mov r7, r5 , r8, r7, #1 cmp r8, #1 bge loop 

you have this:

tst r5, #1      @ tst bitwise , bne loop        @ not equal? what? 

that apparently misleading condition because we're testing whether result 1 or 0 - since integer comparison cmp done subtraction, "equal" synonymous "zero result", , "not equal" "nonzero result".


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -