嘿,这里只是我用c写的一个简单的程序,在avr-gcc下编译。相应的汇编代码也贴出来了。
仍然无法理解 switch 语句的汇编代码在做什么,任何帮助都会很棒。谢谢。
int main()
{
char myinput;
printf("Which option will you choose:\n");
printf("a) Program 1 \n");
printf("b) Program 2 \n");
scanf("%c", &myinput);
switch (myinput)
{
case 'a':
printf("Run program 1\n");
break;
case 'b':
{
printf("Run program 2\n");
printf("Please Wait\n");
break;
}
default:
printf("Invalid choice\n");
break;
}
return 0;
返回0;
}
汇编代码:
switch (myinput)
+00000147: 900F POP R0 Pop register from stack
+00000148: 900F POP R0 Pop register from stack
+00000149: 900F POP R0 Pop register from stack
+0000014A: 900F POP R0 Pop register from stack
+0000014B: 8189 LDD R24,Y+1 Load indirect with displacement
+0000014C: 3681 CPI R24,0x61 Compare with immediate
+0000014D: F019 BREQ PC+0x04 Branch if equal
+0000014E: 3682 CPI R24,0x62 Compare with immediate
+0000014F: F459 BRNE PC+0x0C Branch if not equal
+00000150: C003 RJMP PC+0x0004 Relative jump
22: printf("Run program 1\n");
+00000151: E38D LDI R24,0x3D Load immediate
+00000152: E092 LDI R25,0x02 Load immediate
+00000153: C009 RJMP PC+0x000A Relative jump
26: printf("Run program 2\n");
+00000154: E48B LDI R24,0x4B Load immediate
+00000155: E092 LDI R25,0x02 Load immediate
+00000156: 940E02A9 CALL 0x000002A9 Call subroutine
27: printf("Please Wait\n");
+00000158: E589 LDI R24,0x59 Load immediate
+00000159: E092 LDI R25,0x02 Load immediate
+0000015A: C002 RJMP PC+0x0003 Relative jump
31: printf("Invalid choice\n");
+0000015B: E685 LDI R24,0x65 Load immediate
+0000015C: E092 LDI R25,0x02 Load immediate
+0000015D: 940E02A9 CALL 0x000002A9 Call subroutine
38: }
+0000015F: E080 LDI R24,0x00 Load immediate
+00000160: E090 LDI R25,0x00 Load immediate
+00000161: 900F POP R0 Pop register from stack
+00000162: 91CF POP R28 Pop register from stack
+00000163: 91DF POP R29 Pop register from stack
+00000164: 9508 RET Subroutine return
多谢你们。