Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If statement 如何在装配中使用if-else或switch操作符?_If Statement_Assembly_Switch Statement - Fatal编程技术网

If statement 如何在装配中使用if-else或switch操作符?

If statement 如何在装配中使用if-else或switch操作符?,if-statement,assembly,switch-statement,If Statement,Assembly,Switch Statement,如何在汇编中使用C/C++中的多个if-else语句或switch运算符 类似于C中的内容: if ( number == 2 ) printf("TWO"); else if ( number == 3 ) printf("THREE"); else if ( number == 4 ) printf("FOUR"); 或使用开关: switch (i) { case 2: printf("TWO"); break;

如何在汇编中使用C/C++中的多个if-else语句或switch运算符

类似于C中的内容:

if ( number == 2 )
  printf("TWO");
else if ( number == 3 )
  printf("THREE");
else if ( number == 4 )
  printf("FOUR");
或使用开关:

switch (i)
     {
        case 2:
           printf("TWO"); break;
        case 3:
           printf("THREE"); break;
        case 4:
           printf("FOUR"); break;
     }

谢谢。

体系结构对于细节非常重要,但这里有一些psuedo代码可以满足您的需要

... # your code
jmp SWITCH

OPTION1:
... # do option 1
jmp DONE
OPTION2:
... # do option 2
jmp DONE
Option3:
... # do option 3
jmp DONE

SWITCH:
if opt1:
jmp OPTION1
if opt2:
jmp OPTION2
if opt3:
jmp OPTION3

DONE:
... #continue your program

详细答案取决于您正在为其编写汇编语言的特定机器指令集。基本上,您可以编写汇编代码来执行C语言系列测试(if语句)和分支

在伪汇编中,它可能如下所示:

load  r1, number            // load the value of number into register 1
cmpi r1, 2                  // compare register 1 to the immediate value 2
bne  test_for_3             // branch to label "test_for_3" if the compare results is not equal
call printf                 // I am ignoring the parameter passing here
...                         // but this is where the code goes to handle
...                         // the case where number == 2
branch the_end              // branch to the label "the_end"
test_for_3:                 // labels the instruction location (program counter)
                            // such that branch instructions can reference it
cmpi r1, 3                  // compare register 1 to immediate value 3
bne  test_for_4             // branch if not equal to label "test_for_4"
...                         // perform printf "THREE"
branch the_end              // branch to the label "the_end"
test_for_4:                 // labels the instruction location for above branch
cmpi r1, 4                  // compare register 1 to immediate value 4
bne the_end                 // branch if not equal to label "the_end"
...                         // perform printf "FOUR"
the_end:                    // labels the instruction location following your 3 test for the value of number

为什么不创建一个包含这些语句的C程序,构建它,然后查看生成的汇编代码呢?通常,您可以使用条件跳转来适当地转移控制权。具体细节将取决于您心目中的特定体系结构。这两段代码是不等效的,
开关
有故障,因此对于i==2,将执行所有打印。您知道如何执行单个if吗?如果没有,那就先学习。仅此一点就有很多细节(如反转条件),并且它可以推断其他情况。在某些情况下,switch语句可以作为跳转表更有效地执行。with开关变量的值将作为跳转表的索引。