I have to translate nested-if..else conditions to goto labels in C code. I know that I have to start with outer if-s, but how to translate the inner if-s with goto?
Example: if(condition)
{
if(condition)
{
if(condition)
{
statements;
}
if(condition) return;
statements;
}
}
else statements;
Actually, You do not need to translate Ìf statement
or any statement that belongs to C
.
You could use directly like this, it will automatically translate into 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;
}
However, If you wanted to translate if
, you could use something like this, but as I said yo do not need to convert it, or you do not need C
for this job.
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;
In assembly, you can do it very easily. For example, in À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