简体   繁体   中英

Finding the square root of a number

I want to know how would you find the square root in the EASy68k assembler.

I know it's a function but I don't know the code for it.

I want to find the Square root of 72.

The answer should be an integer so 8 in this case.

I found this algorithm:

value-->c1 
loop: 
value/c1-->c2 
(c1+c2)/2-->c1 
until c1=c2 
c1-->result 

I converted this into 68k code:

    move.w #72,d2  ; value = 64
    move.l d2,d5   ; c1 = 64
    move.l d5,d3   ; hold d3 = 64
LOOP
    divs d2,d3     ; value/c1  
    move.l d3,d6   ; move answer above to c2 = d6
    add.l d5,d6    ; add c1+c2
    divs #2,d6
    move.l d6,d5   ; move the answer above it do d4 = c1
    cmp.l d6,d5
    beq loop

    move.l d5,d7   ; d7 will have the result

And it doesn't work for some reason.

The division at the beginning of your loop will not divide value by c1 except on the very first iteration. Since your value is in d2 and c1 is in d5 you should replace:

divs d2,d3     ; value/c1  
move.l d3,d6   ; move answer above to c2 = d6

with:

move.l d2,d1   ; temp = value
divs d5,d1     ; temp /= c1
move.l d1,d6   ; d6 = value / c1

I found it unclear whether you're using divs.w or divs.l . If you're using divs.w you'll have to keep in mind that d1 will contain both the quotient and the remainder after the division.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM