我的X86装配代码循环正在抛出SEG故障

发布于 2025-01-29 05:58:45 字数 2091 浏览 2 评论 0原文

我正在为课堂进行X86组装项目,我们应该实施大量人员记录。 呼叫HEAP_SWAP线路正在给我麻烦。如果我删除它,它会造成SEG故障。但是,无论我如何测试, HEAP_SWAP 功能都可以正常工作。我真的很振奋,并感谢任何人都能提供的任何帮助!

sift_up1:
    
    
    # ecx = i
    # rdx = address to heap
    # r9 = address to heap[i]
    # rax = offset of id
    # r8 = address for heap[i].id_number
    # r10d = heap[i].id_number
    # r11d = index of parent
    # rdx = address for parent id number
    # ebx = heap[parent].id_number
    pushq   %rbp         
    movq    %rsp, %rbp   

    subq    $32, %rsp
    
    pushq   %rbx            #a section to keep track of all the callee saved registers
    pushq   %rdi            #that need to be restored
    
    
    leaq    offset_of_id(%rip), %rax        #put the id offset into a register
    leaq    heap(%rip), %rdx
    
    jmp     LOOP_TOP            

LOOP_TOP:   
    cmpl    $0, %ecx                #Check if i=0, if so jump to exit loop
    je      EXIT_LOOP
    
    movl    $8, %r9d            
    imull   %ecx, %r9d          #finding heap[i]
    addq    (%rdx), %r9
                                

    movq    %r9, %r8            #r8 contains heap[i]
    addq    (%rax), %r8         #add id offset, it becomes heap[i].id_number
    movl    (%r8), %r10d        #dereference id_number and place it into r10d
    
    movl    %ecx, %r11d         #find the index of the parent of i
    subl    $1, %r11d
    shrl    $1, %r11d   
    
    
    movl    $8, %edi            
    imull   %r11d, %edi         
    addq    (%rdx), %rdi        #rdi holds the address of heap[parent]
    addq    (%rax), %rdi        #rdi holds the address of heap[parent].id_number
    
    
    movl    (%rdi), %ebx        #ebx holds the heap[parent].id_number
    
    cmpl    %ebx, %r10d
    jle     EXIT_LOOP
    
    pushq   %rdx
    movq    %r11, %rdx          #put the indexes in the correct parameter functions
                                
#   call    heap_swap           #call heap_swap
    
    popq    %rdx
    movl    %r11d, %ecx         #modify i
    jmp     LOOP_TOP            #jump to loop top

I'm doing an x86 assembly project for class and we're supposed to implement a heap of personnel records. The call heap_swap line is giving me trouble. If I uncomment it, it throws a seg fault. However, the heap_swap function works fine no matter how I test it. I've really racked my brain and would appreciate any help anyone can give!

sift_up1:
    
    
    # ecx = i
    # rdx = address to heap
    # r9 = address to heap[i]
    # rax = offset of id
    # r8 = address for heap[i].id_number
    # r10d = heap[i].id_number
    # r11d = index of parent
    # rdx = address for parent id number
    # ebx = heap[parent].id_number
    pushq   %rbp         
    movq    %rsp, %rbp   

    subq    $32, %rsp
    
    pushq   %rbx            #a section to keep track of all the callee saved registers
    pushq   %rdi            #that need to be restored
    
    
    leaq    offset_of_id(%rip), %rax        #put the id offset into a register
    leaq    heap(%rip), %rdx
    
    jmp     LOOP_TOP            

LOOP_TOP:   
    cmpl    $0, %ecx                #Check if i=0, if so jump to exit loop
    je      EXIT_LOOP
    
    movl    $8, %r9d            
    imull   %ecx, %r9d          #finding heap[i]
    addq    (%rdx), %r9
                                

    movq    %r9, %r8            #r8 contains heap[i]
    addq    (%rax), %r8         #add id offset, it becomes heap[i].id_number
    movl    (%r8), %r10d        #dereference id_number and place it into r10d
    
    movl    %ecx, %r11d         #find the index of the parent of i
    subl    $1, %r11d
    shrl    $1, %r11d   
    
    
    movl    $8, %edi            
    imull   %r11d, %edi         
    addq    (%rdx), %rdi        #rdi holds the address of heap[parent]
    addq    (%rax), %rdi        #rdi holds the address of heap[parent].id_number
    
    
    movl    (%rdi), %ebx        #ebx holds the heap[parent].id_number
    
    cmpl    %ebx, %r10d
    jle     EXIT_LOOP
    
    pushq   %rdx
    movq    %r11, %rdx          #put the indexes in the correct parameter functions
                                
#   call    heap_swap           #call heap_swap
    
    popq    %rdx
    movl    %r11d, %ecx         #modify i
    jmp     LOOP_TOP            #jump to loop top

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文