汇编代码程序发生分段错误错误

时间:2011-03-19 20:36:58

标签: linux assembly segmentation-fault nasm

运行代码时,我一直遇到分段错误错误。一切都汇编得很好,但我似乎无法做到我想做的事情。程序是要求用户输入3个整数,然后询问用户他们认为数字的平均值是什么,将其考虑在内,然后回过头来判断用户是否正确

    segment .data
;
; Output strings
;
prompt1          db    "Enter a positive integer: ", 0
prompt2          db    "Enter a second positive integer: ", 0
prompt3          db    "Enter a third positive integer: ", 0
prompt4      db    "Enter a guess of their average: ", 0   
outmsg1          db    "You entered ", 0
outmsg2          db    " and ", 0
outmsg3          db    " and ", 0
outmsg4      db    "You guessed that the average is ", 0
outmsg5      db    "You did you guess correctly? (0 = no, 1 = yes)", 0
avermsg      db    "The average of the numbers is ", 0


segment .bss

input1   resd 1
input2   resd 1
input3   resd 1
input4   resd 1
guess    resd 1

segment .text
        Global  main
main:
        enter   0,0               ; setup routine
        pusha

        mov     eax, prompt1      ; print out prompt1
        call    print_string

        call    read_int          ; read integer    
        mov     [input1], eax     ; store integer into input1


        mov     eax, prompt2      ; print out prompt2
        call    print_string

    call    read_int      ; read integer
    mov [input2], eax     ; store integer into input2

    mov     eax, prompt3      ; print out prompt3
        call    print_string

    call    read_int      ; read integer
    mov     [input3], eax     ; store integer into input3

    mov eax, prompt4      ; print out prompt4
    call    print_string      

    call    read_int      ; read integer
    mov [guess], eax


    mov eax, [input1]     ; eax = dword at input1
    add eax, [input2]     ; eax += dword at input2
    add eax, [input3]     ; eax += dword at input3
    mov ebx, 3          
    div ebx       ; divides the sum by 3
    mov ecx, eax      ; freeing up eax, puts quotient into ecx

    dump_regs 1       ; print out register values

; next print out results    
    mov    eax, outmsg1
    call   print_string   ; print out first message
    mov    eax, [input1]
    call   print_int

    mov    eax, outmsg2   
    call   print_string   ; print out second message
    mov    eax, [input2]
    call   print_int

    mov    eax, outmsg3
    call   print_string       ; print out  thrid message
    mov    eax, [input3]
    call   print_int    

    mov eax, outmsg4
    call    print_string      ; print out fourth message
    mov eax, [input4]
    call    print_int   

    xor    ebx, ebx
    cmp    ecx, [guess]

    sete   bl
    neg    ebx
    mov    edx, ebx
    and    ecx, edx
    not    ebx
    and    ebx, [guess]
    or     edx, ebx

    mov    eax, outmsg5
    call   print_string
    mov    ecx, eax
    call   print_int

    mov    eax, [avermsg]
    call   print_string   ; print out final message
    mov    ecx, edx
    call   print_int      ; print out average of ebx
    call   print_nl       ; print new line

    popa
    mov eax, 0        ; return back to C
    leave
    ret

2 个答案:

答案 0 :(得分:1)

在不知道您使用的编译器的情况下查明问题并不容易。

当您尝试访问您无权访问的细分时,

细分错误会在受保护模式下停止。

您在此处声明了3个不同的细分。在致电ds之前,您必须确保.data注册表已初始化为print_string段。

read_int将数据保存到input1变量之后似乎也存在问题,该变量似乎与您用于打印邮件的段不同,但是您没有更改{{} 1}}。

我不熟悉您的编译器如何处理这些段,因此请提供其文档的链接。

答案 1 :(得分:0)

div ebx

问题似乎就在这里。你必须将edx归零,因为is是除数的高阶词,所以你可能会得到像除法溢出例外的东西。
如果不是这种情况,那么问题就出在你的一些I / O例程中