Andes Workshop

It is currently Wed Nov 21, 2018 9:24 pm

All times are UTC + 8 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: register變數
PostPosted: Thu Sep 06, 2012 11:26 am 
Offline
User avatar

Joined: Fri Mar 04, 2011 9:36 pm
Posts: 500
常常使用的local變數,加上register關鍵字,
可以加快執行效率。

例如下面的code
Code:
   register int i;
   int j;
for(i=0;i<0x100000;i++)
   j++;


上面將i設定成放在暫存器裡..
編出來的for loop如下:
Code:
00500152:   addi $r6,$r6,#1
00500156:   sethi $r0,#255
0050015a:   ori $r0,$r0,#0xfff
0050015e:   slts $r15,$r0,$r6
00500162:   beqz $r15,0x500146 <foo+18>

其中r6是變數i。

如果不設register的話..
Code:
code如下:
   int i;
   int j;
for(i=0;i<0x100000;i++)
   j++;


組合語言如下:
Code:
00500156:   lwi $r0,[$fp+#-12]
0050015a:   addi $r0,$r0,#1
0050015e:   swi $r0,[$fp+#-12]
00500162:   sethi $r0,#255
00500166:   ori $r0,$r0,#0xfff
0050016a:   lwi $r1,[$fp+#-12]
0050016e:   slts $r15,$r0,$r1
00500172:   beqz $r15,0x50014a <foo+22>


和上面比,多了3行。
執行多次之後,需要執行的code數量差距相當大。


Top
 Profile Send private message E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 30 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group