我用了DOS下面Franklin C51 V3.2(就是Keil在美国销售代理的名字)测试了一下,果然如果我所料,你的代码执行效率非常差。生成出来的汇编代码对比如下:
- ; void LED_dula(unsigned char dat)
- RSEG ?PR?_LED_dula?1801
- USING 0
- _LED_dula:
- MOV dat?00,R7
- ; SOURCE LINE # 32
- ; {
- ; SOURCE LINE # 33
- ; sega=dat &0x01;
- ; SOURCE LINE # 34
- MOV A,dat?00
- RRC A
- MOV sega,C
- ; segb=dat>>1&0x01;
- ; SOURCE LINE # 35
- MOV A,dat?00
- CLR C
- RRC A
- RRC A
- MOV segb,C
- ; segc=dat>>2&0x01;
- ; SOURCE LINE # 36
- MOV A,dat?00
- RRC A
- RRC A
- ANL A,#03FH
- RRC A
- MOV segc,C
- ; segd=dat>>3&0x01;
- ; SOURCE LINE # 37
- MOV A,dat?00
- RRC A
- RRC A
- RRC A
- ANL A,#01FH
- RRC A
- MOV segd,C
- ; sege=dat>>4&0x01;
- ; SOURCE LINE # 38
- MOV A,dat?00
- SWAP A
- ANL A,#0FH
- RRC A
- MOV sege,C
- ; segf=dat>>5&0x01;
- ; SOURCE LINE # 39
- MOV A,dat?00
- SWAP A
- RRC A
- ANL A,#07H
- RRC A
- MOV segf,C
- ; segg=dat>>6&0x01;
- ; SOURCE LINE # 40
- MOV A,dat?00
- SWAP A
- RRC A
- RRC A
- ANL A,#03H
- RRC A
- MOV segg,C
- ; segdp=dat>>7&0x01;
- ; SOURCE LINE # 41
- MOV A,dat?00
- SWAP A
- RRC A
- RRC A
- RRC A
- ANL A,#01H
- RRC A
- MOV segdp,C
- ; }
- ; SOURCE LINE # 42
- RET
- ; END OF _LED_dula
- ; void LED_dula1(unsigned char dat)
- RSEG ?PR?_LED_dula1?1801
- USING 0
- _LED_dula1:
- MOV dat?10,R7
- ; SOURCE LINE # 43
- ; {
- ; SOURCE LINE # 44
- ; sega=dat &0x01;
- ; SOURCE LINE # 45
- MOV A,dat?10
- RRC A
- MOV sega,C
- ; segb=dat&0x01<<1;
- ; SOURCE LINE # 46
- MOV A,dat?10
- MOV C,ACC.1
- MOV segb,C
- ; segc=dat&0x01<<2;
- ; SOURCE LINE # 47
- MOV C,ACC.2
- MOV segc,C
- ; segd=dat&0x01<<3;
- ; SOURCE LINE # 48
- MOV C,ACC.3
- MOV segd,C
- ; sege=dat&0x01<<4;
- ; SOURCE LINE # 49
- MOV C,ACC.4
- MOV sege,C
- ; segf=dat&0x01<<5;
- ; SOURCE LINE # 50
- MOV C,ACC.5
- MOV segf,C
- ; segg=dat&0x01<<6;
- ; SOURCE LINE # 51
- MOV C,ACC.6
- MOV segg,C
- ; segdp=dat&0x01<<7;
- ; SOURCE LINE # 52
- RLC A
- MOV segdp,C
- ; }
- ; SOURCE LINE # 53
- RET
- ; END OF _LED_dula1
复制代码
还有更好的写法:
- unsigned char bdata tmp;
- sbit tmp0=tmp^0;
- sbit tmp1=tmp^1;
- sbit tmp2=tmp^2;
- sbit tmp3=tmp^3;
- sbit tmp4=tmp^4;
- sbit tmp5=tmp^5;
- sbit tmp6=tmp^6;
- sbit tmp7=tmp^7;
- void LED_dula2(unsigned char dat)
- {
- tmp=dat;
- sega=tmp0;
- segb=tmp1;
- segc=tmp2;
- segd=tmp3;
- sege=tmp4;
- segf=tmp5;
- segg=tmp6;
- segdp=tmp7;
- }
复制代码
最终生成的汇编就更加简洁了:
- ; void LED_dula2(unsigned char dat)
- RSEG ?PR?_LED_dula2?1801
- USING 0
- _LED_dula2:
- MOV dat?20,R7
- ; SOURCE LINE # 54
- ; {
- ; SOURCE LINE # 55
- ; tmp=dat;
- ; SOURCE LINE # 56
- MOV tmp,dat?20
- ; sega=tmp0;
- ; SOURCE LINE # 57
- MOV C,tmp0
- MOV sega,C
- ; segb=tmp1;
- ; SOURCE LINE # 58
- MOV C,tmp1
- MOV segb,C
- ; segc=tmp2;
- ; SOURCE LINE # 59
- MOV C,tmp2
- MOV segc,C
- ; segd=tmp3;
- ; SOURCE LINE # 60
- MOV C,tmp3
- MOV segd,C
- ; sege=tmp4;
- ; SOURCE LINE # 61
- MOV C,tmp4
- MOV sege,C
- ; segf=tmp5;
- ; SOURCE LINE # 62
- MOV C,tmp5
- MOV segf,C
- ; segg=tmp6;
- ; SOURCE LINE # 63
- MOV C,tmp6
- MOV segg,C
- ; segdp=tmp7;
- ; SOURCE LINE # 64
- MOV C,tmp7
- MOV segdp,C
- ; }
- ; SOURCE LINE # 65
- RET
- ; END OF _LED_dula2
复制代码 |