分段故障错误与大会code程序发生 [英] Segmentation-fault error happening with Assembly code program

查看:123
本文介绍了分段故障错误与大会code程序发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直运行我的code时,得到一个分段错误。一切都已经编好,但我似乎无法得到它做我想做的。该方案是要求用户输入3的整数,然后问他们认为这些数字的平均值将是用户,考虑到这一点,然后与用户是否猜中回来

 段。数据
;
;输出字符串
;
PROMPT1 DB输入一个正整数:0
PROMPT2 DB输入第二个正整数:0
prompt3 DB进入第三个正整数:0
prompt4 DB输入自己的平均水平的猜测:0
outmsg1 DB您输入0
outmsg2分贝和0
outmsg3分贝和0
outmsg4 DB你猜,平均为0
outmsg5 DB你没你猜正确的?(0 =否,1 =是),0
avermsg分贝的数字的平均值为0
段的.bss输入1 RESD 1
输入2 RESD 1
输入3 RESD 1
INPUT4 RESD 1
猜RESD 1段的.text
        全球主要
主要:
        进入0,0;安装例程
        PUSHA        MOV EAX,PROMPT1;打印出PROMPT1
        电话print_string        调用read_int;读整
        MOV [输入1],EAX;店内整数到输入1
        MOV EAX,PROMPT2;打印出PROMPT2
        电话print_string    调用read_int;读整
    MOV [输入2],EAX;店内整数到输入2    MOV EAX,prompt3;打印出prompt3
        电话print_string    调用read_int;读整
    MOV [输入3],EAX;店内整数到输入3    MOV EAX,prompt4;打印出prompt4
    电话print_string    调用read_int;读整
    MOV [猜想],EAX
    MOV EAX,[输入1];在输入1 EAX = DWORD
    添加EAX,[输入2]; EAX + =在输入2 DWORD
    添加EAX,[输入3]; EAX + =在输入3 DWORD
    MOV EBX,3
    DIV EBX;除以3的总和
    MOV ECX,EAX;腾出EAX,把商到ECX    dump_regs 1;打印出寄存器的值;下一个打印出结果
    MOV EAX,outmsg1
    调用print_string;打印出第一条消息
    MOV EAX,[输入1]
    电话print_int    MOV EAX,outmsg2
    调用print_string;打印出第二个消息
    MOV EAX,[输入2]
    电话print_int    MOV EAX,outmsg3
    调用print_string;打印出THRID消息
    MOV EAX,[输入3]
    电话print_int    MOV EAX,outmsg4
    调用print_string;打印出第四个消息
    MOV EAX,[INPUT4]
    电话print_int    XOR EBX,EBX
    CMP ECX,[猜想]    SETE BL
    NEG EBX
    MOV EDX,EBX
    和ECX,EDX
    不EBX
    和EBX,[猜想]
    或EDX,EBX    MOV EAX,outmsg5
    电话print_string
    MOV ECX,EAX
    电话print_int    MOV EAX,[avermsg]
    调用print_string;打印出最后的消息
    MOV ECX,EDX
    调用print_int;打印出EBX的平均
    调用print_nl;打印新的生产线    波帕
    MOV EAX,0;返回到Ç
    离开
    RET


解决方案

这是不容易查明问题不知道你使用的编译器。

分段故障 hapens在保护模式下,当您试图访问一个段你没有访问权限的。

您在这里声明3 diferent段。你必须确保你的 DS 寄存器初始化为你的。数据段之前调用 print_string

这似乎也是有问题的,经过 read_int 将数据保存到这似乎是一个在输入1 变量不同的细分比您用于打印邮件,但你不改变 DS

我不熟悉你的编译器究竟是如何处理这些段,所以请给一个链接到它的文档。

I keep getting a segmentation fault error when running my code. Everything has compiled well, but I can't seem to get it to do what I want. The program is to ask the user to enter 3 integers, then ask the user what they think the average of the numbers would be, take that into account and then come back with whether or not the user guessed correctly

    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

解决方案

It is not easy to pinpoint the problem without knowing the compiler you use.

Segmentation fault hapens in protected mode, when you try to access a segment you have no permissions to access.

You declare 3 diferent segments here. You have to make sure your ds register is initialized to your .data segment, before calling print_string.

It also seems problematic that after read_int you save the data to the input1 variable which seems to be in a different segment than that you used for printing the message, but you do not change ds.

I'm not familiar how exactly your compiler handles these segments, so please give a link to its documentation.

这篇关于分段故障错误与大会code程序发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆