2ndwolf
Posts: 3
Joined: Fri Jan 04, 2013 5:18 am

Baking Pi OK4

Fri Jan 04, 2013 5:27 am

Considering the following code:

Code: Select all

 .globl GetSystemTimerBase
GetSystemTimerBase:
ldr r0,=0x20003000
mov pc,lr

.globl GetTimeStamp
GetTimeStamp:
push {lr}
bl GetSystemTimerBase
ldrd r0,r1,[r0,#4]
pop {pc}

delay .req r2
mov delay,r0
push {lr}
bl GetTimeStamp
start .req r3
mov start,r0

loop$:

bl GetTimeStamp
elapsed .req r1
sub elapsed,r0,start
cmp elapsed,delay
.unreq elapsed
bls loop$

.unreq delay
.unreq start
pop {pc}
Why do we push{lr} just before bl GetTimeStamp?
Wouldn't pop{pc} at the end of the code just take that lr we pushed and give an endless loop?

User avatar
Chadderz
Posts: 64
Joined: Thu Aug 30, 2012 12:50 pm
Location: Cambridge, UK

Re: Baking Pi OK4

Fri Jan 04, 2013 7:29 am

Yes, it would take the lr we pushed, but the lr contains the address after the bl that takes us to GetTimeStamp. The bl sets the pc to the address we wish to branch to, but also has the side effect of setting lr to the address after the bl command. This means at the time of the push the lr contains the address after the bl, and so when we do the pop we are effectively branching to that address. This achieves the effect of a function call and return. We have to do this in order to preserve the lr, which would otherwise be overwritten in the bl GetSystemTimerBase.

2ndwolf
Posts: 3
Joined: Fri Jan 04, 2013 5:18 am

Re: Baking Pi OK4

Fri Jan 04, 2013 10:43 pm

So we do want the program to branch back to bl GetTimeStamp once loop$ has finished?

2ndwolf
Posts: 3
Joined: Fri Jan 04, 2013 5:18 am

Re: Baking Pi OK4

Wed Jan 09, 2013 9:53 pm

*bump*

User avatar
DavidS
Posts: 3800
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Baking Pi OK4

Wed Jan 09, 2013 11:50 pm

2ndwolf wrote:So we do want the program to branch back to bl GetTimeStamp once loop$ has finished?
No we branch back to the instruction fallowing bl GetTimeStamp
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

Return to “Bare metal, Assembly language”