浮点异常程序集64位

问题描述 投票:0回答:1

浮点异常核心已转储。我正在使用64位汇编。我认为我从关于错误的看到的地方使用div指令时遇到错误,(人们似乎在不清除RDX的情况下得到了它)但是由于我正在用xor函数清除它不知道我的代码有什么问题。

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

_start:
mov rdi,1                                  ;tell linker entry point
mov rax,rdi
push rax
jmp loop

loop:
pop rax
cmp rax,19
jle test3
mov rax,1;quit
syscall ;quit

test3:
add rdi,1
push rdi
mov rax,rdi 
xor rdx,rdx
mov rbx,3
div rbx
cmp rdx,0
je fizz
jmp test5

test5:
mov rax,rdi 
xor rdx,rdx
mov rbx,5
div rbx
cmp rdx,0
je buzz
jmp loop 


fizz:
    mov     rdx,5                               ;message length
    mov     rcx,msg                             ;message to write
    mov     rbx,1                               ;file descriptor (stdout)
    mov     rax,4                               ;system call number (sys_write)
    syscall                               ;call kernel
jmp loop

buzz:
    mov     rdx,5                               ;message length
    mov     rcx,msg2                            ;message to write
    mov     rbx,1                               ;file descriptor (stdout)
    mov     rax,4                               ;system call number (sys_write)
    syscall                               ;call kernel
jmp loop


section     .data

msg     db  'fizz',10               
msg2     db  'buzz',10   
assembly x86-64 floating-point-exceptions
1个回答
0
投票

您的程序生成错误的syscall

从系统V ABI,syscall的参数在以下寄存器中:rdirsirdxrcxr8r9

[另外,1代表sys_write4代表sys_statsys_exit代表60)。

浮点异常仅在除数为0或未清除rdx时发生。由于两者都不是,所以不应该发生。

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

_start:
mov r8,1                                  ;tell linker entry point
mov rax,r8; rdi is needed for syscalls, have to use another register 
          ; or save it before preparing to call the kernel
push rax
jmp loop

loop:
pop rax
cmp rax,19
jle test3
mov rax, 60; sys_exit
xor rdi, rdi; clear rdi(exit code)
syscall

test3:
add r8,1
push r8
mov rax,r8 
xor rdx,rdx
mov rbx,3
div rbx
cmp rdx,0
je fizz
jmp test5

test5:
mov rax,r8 
xor rdx,rdx
mov rbx,5
div rbx
cmp rdx,0
je buzz
jmp loop 


fizz:
    mov     rdx,5                               ;message length
    mov     rsi,msg                             ;message to write
    mov     rdi,1                               ;file descriptor (stdout)
    mov     rax,1                               ;system call number (sys_write)
    syscall                               ;call kernel
jmp loop

buzz:
    mov     rdx,5                               ;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
jmp loop


section     .data

msg     db  'fizz',10               
msg2     db  'buzz',10   

输出为:

fizz
buzz
fizz
fizz
buzz
fizz
fizz
fizz
buzz

您可以在此表中找到syscall号:https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/

© www.soinside.com 2019 - 2024. All rights reserved.