0

I am using a nasm compiler (rextester) for assembly. I am trying to divide 300 by 2 to get the quotient. I am able to do this for small numbers like 4/2 and my code works with no problem. When I try to do a big number like 300 I get a floating point exception. Can anyone help me figure out how to get my code to work please?

section .bss
    num1 resb 3
    num2 resb 1
    op resb 1
    result resb 3
    onedig resb 1
    tendig resb 1
    hundig resb 1

section .text
    global _start

_start:
;first number input    
    mov eax,3            ; system call (sys_read)
    mov ebx,0            ; stdin
    mov ecx,num1         ; declared var
    mov edx,3            ; byte length
    int 80h              ; Call the kernel
;operator input    
    mov eax,3            ; system call (sys_read)
    mov ebx,0            ; stdin
    mov ecx,op         ; declared var
    mov edx,1            ; byte length
    int 80h              ; Call the kernel
;second number input    
    mov eax,3            ; system call (sys_read)
    mov ebx,0            ; stdin
    mov ecx,num2         ; declared var
    mov edx,1            ; byte length
    int 80h              ; Call the kernel
;division
    mov ax,[num1]
    sub ax, '0'
    mov bl,[num2]
    sub bl, '0'
    div bl
    mov [result],al

    add [result], dword '0'

    mov eax,4            ; system call (sys_write)
    mov ebx,1            ; stdout
    mov ecx,result         ; declared var
    mov edx,3            ; byte length
    int 80h              ; Call the kernel

end:
    mov eax,1            ; The system call for exit (sys_exit)
    mov ebx,0            ; Exit with return code of 0 (no error)
    int 80h;
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • 2
    One thing is that you cannot convert an ASCII string to a DWORD value with ` mov ax,[num1]; sub ax, '0'`. You'd have to write a conversion routine (or search for it on SO). – zx485 Oct 17 '20 at 21:23
  • 2
    Single-step with your debugger and see what is actually in `ax` and `bl` when you execute `div bl`. Then look up the precise behavior of the `div` instruction. – Nate Eldredge Oct 17 '20 at 21:33
  • 1
    This doesn't seem to have anything to do with webassembly so I deleted that tag. I also added the x86 tag; every question about assembly language should be tagged with the relevant CPU architecture. – Nate Eldredge Oct 17 '20 at 21:41
  • 1
    *I am using a nasm compiler (rextester) for assembly* - If that online site doesn't let you single-step in a debugger, stop wasting your time with it (and everyone else's). Trying to figure out what happened from just a fault message is just making things harder for yourself for no reason. OnlineGDB has GAS syntax assembly, but unfortunately not NASM, if you want to avoid installing a GNU/Linux dev environment locally for some reason. https://www.onlinegdb.com/online_gcc_assembler – Peter Cordes Oct 17 '20 at 22:03
  • Related: [Why does integer division by -1 (negative one) result in FPE?](https://stackoverflow.com/q/46378104) re: divide overflow -> #DE -> SIGFPE – Peter Cordes Oct 18 '20 at 00:43

0 Answers0