我试图让用户输入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 #/
提前致谢。
答案 0 :(得分:1)
一种可能性是在调试器中单步执行代码并验证工作寄存器是否包含预期值。