Hardware Reference
In-Depth Information
printf(
′′
Move a disk from %d to %d\n
′′
,i,j)
For our purposes, the syntax of printf format strings is unimportant (basically, the
string is printed literally except that %d means print the next integer in decimal).
The only thing that is relevant here is that the procedure is called with three param-
eters: a format string and two integers.
The reason for using the C version for the Core i7 and OMAP4430 ARM CPU
is that the Java I/O library is not available in native form for these machines; the C
library is. The difference is minimal, affecting only the print statement.
5.7.1 The Towers of Hanoi in Core i7 Assembly Language
Figure 5-44 gives a possible translation of the C version of the Towers of
Hanoi for the Core i7. For the most part, it is fairly straightforward. The EBP reg-
ister is used as the frame pointer. The first two words are used for linkage, so the
first actual parameter, n (or N here, as MASM is case insensitive), is at EBP +8,
followed by i and j at EBP +12 and EBP + 16, respectively. The local variable, k ,is
in EBP + 20.
The procedure begins by establishing the new frame at the end of the old one.
It does this by copying ESP to the frame pointer, EBP . Then it compares n to 1,
branching off to the else clause if n > 1. The then code pushes three values on the
stack: the address of the format string, i , and j , and calls itself.
The parameters are pushed in reverse order, which is required for C programs.
This is necessary to put the pointer to the format string on top of the stack. Since
printf has a variable number of parameters, if the parameters were pushed in for-
ward order, printf would not know how deep in the stack the format string was.
After the call, 12 is added to ESP to remove the parameters from the stack. Of
course, they are not really erased from memory, but the adjustment of ESP makes
them inaccessible via the normal stack operations.
The else clause, which starts at L1 , is straightforward.
It first computes
6
j and stores this value in k . No matter what values i and j have, the third
peg is always 6
i
i
j . Saving it in k saves the trouble of recomputing it the sec-
ond time.
Next, the procedure calls itself three times, with different parameters each
time. After each call, the stack is cleaned up. That is all there is to it.
Recursive procedures sometimes confuse people at first, but when viewed at
this level, they are straightforward. All that happens is that the parameters are
pushed onto the stack and the procedure itself is called.
5.7.2 The Towers of Hanoi in OMAP4430 ARM Assembly Language
Now let us try again, only this time for the OMAP4430 ARM. The code is
listed in Fig. 5-45. Because the OMAP4430 ARM code is especially unreadable,
even for assembly code and even after a lot of practice, we have taken the liberty to
 
 
 
Search WWH ::




Custom Search