汇编语言中间部分查找数字的整数平方根的方法

时间:2018-02-25 21:53:07

标签: assembly x86

我试图利用Mid Section方法来确定数字的整数平方根。我试图在汇编代码中编程这个循环。

l=1; u=x;
while l<=u
    m=(l+u)/2
    if m*m>x
        l=m+1
    else
        u=m-1
return m

这是我为程序集中的Mid Section方法编写的代码

INCLUDE Irvine32.inc
.data
; PLACE DATA DIRECTIVES HERE
    value   SDWORD  1
    lower   SDWORD  1
    upper   SDWORD  0
    middle  SDWORD  ?

;Prompts and messages
prompt      BYTE    "Please enter a signed integer value. The program will terminate if a non-positive number or zero is entered.",0dh, 0ah, 0
endOfLine   BYTE    " ",0dh, 0ah, 0


.code
main PROC
; PLACE SOURCE CODE HERE

program_loop:
    mov     edx, OFFSET prompt      ;print prompt for user to enter height
    call    WriteString
    call    ReadDec
    mov     value, eax              ;read user's height and store it in a variable
    mov     eax, value
    cmp     eax, 0

    jle     endprogram
    mov     upper, eax
    while_loop:
        mov     eax, lower
        cmp     eax, upper
        jg      end_while
        add     eax, upper
        mov     dl, 2
        idiv    dl
        mov     middle, eax
        imul    middle
        cmp     eax, value
        jle     else_if
            mov     eax, middle
            add     eax, lower
            mov     lower, eax
            jmp     end_if
        else_if:
            mov     eax, middle
            sub     eax, lower
            mov     upper, eax
        end_if:

        jmp     while_loop
    end_while:
    mov     edx, OFFSET middle
    call    WriteDec
    call    CrLf
    jmp     program_loop
endprogram:
    call    DumpRegs
    call    WaitMsg
    exit
main ENDP
; PLACE ADDITIONAL PROCEDURES HERE
END main

每当我尝试将(l + u)除以奇数到2以找到中间值时,我得到值258?

0 个答案:

没有答案