字符串段错误/计数子程序的程序集频率未保存返回值



所以我在主程序的第 27 行遇到了一个段错误,在我的子程序调用后,我的 %rax 在不应该的时候保持在值 0。

我试图弄清楚我的子程序没有正确执行什么 - 我假设它与我的字节比较,因为我还不太熟悉它们。

我还在主程序的第 27 行上收到带有 printf 调用的段错误。我在这里假设 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

程序计算句子中每个元音的数量 - 在打印语句中输出该字符的数量。

至少存在以下问题(我假设它是针对 x86-64 的):

  1. 第二个参数必须在 %rsi 中传递。
  2. 第三个参数必须在 %rdx 中传递。
  3. 对于printf,您需要将%rax设置为使用的矢量寄存器数量(在您的情况下0),例如使用 xor %rax, %rax .

printf 是一个带有变量参数列表的 C 函数。调用此类函数的约定可以在 ABI 第 3.5.7 节中找到:

调用采用变量参数的函数时,必须将%rax设置为在向量寄存器中传递给该函数的浮点参数总数。

最新更新