nasm 64位-分段错误

时间:2018-12-17 22:16:52

标签: assembly nasm

这是代码,我不知道为什么会产生分段错误。 该程序在两个数字之间进行加法运算,将其以十进制逐个打印,然后比较数字。

可能是寄存器。

这是我复制代码的链接:https://stackoverflow.com/a/46301894/3857942

section .bss
    res: resq 8

section .data
     msg dq 'Hello, world!', 0xa  ;string to be printed
     len equ $ - msg     ;length of the string 
     msg2 dq 'Is largest',0xa,0xD
     len2 equ $ - msg2
     msg3 dq 'Is less',0xa, 0xD
     len3 equ $ - msg3
     filename dq 'hello.txt'

section .text
     global _start:     ;must be declared for linker (ld)


condition:
     cmp r8,r9
     jg _is_large
     jl _is_less
      _is_large:
          mov   rdx,len2     ;message length
          mov   rsi,msg2  ;message to write
          mov   rdi,1       ;file descriptor (stdout)
          mov   rax,1       ;system call number (sys_write)
          syscall        ;call kernel
          ret
      _is_less:
         mov    rdx,len3     ;message length
         mov    rsi,msg3 ;message to write
         mov    rdi,1       ;file descriptor (stdout)
         mov    rax,1       ;system call number (sys_write)
         syscall        ;call kernel
         ret


sum:
     mov rax,r8
     add rax,r9
     mov edi,eax
     mov rcx, 10
     push   rcx  
     mov    rsi, rsp
     .divide:
           xor edx,edx
           div ecx
           add edx,'0'
           dec rsi
           mov [rsi],dl
           test eax,eax
           jnz .divide

           lea    edx, [rsp+16 + 1]    ; yes, it's safe to truncate pointers before subtracting to find length.
           mov  rdx,3     ;message length
           mov  rdi,1       ;file descriptor (stdout)
           mov  rax,1       ;system call number (sys_write)
           syscall
           ret



_start:             ;tells linker entry point
      mov r8,100
      mov r9,101
      call sum
      call condition    
      mov   rax,60       ;system call number (sys_exit)
      syscall       ;call kernel

输出是:

     201 
     Violación de segmento (`core' generado)

调试器输出:

       58              mov     rdx,3     ;message length
       (gdb) n
       59              mov     rdi,1       ;file descriptor (stdout)
      (gdb) n
       61              mov     rax,1       ;system call number (sys_write)
      (gdb) n
       62              syscall
      (gdb) n
      201sum.divide () at Hello.asm:64
      64              ret
      (gdb) 
      0x000000000000000a in ?? ()
      (gdb) n
      No se pueden encontrar límites en la función actual

0 个答案:

没有答案