关于汇编语言条件选择

时间:2012-11-08 10:33:30

标签: assembly

我对c语言比较熟悉,但是我们如何将下面的c语言编码写成汇编语言?我试过但总是失败。

if(a==4)
{
   routine1();
}
else if(a==5)
{
    routine2();
}
else if(a==6)
{
    routine3();
}

2 个答案:

答案 0 :(得分:2)

一种可能性是使用goto重写它,然后它应该是直接移植到您使用的任何asm语言。

if (a != 4) goto not4;
routine1();
goto end;

not4: if (a != 5) goto not5;
routine2();
goto end;

not5: if (a != 6) goto not6;
routine3();
end:

答案 1 :(得分:0)

在x86中它会是这样的,也可以查看链接以获得命令的深度描述http://www.gabrielececchetti.it/Teaching/CalcolatoriElettronici/Docs/i8086_instruction_set.pdf

a100 
mov al, 0500;move whats in memory location 0500 to al register
mov bl,4; mov 4 into bl register
cmp al,bl;this compares them basically subtracting them so 0 is equal
jz label1;the label is another memory location that you would jump to if they are equal
mov bl,5; if it doesnt jump then the code will continue to run
cmp al,bl
jz label2
mov bl,6
cmp al,bl
jz label3
int 3; to end program or you can use ret 

...

label1 call 0200;call instruction runs the code that is at the memory location stated
label2 call 0300
label3 call 0400

希望这有助于记住这是x86 !! 快乐编码!!