0

我试图让用户输入 2 位数字,第一个是基数,第二个是指数。

这两个值被正确存储。我通过打印它们知道这一点(此打印代码目前已被注释掉)。但是,我计算 base^exponent 答案的循环返回了错误的值。

谁能指出我正确的方向甚至解决我的问题?

这是我的代码:

#/**
#* 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       #/

提前致谢。

4

1 回答 1

1

一种可能性是在调试器中单步执行代码并验证工作寄存器是否包含预期值。

于 2012-06-07T08:17:57.870 回答