0

I want to write a simple program that takes two integer numbers, divides them, converts the result into string and prints it to a screen.

Here's the part of code I got. It converts floating point number to an integer portion. There's an error there somewhere. It causes the floating point exception. Here I've tried to explain myself what actually happens within my program

_getTempIntegerPortion:
        mov r8, temp
        mov rcx, 10
        xor rdx, rdx

; Input - rax as integer, r8 as pointer to temp, rcx is 10
_assignTempIntegerPortion:
        div rcx 
        cmp rax, 0
        mov rax, rdx
        add rdx, 48
        mov [r8], dl
        inc r8
        jne _assignTempIntegerPortion
        ret

MindW1n
  • 11
  • 4
  • You need to zero RDX *inside* the loop, before every `div`. Look at registers with a debugger when you fault: you'll have RDX = '0' + something, so it'll be greater than 10. Also, `inc r8` / `jne` will loop until the pointer wraps around, so it'll segfault way before that. – Peter Cordes Dec 09 '22 at 18:00
  • Thanks for answer and I've tried, but it didn't help. – MindW1n Dec 09 '22 at 18:18
  • `div rcx` with RDX=0 can't fault (with non-zero RCX). If you're still getting SIGFPE, you have another `div` somewhere else in your program that's also faulting. Use a debugger to find out where. See the bottom of https://stackoverflow.com/tags/x86/info for asm debugging tips with GDB. (And/or see [How do I print an integer in Assembly Level Programming without printf from the c library? (itoa, integer to decimal ASCII string)](https://stackoverflow.com/a/46301894) for a working loop.) – Peter Cordes Dec 09 '22 at 18:55
  • Just noticed you are apparently trying to exit after division produces a `0`. You need `cmp rax, 0` to be the last thing that sets FLAGS before `jne`, so it needs to come after the `inc`. Also, `mov rax, rdx` is wrong; you want `x /= 10` across loop iterations, not `x %= 10` – Peter Cordes Dec 09 '22 at 19:01

0 Answers0