0

I am recently learning assembly language and I am trying to implement a function, that takes two parameters, first one being a value and 2nd one being an array with length 3. Then I want to store value%16, and (value/16)%16 in the array respectively. However when I implement it, it generates a floating point exception:

    subq $8,%rsp
    movq $0,%r10
    movq %rdi,%rax /*store input into %rax*/
    movq $16,%rbx  /*store 16 into %rbx*/
    idivq %rbx     /*store dividend in rax, and remainder in rdx*/

    movq %rdx,(%rsi,%r10) /*store remainder in %rsi[0]*/
    addq $1,%r10 /*increase %r10 by 1*/

    idivq %rbx /*do the division again*/
    movq %rdx,(%rsi,%r10)
    addq $8,%rsp

When I test it with

  char buf[16];
  hex_format_byte_as_hex("h", buf);
  ASSERT(0 == strcmp(buf, "48"));

it generates a floating point exception. Why is this the case? Can someone point out what I am doing wrong in my function? Thank you in advance.

Michael
  • 57,169
  • 9
  • 80
  • 125
M. Chen
  • 203
  • 1
  • 6
  • 1
    `idivq` with a single operand takes `rdx:rax` as input. You did not initialise `rdx`. – ecm Sep 28 '20 at 16:58
  • And the easiest way to initialise `rdx` is to issue a `cqo` instruction right before the `idiv`. – fuz Sep 29 '20 at 09:39

0 Answers0