2012-06-07 49 views
0

我想讓用戶輸入2位數字,第一個是基數,第二個是指數。裝配電源返回錯誤值

這兩個值正確存儲。我通過打印來了解它(此打印代碼目前已被註釋掉)。 但是,我的循環來計算基^指數的答案是返回一個錯誤的值。

任何人都可以指向正確的方向,甚至解決我的問題嗎?

這是我的代碼:

#/** 
#* The pow subroutine calculates powers of natural bases 
#* and exponents. 
#* 
#* Arguments: 
#* 
#* base - the exponential base 
#* exp - the exponent 
#* 
#* Return value: 'base' raised to the power of 'exp'. 
#*/ 

#int pow(int base, int exp) 
# { 
# int total = 1; 
# while !(exp <= 0){ 
# total = total * base; 
# exp = exp -1; 
# } 
# return total; 
# } 

.bss 
    EXP: .long 
    BASE: .long 
    TOTAL: .long 

.text 
    FSTR: .asciz "%d" 
    PSTR: .asciz "%d\n" 

.global main 

    inout: 
    pushl %ebp  # Prolog: push the base pointer. 
    movl %esp, %ebp # and copy stack pointer to EBP. 

    subl $4, %esp  # Reserve stack space for variable 
    leal -4(%ebp), %eax  # Load address of stack var in eax 


    pushl %eax  # Push second argument of scanf 
    pushl $FSTR  # Push first argument of scanf 
    call scanf  # Call scanf 


    movl -4(%ebp), %eax  # Move result of scanf from stack to eax 

    movl %ebp, %esp  # Clear local variables from stack. 
    popl %ebp  # Restore caller's base pointer. 
    ret    # return from subroutine. 

    main: 
    call inout 
    movl %eax, BASE 

    #pushl BASE 
    #pushl $PSTR 
    #call printf 

    call inout 
    movl %eax, EXP 

    #pushl EXP 
    #pushl $PSTR 
    #call printf 


    #subl $4, %esp 
    #leal -4(%ebp), %eax 
    #movl %eax, TOTAL 

    movl $1, TOTAL 

    loop: 
    cmpl $0, EXP 
    jle end 
    movl TOTAL, %eax 
    mull BASE 
    movl %eax, TOTAL 
    decl EXP 
    jmp loop 
    end: 
    pushl %eax 
    pushl $PSTR 
    call printf 
    #addl $4, %esp  #\ 
    pushl $0  #- Clean up and exit 
    call exit  #/ 

在此先感謝。

+0

你會得到什麼樣的價值觀?你的scanf是否給你數字值或ascii值? –

回答

1

一種可能性是在調試器中單步執行代碼,並驗證工作寄存器是否包含期望值。