我对汇编语言还是陌生的。我正在尝试在汇编中实现pow()
函数。该函数具有两个变量,它正在计算基数和指数的幂。我仅使用乘法来完成此操作。但是出于某些奇怪的原因,我得到了Segmentation fault (core dumped)
。我假设我正在尝试访问一些我不应该访问的内存地址,但是已经有两天了,我无法弄清。
这是我的代码:
.data
formatstr: .asciz "%ld\n"
formatstr_scanf: .asciz "%ld"
user_instructions: .asciz "Please enter two POSITIVE integers for the base and the exponent(respectivly)\n"
.global main
main:
movq %rsp , %rbp # Initialize base pointers
movq $0, %rax # Set %rax to 0
movq $user_instructions, %rdi
call printf
call pow # Call subroutine inout
movq %rax, %rsi
movq $0, %rax # reset rax
movq $formatstr, %rdi # save formatstr to rdi so that it's printed first
call printf # Print the First argument (%rdi)
call end
pow: # Creating subroutine inout
pushq %rbp # Pushing the %rbp to the stack
movq %rsp, %rbp # "Creating" new stack for the subroutinne
subq $16, %rsp # Reverse stack space for variable
leaq -8(%rbp), %rsi # Load address of stack var in rsi
movq $formatstr_scanf, %rdi # string formationg the first element for scanf
movq $0, %rax
call scanf # call scanf
leaq -16(%rbp), %rsi # Load address of stack var in rsi
movq $formatstr_scanf, %rdi # string formationg the firs element for scanf
movq $0, %rax
call scanf # call scanf
movq $1, %rbx
loop1:
cmpq %rbx, -16(%rbp)
je endloop
inc %rbx
mulq -8(%rbp)
jmp loop1
endloop:
movq -8(%rbp), %rax # Put the number for return
movq %rbp, %rsp # Put the stack abck to normal
popq %rbp # move base pointer back to the top of the stack
ret
end:
mov $0, %rdi # load program exit code
call exit # exit the program
任何帮助或建议都将受到赞赏!