汇编语言: 用减奇数次数的方法,求一个数的近似平方根,这个平方根是一个整数。

来源:互联网 发布:查看端口有没有被占用 编辑:程序博客网 时间:2024/05/09 06:46

用减奇数次数的方法,求一个数的近似平方根,这个平方根是一个整数。如求17的平 方根,可以用17相继减去奇数1、3、5、7、…,当结果为负数时停止,即: 17-1-3-5-7-9<0 可以看出,17 在减去 5 次奇数后结果变为负数,可以近似认为 17 的平方根在 4 与 5 之间, 计算 NUM 的平方根,如果 NUM=17,则 ANS 中保存结果 4。

s segment    org 100h    assume cs:s,ds:s,ss:s,es:sp1 proc near    lea ax,ans    push ax    mov ax,num    push ax    call p2    mov ah,4ch    int 21hnum dw 17ans dw ?p1 endpp2 proc near    push bp    mov bp,sp    push ax    push bx    push cx    mov ax,[bp+4];number    mov cx,0    mov bx,1l1: sub ax,bx    jl exit    inc cx    add bx,2    jmp l1exit:    mov bx,[bp+6]    mov [bx],cx    pop cx    pop bx    pop ax    pop bp    ret 4p2 endps ends    end p1
原创粉丝点击