是什么导致分段错误(核心转储)?以及如何解决?

时间:2014-05-04 03:22:49

标签: linux assembly

这是我的代码,它应该是用户添加,乘法,除法或减去两个整数。当我执行它时,我可以输入整数和运算符,甚至可以输入'字符串打印出来但不是打印出问题的实际答案,而是说“分段故障(核心转储)”。我知道当程序试图使用或写入它无法访问的内存时会发生这种情况。我是汇编语言的新手,我花了很长时间才能做到这一点。请帮我。

1 ; Function: adds, subtracts, divides or multiplies two
2 ; integers the user inputs
3 ;
4 ;
5 ; --------------------------------------------------------------------------    --
6
7 %include "CPsub32.inc"
8 %include "Macros_CPsub32.inc"
9
10 section .data
11
12 CRLF     db 0Ah,0Dh,00h
13 prompt1  db 0Ah,0Dh,'Please enter an Integer?',0Ah,0Dh,00h
14 prompt2  db 0Ah,0Dh,'Please enter an Operator?',0Ah,0Dh,00h
15 answer   db 0Ah,0Dh,'The answer is: ',0Ah,0Dh,00h
16
17 operand1 DD 0d
18 operand2 DD 0d
19 operator db 00h
20 result   DD 0d
21
22
23 ipbuffer db "   "
24 ipbuflen equ $-ipbuffer
25
26
27 section   .text
28           global main
29
30 main      equ   $
31           mov   edx, prompt1            ;move prompt1 to reg edx
32           call  WriteString             ;write out prompt1
33           mov   edx, ipbuffer
34           mov   ecx, ipbuflen
35
36           call  ReadString              ;read users input (operand1)
37           mov   edx, ipbuffer
38           mov   ecx, 4                  ;something with 4 bytes in reg ecx
39           call  ParseDecimal32          ;change users input (op1) into decim    al
40           mov   dword [operand1], eax   ;load user input (op1) into operand1    ?
41
42           mov   edx, prompt2            ;move prompt2 to reg edx
43           call  WriteString             ;write out prompt2
44           mov   edx, ipbuffer
45           mov   ecx, ipbuflen
46
47           call  ReadString              ;read user input (operator)
48           mov   edx, [ipbuffer]
49           mov   [operator], dl          ;move user input (operator) to opera    tor
50
51           mov   edx, prompt1            ;move prompt1 to reg edx again
52           call  WriteString             ;write out promt1 again
53           mov   edx, ipbuffer
54           mov   ecx, ipbuflen
55
56           call  ReadString              ;read users input (operand2)
57           mov   edx, ipbuffer
58           mov   ecx, 4                  ;something with 4 bytes in reg ecx
59           call  ParseDecimal32          ;change user input (op2) into decima    l
60           mov   dword [operand2], eax   ;move user input (op2) into operand2    ?
61
62           mov   edx, answer             ;move answer to reg edx
63           call  WriteString             ;write out "answer is"
64           mov   edx, ipbuffer
65           mov   ecx, ipbuflen
66
67           mov   dl, [operator]          ;move operator to dl
68           mov   eax, [operand1]         ;move operand1 to eax
69           mov   ebx, [operand2]         ;move operand2 to ebx
70
71
72           cmp   dl, 2Bh         ;is this a + sign
73           je    myAdd
74           cmp   dl, 2Dh  ;is this a - sign
75           je    mySub
76           cmp   dl, 2Ah  ;is this a * sign
77           je    myMul
78           cmp   dl, 2Fh  ;is this a / sign
79           je    myDiv
80
81           mov   eax, [result]   ;mov result to reg eax
82           call  WriteString     ;write out the result
83           mov   edx, ipbuffer
84           mov   ecx, ipbuflen
85
86           jmp   myExit
87
88 myAdd   equ   $
89         add     eax,ebx                 ;add operand1 and operand2
90         mov     dword[result],eax       ;move sum to result
91
92 mySub   equ     $
93         sub     eax,ebx                 ;subtract operand1 and operand2
94         mov     dword [result], eax
95
96 myDiv   equ     $
97         mov     ecx, ebx
98         div     byte[ecx]                       ;divide operand1 by operand2
99         mov     dword [result], eax
100
101 myMul   equ     $
102         xor     edx,edx
103         mul     byte[ebx]                       ;multiply operand1 and operand2
104         mov     dword [result], eax
105
106 myExit    equ   $

0 个答案:

没有答案