
244 CHAPTER 5 Program Design and Analysis
1234
y
z
x
w
d
e
c
b
a
Compare the ARM assembly code for the two code fragments. We have written both
assuming that we have only four free registers. In the before version, we do not have to write
out any values, but we must read a and b twice. The after version allows us to retain all values
in registers as long as we need them.
Before version After version
LDR r0,a LDR r0,a
LDR r1,b LDR r1,b
ADD r2,r0,r1 ADD r2,r1,r0
STRr2,w;w=a+b STRr2,w;w=a+b
LDRr r0,c SUB r2,r0,r1
LDR r1,d STR r2,z;z=a–b
ADD r2,r0,r1 LDR r0,c
STRr2,x;x=c+d LDRr1,d
LDR r1,e ADD r2,r1,r0
ADD r0,r1,r2 STR r2,x;x=c+d
STRr0,y;y=x+e LDRr1,e
LDR r0,a ; reload