循环分段故障

Loop Segmentation Fault

到目前为止,这是我的代码。

            .data
S:          .string "-149"
Length:     .byte   -1
Result:     .quad 

            .text
            .globl main

main: 
    mov     S,%rdx              #Storage of string, counter, position and result in memory
    mov     Length, %rcx
    mov     Result, %rax
    mov     , %r10
    mov     , %r13
    mov     $-1, %r9

Loop1:                          #loop string from beginning to end  
    cmp     [=10=],0(%rdx)          #compare base addresss value with null
    je      Counter_Made        #if null, branch to end loop. 
    add     %r14, Length        #increment length by for each digit thats not null (creates counter for 2nd loop)
    add     , %rdx            #increment base by 1 to move onto next digit in string
    jmp     Loop1               #reinitiate loop


Counter_Made: 
    cmp     %r15,Length         #check if length is zero
    je      Output              #End program, output null result
    cmp     %r15,Length(%rdx)   #Determine negativity/positivity of integer
    jl      Negative_counter
    jmp     Positive_loop 

Positive_loop: 
    cmp     %r9,Length          #End of loop check 
    je      Output              #Store result if loop end condition satisfied
    mov     %r10, Length(%rdx)  #Store byte of integer in supplementary register
    sub     %r13,  %r10         #Conversion from 8bitASCII to 2Bit Binary
    imul    %r11, %r10          #Place holder multiplication 
    add     %r10, %rax          #Store cumulative addition in memory
    sub     %r14, Length        #Length decrement 
    jmp     Positive_loop       #reloop

Negative_counter:
    sub     %r14,Length
    jmp     Negative_loop

Negative_loop: 
    cmp     %r9,Length
    je      Negative_Complement 
    mov     %r10, Length(%rdx)  #Store byte of integer in supplementary register
    sub     %r13, %r10          #Conversion from 8bitASCII to 2Bit Binary
    imul    %r10, %r10          #Place holder multiplication 
    add     0(%rdx), %rax       #Store cumulative addition in memory
    sub     %r14, Length
    jmp     Negative_loop

Negative_Complement:
    not     %rdx                #Convert to 2's complement with negation and then + 1
    add     %r14,%rdx
    jmp     Output

Output:
    mov      %rdx, Result 
    ret 
#size mismatch for imul 
#Specific place in memory to put output or no?

该代码应该将表示任何有符号整数的字符串转换为其 2 的补码值。

我在我的一个循环中收到一个段错误,我在这里尝试了多种不同的方法但无济于事 - 谁能解释我应该如何修复这个段错误?我很难过。

这是 GDB 错误

Program received signal SIGSEGV, Segmentation fault.
Loop1 () at data.s:18
18      cmp     [=11=],0(%rdx)          #compare base addresss value with null

这是第二个错误。

Counter_Made () at data.s:28
28      cmp     [=12=],Length(%rdx)     #Determine negativity/positivity of integer, if <0 value is negative

我怀疑它是我试图用来解释循环的 Length(%rdx) 方法。 sub ,%rdx

会不会更好

你想要

mov $S,%edx

将字符串的地址加载到%rdx。这是有效的,因为程序图像总是加载到地址 space 的低 4 GB 中。或者,您可以使用 %rip 相对 lea 来加载地址,即使过程映像加载在前 4 GB 之外:

lea S(%rip),%rdx

但是该指令的编码稍长(两个额外的字节)。

说明

mov S,%rdx

加载S指向的内存的前八个字节到%rdx,这不是你想要的。