【问题标题】:Assembly power returning wrong value装配功率返回错误值
【发布时间】:2012-06-07 07:11:36
【问题描述】:

我试图让用户输入 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       #/

提前致谢。

【问题讨论】:

  • 你得到了什么价值?你的 scanf 是给你数值还是 ascii 值?

标签: math assembly x86 exponentiation


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2018-06-29
    • 1970-01-01
    • 2017-07-28
    • 2023-03-24
    • 1970-01-01
    • 2015-06-19
    相关资源
    最近更新 更多