X86汇编 - 处理的指令IDIV [英] X86 assembly - Handling the IDIV instruction

查看:5426
本文介绍了X86汇编 - 处理的指令IDIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个简单的C编译器,这需要一个.c文件作为输入并产生装配code(X86,AT& T公司语法)。
Everyting是好的,但是当我尝试执行IDIVQ指令,我得到一个浮点异常。这里是我输入:

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 IDIVQ instruction, I get a floating-point exception. Here's my input:

int mymain(int x){
  int d;
  int e;
  d = 3;
  e = 6 / d;
  return e;
}

这是我产生code:

mymain:
.LFB1:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    movq    %rsp, %rbp
    .cfi_offset 6, -16
    .cfi_def_cfa_register 6
    movq    %rdi, -40(%rbp)
    movq    $3, -8(%rbp)
    movq    $6, %rax
    movq    -8(%rbp), %rdx
    movq    %rdx, %rbx
    idivq   %rbx
    movq    %rax, -16(%rbp)
    movq    -16(%rbp), %rax
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE1:
    .size mymain, .-mymain

据<一个href=\"http://www.cs.virginia.edu/~evans/cs216/guides/x86.html\">http://www.cs.virginia.edu/~evans/cs216/guides/x86.html, idivq%RBX 应产生6 / D(商)的%RAX 。但我发现了一个浮点异常,而我似乎无法找出问题所在。

According to http://www.cs.virginia.edu/~evans/cs216/guides/x86.html, idivq %rbx should produce 6/d (the quotient) in %rax. But I'm getting a floating-point exception, and I can't seem to find the problem.

任何帮助将非常AP preciated!

Any help will be much appreciated!

推荐答案

的Mysticials回答第一部分是正确的, IDIV 做了128/64位除法,所以 RDX ,其持有股息高64位不能包含一个随机值的价值。但零扩展是错误的路要走。

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.

当你拥有的签署的变量,你需要的签署的延长 RAX RDX :RAX 。没有为这个特定的指令, cqto 转换四至10月的)在AT&amp; T公司和 CQO 英特尔语法。 AFAIK气体的新版本接受这两个名字。

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

这篇关于X86汇编 - 处理的指令IDIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆