# Forum: µC & Digital Electronics AVR Arithmatic

Rate this post
 • ▲ useful ▼ not useful
Found AVR code for 32 bit square root under AVR Arithmatic, topic 7.1

The code is look like assembly language but, some instruction is unknown
like "role r24"

and some jump lable not found any where in the code (like brcs 2f, brcs
3f etc)

can anybody make necessary correction ?

The code file is attached.

Rate this post
 • ▲ useful ▼ not useful
Hi,

the code in your attachment sqrt32_Fb.txt  looks different compared to
the one at
https://www.mikrocontroller.net/articles/AVR_Arithmetik#Wurzel

 1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  2 ; Fast and short 32 bits AVR sqrt routine, avr-gcc ABI compliant  3 ; R25:R24 = SQRT (R25:R24:R23:R22)  4 ; rounded down to integer  5 ; Destroys R26,R27,R22,R23,R18,R19  6 ; Cycles incl call & ret = 260-300  7 ; Stack incl call = 2-3  8 .text  9 .global sqrt32_floor  10 .type sqrt32_floor, @function  11 12 sqrt32_floor:  13  ldi R19, 0xc0  14  clr R18 ; rotation mask in R19:R18  15  ldi R27, 0x40  16  sub R26, R26 ; developing sqrt in R27:R26, C=0  17 1: brcs 2f ; C --> Bit is always 1  18  cp R24, R26  19  cpc R25, R27 ; Does test value fit?  20  brcs 3f ; C --> nope, bit is 0  21 2: sub R24, R26  22  sbc R25, R27 ; Adjust argument for next bit  23  or R26, R18  24  or R27, R19 ; Set bit to 1  25 3: lsr R19  26  ror R18 ; Shift right mask, C --> end loop  27  eor R27, R19  28  eor R26, R18 ; Shift right only test bit in result  29  rol R22 ; Bit 0 only set if end of loop  30  rol R23  31  rol R24  32  rol R25 ; Shift left remaining argument (C used at 1:)  33  sbrs R22, 0 ; Skip if 15 bits developed  34  rjmp 1b ; Develop 15 bits of the sqrt  35 36  brcs 4f ; C--> Last bits always 1  37  lsl R23 ; Need bit 7 in C for cpc  38  cpc R26, R24  39  cpc R27, R25 ; After this C is last bit  40 41 4: adc R26, R19 ; Round up if C (R19=0)  42  mov R25, R27 ; return in R25:R24 as for avr-gcc ABI  43  mov R24, R26  44  ret  45 46 .size sqrt32_floor, .-sqrt32_floor 

Rate this post
 • ▲ useful ▼ not useful
Ok, thanks

but where is jump locations ?

see line 16, brcs 2f
see line 20, brcs 3f

assembler shows an error on this instructions

Rate this post
 • ▲ useful ▼ not useful
Kishor S. wrote:

> Ok, thanks
>
> but where is jump locations ?
>
> see line 16, brcs 2f
> see line 20, brcs 3f
>
> assembler shows an error on this instructions

These notations are from C inline assembly language, which is very
different from an directly usable assembly language.

The understanding of this crude notation is simple: the number describes
the relative branch distance (in instruction words), the trailing
character denotes, if it is a forward branch or a backward branch. Of
course the 'f' stands for 'forward'.

So your task is: count instruction words up from the branch instruction
and insert a label at found point in source code. Finally replace the
branch target parameter by the name of this label.

Rate this post
 • ▲ useful ▼ not useful
C-hater wrote:
> The understanding of this crude notation is simple: the number describes
> the relative branch distance (in instruction words), the trailing
> character denotes, if it is a forward branch or a backward branch. Of
> course the 'f' stands for 'forward'.
>
> So your task is: count instruction words up from the branch instruction
> and insert a label at found point in source code. Finally replace the
> branch target parameter by the name of this label.

That's so wrong, even the opposite wouldn't be any better!

1f or 1b (or any other number) simply points to a numbered label in
forward or backward direction.

Rate this post
 • ▲ useful ▼ not useful
Thanks a lot,

I learn this new thing, after 25 yrs. of coding practice..........

Rate this post
 • ▲ useful ▼ not useful
after so many efforts, I can't be able to assemble this code in assembly
language.

I cant interpret this keyword
"brcs 2f"
"brcs 2b"
and
sbrs R22,0 (at line 33)

can anyone explain ?

Please log in before posting. Registration is free and takes only a minute.
Existing account