NASM编程 - 分段故障(核心转储)

时间:2014-01-17 10:49:16

标签: arrays x86 nasm

我的程序非常简单。它需要用户提供两个输入:

  1. 一个数组(只有2位数的正数)
  2. 要搜索的2位数字
  3. 输出:给定号码的次数。发生在数组

    样品

      

    输入数组的大小
      03
      输入数字
      10个
      输入数字
      12个
      输入数字
      10个
      输入要搜索的号码
      10

    您的电话号码 02

    似乎一切正常,但这个程序给出了错误

      

    0分段错误(核心转储)

    请帮我解决这个问题

    enter code here
    
      section .data
    
     msg1: db "Enter the size of array",10    
     len1: equ $-msg1    
    
    msg2: db "Enter a number",10    
    len2: equ $-msg2     
    
    msg3: db "Enter the number to be searched :"     
    len3: equ $-msg3      
    
    msg4: db "Your entered number appeared "    
    len4: equ $-msg4    
    
    section .bss      
    temp: resb 1     
    array: resb 100     
    d1: resb 1      
    d0: resb 1     
    size: resb 1     
    num: resb 1     
    ele: resb 1     
    time: resb 1     
    
    section .text     
    
     global _start:     
    
     _start:      
     ;Getting the size of array     
    
     mov eax,4           
     mov ebx,1          
     mov ecx,msg1            
     mov edx,len1          
     int 80h             
    
     mov eax,3           
     mov ebx,0        
     mov ecx,d1         
     mov edx,1           
     int 80h              
    
     mov eax,3            
     mov ebx,0           
     mov ecx,d0           
     mov edx,2          
     int 80h            
    
    
     sub byte[d1],48           
     sub byte[d0],48          
    
     mov al,byte[d1]          
     mov bl,10           
     mul bl          
     add byte[d0],al             
     mov al,byte[d0]           
     mov byte[size],al           
     mov byte[temp],al             
    
     mov ebx,array          
    
     reading:           
     push rbx           ;preserves the value of ebx     
    
    
      mov eax,4   
      mov ebx,1  
     mov ecx,msg2   
     mov edx,len2   
     int 80h   
    
     mov eax,3   
      mov ebx,0   
     mov ecx,d1     
     mov edx,1    
     int 80h     
    
     mov eax,3    
     mov ebx,0    
     mov ecx,d0    
     mov edx,2    
     int 80h    
    
     sub byte[d1],48     
     sub byte[d0],48     
    
     mov al,byte[d1]     
     mov bl,10     
     mul bl     
     add byte[d0],al      
     mov al,byte[d0]      
    
     pop rbx         
    
     mov byte[ebx],al          
     add ebx,1          
     dec byte[temp]          
     cmp byte[temp],0            
     jg reading              
    
     mov eax,4              
     mov ebx,1          
     mov ecx,msg3            
     mov edx,len3            
     int 80h          
    
     mov eax,3         
     mov ebx,0         
     mov ecx,d1          
     mov edx,1          
     int 80h          
    
     mov eax,3       
     mov ebx,0            
     mov ecx,d0             
     mov edx,2          
     int 80h             
    
     sub byte[d1],48                 
     sub byte[d0],48              
    
     mov al,byte[d1]              
      mov bl,10             
      mul bl             
     add byte[d0],al                
     mov al,byte[d0]                  
    
     mov byte[ele],al              
    
     mov cl,byte[size]                
     mov byte[temp],cl              
    
     mov byte[time],0              
    
     search:             
    
     mov al,byte[ebx]             
     cmp byte[ele],al           
     jne nf               
     jmp f             
     mov cl,0
    
    
     f:   add ebx,1            
       add byte[time],1             
        dec byte[temp]           
       cmp byte[temp],cl           
        jg search          
        jmp next
    
    
     nf:        
         add ebx,1         
         add byte[time],0           
         dec byte[temp]       
         cmp byte[temp],cl          
         jg sea rch        
         jmp next
    
    
      next: 
    
      mov eax,4        
      mov ebx,1         
      mov ecx,msg4          
      mov edx,len4          
      int 80h           
    
      movzx ax,byte[time]           
      mov bl,10           
      div bl              
      mov byte[d1],al           
      mov byte[d0],ah          
    
       add byte[d0],48            
       add byte[d1],48         
    
       mov eax,4        
       mov ebx,1        
       mov ecx,d1            
       mov edx,1          
       int 80h           
    
       mov eax,4           
       mov ebx,1         
       mov ecx,d0           
       mov edx,1``        
       int 80h      
    
        mov eax,1      
        mov ebx,0        
        int 80h       
    

2 个答案:

答案 0 :(得分:1)

global _start
section .text
_start:

mov rax,1
mov rdi,1
mov rsi,msg
mov rdx,msglen
syscall

mov rax,0
mov rdi,0
mov rsi,no1
mov rdx,5
syscall

mov rax,1
mov rdi,1
mov rsi,msg1
mov rdx,msg1len
syscall

mov rax,0
mov rdi,0
mov rsi,no2
mov rdx,5
syscall

mov rax,[no1]
sub rax,30h
mov rbx,[no2]
sub rbx,30h
add rax,rbx
add rax,30h
mov [res],rax
mov rdx,2
syscall

mov rax,1
mov rdi,1
mov rsi,res
mov rdx,2
syscall

mov rax,60h
mov rdx,0
syscall

section .data
msg: db "Enter 1st Number",0x0a
msglen equ $ -msg
msg1: db "Enter 2st Number",0x0a
msg1len equ $ -msg1
msg2:db "Addition is==",0x0a
msg2len equ $ -msg2

section .bss
no1: resb 5
no2: resb 5
res: resb 15

答案 1 :(得分:0)

看看来源,我注意到了一件事。我认为你的'搜索'循环超出界限,因为你无法初始化寄存器cl;指令mov cl,0目前是死代码;它在无条件跳跃之下,所以永远不会被执行。

但正如我之前所说,如果有疑问,请使用调试器。