X86 assembly - Handling the IDIV instruction

后端 未结 2 689
抹茶落季
抹茶落季 2020-12-01 16:06

I am currently writing a simple C compiler, that takes a .c file as input and generates assembly code (X86, AT&T syntax). Everyting is good, but when I try to execute a

2条回答
  •  情深已故
    2020-12-01 16:38

    The first part of Mysticials answer is correct, idiv does a 128/64 bit division, so the value of rdx, which holds the upper 64 bit from the dividend must not contain a random value. But a zero extension is the wrong way to go.

    As you have signed variables, you need to sign extend rax to rdx:rax. There is a specific instruction for this, cqto (convert quad to oct) in AT&T and cqo in Intel syntax. AFAIK newer versions of gas accept both names.

    movq    %rdx, %rbx
    cqto                  # sign extend rax to rdx:rax
    idivq   %rbx
    

提交回复
热议问题