问题描述
我是汇编编程的新手,我正在尝试实现一个程序,要求用户输入两个整数。然后,程序应将第一个整数的值返回第二个整数的幂。我试图通过使用循环一次又一次地将RAX中的值相乘来实现此目的。我相信我已正确完成此操作,但是当我尝试调用函数pow时会出现问题,导致分段错误错误。我想知道我在做什么错。代码如下:
.text
welcomeStr: .asciz "\nEnter a non-negative base followed by an exponent:\n"
inputStr: .asciz "%ld"
outputStr: .asciz "The result is%ld\n"
.data
base: .quad 0
exp: .quad 0
.global main
main:
# Prologue
pushq %rbp
movq %rsp,%rbp
# Print the welcome message
movq $0,%rax
movq $welcomeStr,%rdi
call printf
# Get input from user and store in base
leaq inputStr(%rip),%rdi
leaq base(%rip),%rsi
call scanf
# Get input from user and store in exp
leaq inputStr(%rip),%rdi
leaq exp(%rip),%rsi
call scanf
call pow
# Print the value in rax to the command line
movq %rax,%rsi
movq $0,%rax
movq $outputStr,%rdi
call printf
movq $0,%rdi
call exit
pow:
# Prologue
pushq %rbp
movq %rsp,%rbp
# Load rcx with the value of the exponential and rax with 1
movq exp,%rcx
movq $1,%rax
loop:
cmpq $0,%rcx
jle end # Break loop if the value in rcx is less than or equal to 1
mulq base
dec %rcx # Decrement counter by one
jmp loop # Loop again
# Epilogue
movq %rbp,%rsp
popq %rbp
ret
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)