2

I'm using Assembly x86 in NASM and what I want it to do is divide 2 numbers by 10, but it throws a floating point exception (core dumped).

section .data

    mensaje     db "Ingrese el primer numero: ", 0xA
    longitud    equ $ - mensaje
    mensaje2    db "Ingrese el segundo numero: ", 0xA
    longitud2   equ $ - mensaje2
    invalido    db "Algun caracter ingresado es invalido", 0xA
    longitud1   equ $ - invalido
    numeros     db "0123456789", 0xA

section .bss

    num1 resb 100
    num2 resb 100

section .text

_inicio:

    mov rax, 1
    mov rdi, 1
    mov rsi, mensaje
    mov rdx, longitud
    syscall

    mov rax, 0
    mov rdi, 0
    mov rsi, num1
    mov rdx, 100
    syscall

    mov rax, 1
    mov rdi, 1
    mov rsi, mensaje2
    mov rdx, longitud2
    syscall

    mov rax, 0
    mov rdi, 0
    mov rsi, num2
    mov rdx, 100
    syscall

    mov rcx, 0
    ;mov rdx, num1
    jmp toInt

volver1:

    mov rdx, rax
    jmp toAscii

volver2:

    mov rax, 1
    mov rdi, 1
    mov rsi, rdx
    mov rdx, 100
    syscall

    jmp end

toInt:

    mov bl, byte[num1+rcx]
    cmp bl, 0xA
        je volver1
    cmp bl, '0'
        jl error
    cmp bl, '9'
        jg error
    sub bl, 35H
    add rax, rbx
    inc rcx
    mov rsi, 10
    or rax, rax
    mul rax
    xor rbx, rbx
    jmp toInt

toAscii:

    mov rcx, 10
    mov rax, rdx
    div rax
    cmp rax, 0
        je volver2  
    mov dl, byte[numeros+cl]
    shl rdx, 8
    jmp toAscii

end:

    mov rax, 60
    xor rdi, rdi
    syscall

error:

    mov rax, 1
    mov rdi, 1
    mov rsi, invalido
    mov rdx, longitud1
    syscall

    jmp end

How it is written right now it should just ask me for two numbers, then it should turn the characters into integers and then back into characters. However, it just gives me a Floating point error.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • https://stackoverflow.com/questions/16928942/why-does-integer-division-by-zero-result-in-a-floating-point-exception – Hans Passant Sep 02 '19 at 11:11

1 Answers1

1

Your one div instruction is in toAscii, div eax. The div instruction will divide the unsigned number in edx:eax by the operand (eax in this case). With your values it will result in an overflow (since the result is too big to fit in eax) and the exception you get.

It looks like you want to divide edx by ecx. This should be

mov eax,edx
xor edx,edx    ; zero out upper 32 bits of numerator
div ecx        ; divide edx:eax by ecx

This will leave the result in eax with the remainder in edx. The rest of that procedure will need some appropriate changes to get the expected output, which I've left as an exercise for the reader.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56