我學習到的x86-64彙編代碼(AT & T)在Linux上(GCC)和無法找到這個簡單的代碼解決我的分段故障。我看到一些問題涉及堆棧對齊;
.global main
main:
#prologue
movq %rsp, %rbp #initialise base pointer
#reserve memory for subroutine
subq $8, %rsp #the line causing the segfault
exit:
movq $0, %rdi
call exit
我的其他程序從來就寫到似乎做工精細後調用printf:然而,當我嘗試$ 8或$ 16本甚至失敗。上面的代碼有什麼問題?該代碼失敗,或不需要退出調用。這和下面的代碼是失敗的。我編譯使用:
gcc -o test test.s
整個代碼:
.text
formatStr: .asciz "%ld"
resultStr: .asciz "The result is: %d\n"
q1: .asciz "Enter the base: "
q2: .asciz "Enter the exponent: "
#qTable2: #look up table for correct string during scanf
# .asciz q1
# .asciz q2
qTable: #alternative look up table
.quad base
.quad exponent
base:
movq $q1, %rdi
ret
exponent:
movq $q2, %rdi
ret
###################
# Subroutine: pow
# Function: Power an integer base to an exponent
# Inputs: uint base, int exponent(natural)
# Outputs: int result
##################
pow:
#prologue
pushq %rbp #store caller base pointer
movq %rsp, %rbp
movq $1, %rax #reset result
movq $0, %rbx #initialise loop
loop1:
imulq %rdi
incq %rbx
cmp %rsi, %rbx #compare loop interator to exponent
jle loop1
#epilogue
movq %rbp, %rsp #clear local variables from stack
pop %rbp #restore caller base pointer
ret
.global main
###################
# Subroutine: Main
# Function: Application entry point
###################
main:
#prologue
pushq %rbp
movq %rsp, %rbp #initialise base pointer
#reserve memory for subroutine
subq $8, %rsp
#Gather the inputs from the user
movq $0, %rbx #loop counter
#inputAcq:
#Call printf using correct question
movq %rax, %rsi #move result into argument 2
movq qTable(,%rbx,8), %rdi #format string as argument 1
call *%rdi
movq $0, %rax #no vector registers
call printf
leaq -16(%rbp,%rbx,8), %rsi #Argument 2
movq formatStr, %rdi #Argument 1
movq $0, %rax #no vector registers
call scanf
incq %rbx #increment loop counter
cmp $1, %rbx #check if more inputs are necessary else continue
jl inputAcq
#Call pow
movq -8(%rbp), %rsi #the exponent
movq -16(%rbp), %rdi #the base
call pow
#Call printf
movq %rax, %rsi #move result into argument 2
movq $resultStr, %rdi #format string as argument 1
movq $0, %rax #no vector registers
call printf
#exit program without errors
exit:
movq $0 , %rdi
call exit
主代碼中的錯誤使用(GDB)X/I $ PC時:
0x4004e5 <exit+7>: callq 0x4004de <exit>
你的代碼的其餘部分是什麼? – fuz
http://stackoverflow.com/help/mcve – sigjuice
@sigjuice:'[mcve]'在評論中擴展爲[mcve]。另請參閱http://meta.stackexchange.com/questions/92060/add-data-se-style-magic-links-to-comments/94000#94000其他魔術評論降價,如'[問]''。 –