首页 > 解决方案 > 装配 X86,分割时出现分段错误

问题描述

我收到以下代码片段的分段错误,我无法弄清楚如果在进行乘法运算时它应该正常工作,它为什么会引发分段错误。该代码本质上是与 asm 和 c 文件进行通信,以显示某些信息,例如正在输入的某些数据的总和、平均值、最小值、最大值和乘法/除法。下面是我完全编码的库,其中包含将被调用的函数。

主要问题是分割时

下面是显示我的划分过程的代码:

#-----------------------------------------------------------------------------------
getStatsExtended:
    ; rdi array of bytes
    ; rsi is a quadword with the value being the number of elements in the array
    ; rdx is the address of a quadword to store the max
    ; rcx is the address of a quadword to store the min
    ; r8  is the address of a quadword to store the sum of the numbers
    ; r9  is the address of a quadword to store the average of the numbers
    ; Stack parameter 1 min*max the minimum number mulitiplied times the maximum
    # Stack parameter 2 max/min the maximum number divided by the minimum number

    ;prologue
    ; push the rbp value to the stack
    ;TBD
     push rbp
    ; save the stack pointer into rbp
    ; TBD
    mov rbp, rsp
    ; save any registers that will be used for calculations
    ; TBD
    push r12
    push r13
     
    ; max
    mov r15, rsi ; set the counter to start at the last value in the array
   
    mov rax, 0x0
    mov al, byte [rdi+r15-1]
    nextMaxE:
    dec r15
    cmp r15, 0x0
    jbe MaxE
    cmp al, byte [rdi+r15-1]
    jb greaterE
    jmp nextMaxE

    greaterE:
    mov al, byte [rdi+r15-1]
    jmp nextMaxE    

    MaxE:
    push rax ; put the value on the stack to pick up later

    ; min
    mov r15, rsi ; set the counter to start at the last value in the array
   
    mov rax, 0x0
    mov al, byte [rdi+r15-1]

    nextMinE:
    dec r15
    cmp r15, 0x0
    jbe MinE
    cmp al, byte [rdi+r15-1]
    ja lesserE
    jmp nextMinE

    lesserE:
    mov al, byte [rdi+r15-1]
    jmp nextMinE

    MinE:
    push rax ; put the value on the stack to pick up later
   
    ; sum
    mov r15, rsi ; set the counter to start at the last value in the array
   
    mov rax, 0x0
    mov al, byte [rdi+r15-1]

    nextSumE:
    dec r15
    cmp r15, 0x0
    jbe SumE
    mov r14b, byte[rdi+r15-1]
    add rax, r14
    jmp nextSumE

    SumE:
    push rax ; put the value on the stack to pick up later
   
    ; ave
    push rdx
    mov rdx, 0x0
    div rsi
    mov qword[r9], rax ; set the return value for ave
    pop rdx
   
    ; pull values from the stack
    pop qword [r8]      ; sum
    pop qword [rcx]     ; min
    pop qword [rdx]     ; max
   
    ; multiply min times max return in 7th parameter
    ; TBD
     mov rax, qword[rcx] ; moving into rax min
     imul qword[rdx] ; multiplying by rdx
     
     mov r12, qword[rbp+16] ; Setting r12 to addres of 7th parameter
     mov qword[r12], rax        ; setting value of rax (mul result) to 7th parameter
     
   
    ; divide max by min return in 8th parameter
    ; TBD
    mov rax, qword[rdx] ; moving max into rax
     xor rdx, rdx           ; zeroing out rdx
    div qword[rcx]           ; dividing by min
   
    call printRAX
   
    mov r13, qword[rbp+24] ; setting r13 to the address of 8th parameter
    mov qword[r13], rax       ; setting value of rax (div result) to 8th parameter
   
    ;epilogue
    ; pop the saved registers
    ; TBD
    pop r13
    pop r12
   
    ; pop the rbp
    ; TBD
    pop rbp
    ret
    
 

标签: functionassemblyx86-64

解决方案


推荐阅读