2016-07-15 3 views
2

mainの27行目でsegfaultを実行しています。サブプログラム呼び出し後に、%raxが値0になっていてはなりません。アセンブリx86 - 文字列segfault/countサブプログラムの頻度が戻り値を保存しない

私のサブプログラムが正しく実行していないことを理解しようとしています。私はバイト比較を前提としています。

メインプログラムの27行目でprintf呼び出しでsegfaultも取得しています。ここではprintf関数があらかじめ定義されていると仮定していますが、ここでprintfサブプログラムを作成する必要があるかもしれません。

サブ

.text 
    .globl FREQ 



FREQ: 
    #subprogram body 
    cmpb $0,0(%rsi)    #check for end of the string 
    je  donefreq 

loopfreq: 
    cmp  %rcx, 0(%rsi)   #compare first string char with vowel 
    je  increment_string  #if equal - jump to increment_string 
    add  $1, %rsi    #if not - increment string 
    jmp  FREQ     #jump to loop to check for end of string status/next char 

increment_string: 
    add  $1, %rsi    #increment to next string character 
    add  $1, %rax    #add 1 to frequency of character 
    jmp  loopfreq 

donefreq: 
    ret 

メイン

   .data 
string:   .string "This course is about encoding numbers and instructions into binary sequences and designing digital systems to process them." 
endofstring: .space 8 
msg:   .string "%c occurs %d times \n" 

       .text 
       .global main 

main: 
    sub  $8,%rsp     #stack alignment 
    mov  $string,%rsi   #rsi = string storage 
    mov  $0x61, %r8    #storage of a 
    mov  $0x65, %r9    #storage of e 
    mov  $0x69, %r10    #storage of i 
    mov  $0x6F, %r11    #storage of o 
    mov  $0x75, %r12    #storage of u 


#Case A 
    mov  %r8,%rcx 
    mov  $0, %rax    #initialize count to 0 
    call FREQ     #Generate %rax value for count 
    mov  %rax, %rdx    #2nd argument for print function - number of 
    mov  $msg, %rdi    #1st argument for print function - format for print function 
    mov  %r8, %rsp   #3rd argument for print function - char 

    call printf     #print the frequency value of the ch in string 


#Case E 
    mov  %r9,%rcx 
    mov  $0, %rax    #initialize count to 0 
    call FREQ 
    mov  $msg, %rdi    #1st argument for print function - format for print function 
    mov  %r9, %rsp   #3rd argument for print function - char 
    mov  %rax, %rdx    #2nd argument for print function - number of 
    call printf     #print the frequency value of the ch in string 

#Case O 
    mov  %r10,%rcx 
    mov  $0, %rax    #initialize count to 0 
    call FREQ 
    mov  $msg, %rdi    #1st argument for print function - format for print function 
    mov  %r10, %rsp   #3rd argument for print function - char 
    mov  %rax, %rdx    #2nd argument for print function - number of 
    call printf     #print the frequency value of the ch in string 

#Case I 
    mov  %r11,%rcx 
    mov  $0, %rax    #initialize count to 0 
    call FREQ 
    mov  $msg, %rdi    #1st argument for print function - format for print function 
    mov  %r11, %rsp   #3rd argument for print function - char 
    mov  %rax, %rdx    #2nd argument for print function - number of 
    call printf     #print the frequency value of the ch in string 
#Case U 
    mov  %r12,%rcx 
    mov  $0, %rax    #initialize count to 0 
    call FREQ 
    mov  $msg, %rdi    #1st argument for print function - format for print function 
    mov  %r12, %rsp   #3rd argument for print function - char 
    mov  %rax, %rdx    #2nd argument for print function - number of 
    call printf     #print the frequency value of the ch in string 

    jmp done 




done: 
    add  $8, %rsp    #reset stack alignment 
    ret 

プログラムは、文の中で、各母音の数をカウント - print文でその文字の数を出力します。

+0

「mov%r9、%rsp」はおそらくタイプミスです。あなたは 'mov%r9、%rsi'をそこに、そして他の場所にしたいと思っています。あなたも他のエラーがありますが、これはあなたが尋ねたものです:) – Jester

+0

これは間違いなくタイプミスです:(おお、ありがとう、うまくいけば、他のエラーは簡単に修正されるでしょう) –

+0

@Jester Hey Jester、どのように私はGDBを使ってサブプログラムの値をチェックできましたか? 'gcc -g -o runtest main.s count.s'でコンパイルした後、コールラインの前後の行で' gdb runtest'をブレークしてコンパイルしますが、ありがとう! –

答えて

1

少なくとも次の問題(私はそれがx86-64のためであると仮定)があります

  1. は、2番目の引数は%rsiに渡す必要があります。
  2. 第3引数は%rdxで渡す必要があります。
  3. printfの場合は、%raxに使用されているベクトルレジスタの数(場合によっては0)を設定する必要があります。 xor %rax, %rax

printfは可変引数リストを持つC関数です。可変引数を取る関数が呼び出されると、%raxベクトルに関数に渡された浮動小数点パラメータの合計数に設定する必要があり

:そのような関数を呼び出す規則がABIセクション3.5.7に見出すことができますレジスタ。

+0

%alはprintf関数に何をしますか? –

+0

@Egyptian_Coder何とか私はそれを間違って思い出しました。 ABIは%raxは0でなければならないと言っています。 – ead

+1

@Egyptian_Coder:可変引数関数に渡されるベクトル引数の数です( 'printf'など)。私はこのSO答えの要件を議論します:http://stackoverflow.com/a/38335743/3857942 –

関連する問題