【发布时间】:2021-12-10 11:28:44
【问题描述】:
我想将我的程序从 c++ 翻译(或手动编译)为 HLA。程序读取输入的数字。然后减去三和十或仅十,确定该值是以零还是三结尾。连续三个这样的数字赢得比赛!一个不以这些数字结尾的值就输了。
我不知道如何使用 HLA 中的 AND 运算符连接两个条件来执行 while 循环。
while ((iend != 1) && (iscore < 3))
这是我用 C++ 编写的完整代码,我想把它翻译成 HLA:
#include <iostream>
using namespace std;
int main() {
int inum;
int iend = 0;
int iscore = 0;
int icheckthree; //To check if it ends in 3
int icheckzero; //To check if it ends in zero
while ((iend != 1) && (iscore < 3)) {
cout << "Gimme a number: ";
cin >> inum;
//Case 1: ends in three
icheckthree = inum - 3;
while (icheckthree > 0) {
icheckthree = icheckthree - 10;
if (icheckthree == 0) {
cout << "It ends in three!" << endl;
iscore++;
}
}
icheckzero = inum;
while (icheckzero > 0) {
icheckzero = icheckzero - 10;
}
//Case 2: ends in zero
if (icheckzero == 0) {
cout << "It ends in zero!" << endl;
iscore++;
}
//Case 3: Loose the game
else {
if (icheckzero != 0) {
if(icheckthree != 0) {
iend = 1;
}
}
}
if (iend == 1) {
cout << "\n";
cout << "Sorry Charlie! You lose the game!" << endl;
}
else if (iscore == 3) {
cout << "\n";
cout << "You Win The Game!" << endl;
} else {
cout << "Keep going..." << endl;
cout << "\n";
}
}
}
【问题讨论】:
-
通常只有两个独立的 cmp/jcc,任何一个都可以让你脱离循环。看看 C 编译器如何在 godbolt.org 上执行此操作。 (当然 C 编译器会使用普通的 asm,而不是 HLA,但指令是一样的。)
标签: c++ assembly compilation translate hla