我在这个汇编代码“nasm”中遇到分段错误

时间:2021-04-25 14:10:51

标签: assembly x86-64

我写了这个汇编代码,但我遇到了分段错误。

我的代码显示一年是否多出一天,因此它有 366 天而不是常规的 365 天。这种情况发生在能被 4 但不能被 100 整除的年份,或者能被 100 和 400 整除的年份。 所以 2008 年是因为除以 4 而不是 100,2000 年是因为除以 100 和 400,但 2200 不是因为除以 100 而不是 400 .

我尝试调试,当调用 div100 时,在我的 if 语句中发生了分割,它进行了分割,但在返回行中发生了分割。

不知道为什么会这样。请帮忙。

输入年份:[2008 Segmentation fault (core dumped)

代码:

extern printf, scanf  

global _start

section .data

format db '%d',0  
message: db " %d is a leap year.",10,0 ;    
message2: db " %d is not a leap year",10,0 ;  

message3: db "Enter a year: ",10,0

len1: equ $ - message
len2: equ $ - message2
len3: equ $ - message3

divisor1:   dq 4
divisor2: dq 100
divisor3:   dq 400
  
section .bss
year:   resq 1

section .text


_start:
   call _intro ;asking to enter year

scan:  ;getting the year

mov rdi, format

mov rsi, year   

mov rax, 0

call scanf


if:  ;the case the year is divisble by 4 but not 100

   call _div4

   cmp rdx,0

   jne elseIf

   je _div100 ;

   cmp rdx,0

   je elseIf

   jne _isLeapYear

   jmp exit


elseIf: ;case the year is divisible by 100 and 400

   call _div100

   cmp rdx, 0

   jne else

   je _div400

   cmp rdx, 0

   jne else

   je _isLeapYear

   jmp exit


else: ;the case that all the other cases failed 

   call _isNotLeapYear

  
  
exit:  ;ending program

   mov rax,60 ;

xor rdi, rdi ;

syscall ;


;functions

_div4:

mov rax, [year]

mov rdx, 0

idiv qword[divisor1]

   ret


_div100:

mov rax, [year]

mov rdx, 0

idiv qword[divisor2]

ret ; I think segmentation is happening right here !!!


_div400:

mov rax, [year]

mov rdx, 0

   idiv qword[divisor3]

   ret

  
_isLeapYear:

   mov rax,0

   mov rdi, message

   mov rsi,[year]

   call printf

   jmp exit


_isNotLeapYear:

mov rax,0

mov rdi, message2

mov rsi,[year]

call printf

   jmp exit


_intro:

mov rax,1 ; 
  
mov rdi,1 ;  
 
mov rsi, message3 ;

   mov rdx,len3 ;   

syscall ;

ret

0 个答案:

没有答案