如何翻译if .. else语句与goto?

问题描述 投票:-2回答:1

我必须将嵌套的if..else条件转换为转到C代码中的标签。我知道我必须从外部if-s开始,但是如何用goto翻译内部if-s?

Example: if(condition)
          {
            if(condition)
            {
             if(condition)
              {
                statements;
              }
              if(condition) return;
              statements;
            }  
          }
        else statements;
c if-statement goto
1个回答
-1
投票

实际上,您不需要翻译Ìf statement或任何属于C的声明。您可以直接使用这样,它会自动转换为Assembly

int main(void)
{
    unsigned char SerData = 0x44;
    unsigned char TempSerData;
    unsigned char x;
    TempSerData = SerData;
    DDRC |= (1<<SerPin); //configure PORT C pin3 as output
    for (x=0;x<8;x++)
    {
       if (TempSerData & 0x01) // check least significant bit
             PORTC |= (1<<serPin); // set PORT C pin 3 to 1
       else
          PORTC &= ~(1<<serPin); // set PORT C pin 3 to 0
       TempSerData = TempSerData >> 1; // shift to check the next bit
    }
    return 0;
}

但是,如果你想翻译if,你可以使用这样的东西,但正如我所说,你不需要转换它,或者你不需要C来完成这项工作。

int x = 0, y = 1;
(x >= y) ? goto A: goto B
A: // code goes here 
    goto end
B: // code goes here  
    goto end
end: return 0;

在装配中,您可以非常轻松地完成。例如,在Àtmega128中:

ldi r16, 0x00 ; load register 16 with 0
ldi r17, 0x01 ; load register 17 with 1

sub r17, r16  ; take the difference
brcs if_label
else_label:             ; do some operation on that line or on the other lines
          rjmp end
if_label:               ; do some operation on that line or on the other lines     
end: rjmp end           ; program finishes here
© www.soinside.com 2019 - 2024. All rights reserved.