2011-03-19 107 views
1

运行我的代码时,我总是收到分段错误错误。一切都编好了,但我似乎无法让它做我想做的事。该方案是要求用户输入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

omg为什么要装配 – dynamic 2011-03-19 20:37:35

+0

请格式化代码,以便它在这里可读,并确定它发生故障的确切位置。 asm很难阅读,因为它是。 – Mat 2011-03-19 20:40:41

+0

感谢digitalRoss,我不确定分段故障发生在哪里,它在运行时发生。 – BDilly 2011-03-19 20:44:18

回答

0
div ebx 
猜到了回来

问题似乎在这里。您必须将edx归零,因为is是除数的高位字,所以您可能会得到类似的分区溢出异常。
如果情况并非如此,可能问题出在您的某些I/O例程上

+0

我不认为这是可能的。分段错误显然是一个内存处理问题。 – vbence 2011-03-20 00:26:17

1

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

分割错误在您尝试访问您无权访问的细分受众群时处于保护模式。

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

这也似乎问题在于read_int后,将数据保存到input1变量这似乎是在不同的细分比您用于打印的消息,但你不改变ds

我不太清楚你的编译器如何处理这些段,所以请给它的文档链接。