Hardware Reference
In-Depth Information
.686
; compile for Core i7 class processor
.MODEL FLAT
PUBLIC towers
; export 'towers'
EXTERN printf:NEAR
; import printf
.CODE
towers: PUSH EBP
; save EBP (frame pointer) and decrement ESP
MOV EBP, ESP
; set new frame pointer above ESP
CMP [EBP+8], 1
; if (n == 1)
JNE L1
; branch if n is not 1
MOV EAX, [EBP+16]
; printf(" ...", i, j);
PUSH EAX
; note that parameters i, j and the format
MOV EAX, [EBP+12]
; string are pushed onto the stack
PUSH EAX
; in reverse order. This is the C calling convention
PUSH OFFSET FLAT:format
; offset flat means the address of format
CALL printf
; call printf
ADD ESP, 12
; remove params from the stack
JMP Done
; we are finished
L1:
MOV EAX, 6
; startk=6 i j
SUB EAX, [EBP+12]
; EAX = 6
i
SUB EAX, [EBP+16]
; EAX = 6 i j
MOV [EBP+20], EAX
;k=EAX
PUSH EAX
; start towers(n
1, i, k)
MOV EAX, [EBP+12]
; EAX = i
PUSH EAX
; push i
MOV EAX, [EBP+8]
; EAX = n
DEC EAX
; EAX = n 1
PUSH EAX
; push n
1
CALL towers
; call towers(n 1, i, 6 i j)
ADD ESP, 12
; remove params from the stack
MOV EAX, [EBP+16]
; start towers(1, i, j)
PUSH EAX
; push j
MOV EAX, [EBP+12]
; EAX = i
PUSH EAX
; push i
PUSH 1
; push 1
CALL towers
; call towers(1, i, j)
ADD ESP, 12
; remove params from the stack
MOV EAX, [EBP+12]
; start towers(n
1, 6
i
j, i)
PUSH EAX
; push i
MOV EAX, [EBP+20]
; EAX = k
PUSH EAX
; push k
MOV EAX, [EBP+8]
; EAX = n
DEC EAX
; EAX = n
1
PUSH EAX
; push n 1
CALL towers
; call towers(n
1, 6
i
j, i)
ADD ESP, 12
; adjust stack pointer
Done:
LEAVE
; prepare to exit
RET 0
; return to the called
. DATA
format
DB "Move disk from %d to %d\n"
; format string
END
Figure 5-44. The Towers of Hanoi for the Core i7.
 
 
Search WWH ::




Custom Search