[英]When to use a particular operand constraint in extended GCC inline assembly?
[英]Extended inline assembly gcc - Calculating Quadratic Formula roots
我正在編寫一個使用gcc擴展內聯匯編代碼的程序,以編寫一個計算二次方根(根據二次方程式)的程序。 我已經編寫了所有代碼,並且不斷遇到以下錯誤:
“無效的'asm':%字母后缺少操作數編號”
嘗試編譯程序時,出現7次此錯誤。 我的主要問題是:這是什么意思,為什么會發生? 這是一項家庭作業,因此我本身並不需要解決方案,但是我無法弄清楚該錯誤消息對代碼所適用的部分(意味着變量,我在想馬上?)
這是我的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// function for checking that your assembly code is computing the correct result
double quadraticRootC(double a, double b, double c)
{
return (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
}
double quadraticRoot(double a, double b, double c)
{
// write assembly code below to calculate the quadratic root
double root;
asm(
"fld %a \n"
"fadd %%ST \n"
"fld %a \n"
"fld %c \n"
"fmulp %%ST(1) \n"
"fadd %%ST \n"
"fadd %%ST \n"
"fchs \n"
"fld %b \n"
"fld %b \n"
"fmulp %%ST(1) \n"
"faddp %%ST(1) \n"
"ftst \n"
"fstsw %%AX \n"
"sahf \n"
"fsqrt \n"
"fld %b \n"
"fchs \n"
"fdivp %%ST(1) \n"
"mov %root, %%eax \n"
"fstp %%qword, %%eax \n"
"mov $1, %%eax \n"
"jmp short done \n"
"done: \n"
:"=g"(root)
:"g"(a), "g"(b), "g"(c)
:"eax"
);
return(root);
}
int main(int argc, char **argv)
{
double a, b, c;
double root, rootC;
printf("CS201 - Assignment 2p - Hayley Howard\n"); // print your own name instead
if (argc != 4)
{
printf("need 3 arguments: a, b, c\n");
return -1;
}
a = atof(argv[1]);
b = atof(argv[2]);
c = atof(argv[3]);
root = quadraticRoot(a, b, c);
rootC = quadraticRootC(a, b, c);
printf("quadraticRoot(%.3f, %.3f, %.3f) = %.3f, %.3f\n", a, b, c, root, rootC);
return 0;
}
如果要在內聯匯編中使用符號名,則需要在約束中重新定義它們:
: [root] "=g"(root)
: [a] "g"(a), [b] "g"(b), [c] "g"(c)
然后在您的代碼中引用它們,您將使用%[root],%[a]等。對於我自己來說,我發現名稱比%0,%1等更易於閱讀。而且,在開發/測試過程中可能會添加/刪除參數,這將迫使您重新整理asm中的所有操作數,這確實很痛苦。
有關更多詳細信息,請參見此處 。
您應該使用操作數,而不是內聯匯編程序中的名稱。 只需將%root
替換為%0
,將%a
替換為%1
,將%b
替換為%2
等。
在這里查看更多詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.