Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 利用堆栈和帧指针分割故障_Assembly_X86_Nasm - Fatal编程技术网

Assembly 利用堆栈和帧指针分割故障

Assembly 利用堆栈和帧指针分割故障,assembly,x86,nasm,Assembly,X86,Nasm,我试图实现一个程序,比较两个不同列表中的一对数字,并存储最大的数字。这必须使用EBP寄存器来访问参数 segment .bss ans resb 4 buff resb 4 largest resb 4 test2 resb 1 segment .data num1 db 1,2,3

我试图实现一个程序,比较两个不同列表中的一对数字,并存储最大的数字。这必须使用EBP寄存器来访问参数

        segment .bss
                ans     resb 4
                buff    resb 4
                largest resb 4
                test2   resb 1
        segment .data
                num1    db 1,2,3,4
                num2    db 5,1,7,2
msg1:      db "  "
msg_size1:             equ $-msg1

space1:  db " "
space1_size:         equ $-space1

        segment .text

        global _start

_start:
                    mov byte[test2], 0
                    mov word[largest], 0
                    mov word[ans], 0
                    mov word[buff], 0
                    mov esi, 0
                    mov bx, 0

label1:
                    cmp esi, 3
                    jg label3
                    mov ecx, [num1+esi]
                    mov edx, [num2+esi]

                    push ecx
                    push edx

                    ;;mov dword[test2], esi
                    call which_is_larger_procedure
                    add esp, 8
                    mov eax, [num2+esi]
                    mov [ans], eax

                    ;;mov esi, dword[test2]
        ;; mov [buff], esi

;;; ;   outut
        ;; mov eax, 4          ; system_write
        ;; mov ebx, 1          ; stdout
        ;; mov ecx, [ans]     ; move biggest element to accumulator
        ;; add ecx, 30h        ; convert to ascii representation
        ;; mov [buff], ecx     ; move to memory
        ;; mov ecx, buff       ; put pointer in ecx for printing
        ;; mov edx, 4          ; size, 4 bytes
        ;; int 80h             ; sytem call.

                    inc esi
                    jmp label1
label3:

exit:
                    mov eax, 1
                    mov ebx, 0
                    int 80h

which_is_larger_procedure:

;;; ;  Body
                    push ebp
                    mov ebp, esp
                    sub esp, 8

                    push eax
                    push ebx

                    mov ecx, [ebp+8]
                    mov edx, [ebp+12]

                    mov eax, [ecx]
                    mov ebx, [edx]



                     cmp al, bl
                     jg CL_g_DL
                     cmp byte[largest], bl
                     jg skip
                     mov byte[largest], bl

AL_g_BL:                        ;If AH is Larger than BL
                     cmp byte[largest], al
                     jg skip
                     mov byte[largest], al

label5:

skip:
;;; ; ;   outut
                    mov eax, 4   ; system_write
                    mov ebx, 1   ; stdout
                    mov ecx, [largest] ; move biggest element to accumulator
                    add ecx, 30h              ; convert to ascii representation
                    mov [buff], ecx    ; move to memory
                    mov ecx, buff      ; put pointer in ecx for printing
                    mov edx, 4        ; size, 4 bytes
                    int 80h           ; sytem call.



;;; ;  Outout
                     mov eax, 4  ; system_write
                     mov ebx, 1  ; stdout
                     mov ecx, msg1 ; put pointer in ecx for printing
                     int 80h      ; sytem call.


                    pop ecx
                    pop edx
                    add esp, 8
                    pop ebp
                    ret
有些代码是在试验其分段错误的原因时遗留下来的


我正在学习本教程:

当我试图找出如何使用GDB调试它时,一位朋友在查看我的代码时发现了这个问题

                mov eax, [ecx]
                mov ebx, [edx]
这里的问题是,通过简单地删除括号,我将保存实际数据而不是内存地址的寄存器解引用到数据,问题就解决了

                mov eax, ecx
                mov ebx, edx

它应该是这样的。

当然您已经尝试过在调试器下运行它,不是吗?segfault发生在哪条指令上?我不知道有任何调试器与nasm一起工作,你有什么建议吗?都有。调试器不关心如何生成二进制可执行文件。任何一个(比如gdb)都会很高兴地反汇编你给它的任何东西,并允许你一条一条地执行指令。