rendered paste body; Tower of Hanoi
; Recursive implementation in C assembly language.
; Compile with :
; $ nasm -f elf hanoi.asm
; $ ld -s -o hanoi hanoi.o
BITS 32
GLOBAL main
EXTERN memset, printf
section .bss
height: resd 1 ; Size of towers
first: resd 1 ; First stack
second: resd 1 ; Second one
third: resd 1 ; Last one
section .data
format: db '%d', 10, 0
argcc: db 'argc=%d',10,0
strarg: db 'str=%s',10,0
section .text
init_stacks:
push 12
push 0x0
push first
call memset
add esp, 12
;mov [second], esp
;sub esp, 0x0a
;mov [third], esp
;sub esp, 0x0a
mov ecx, [height]
inc ecx
push ecx
push argcc
call printf
add esp, 8
ret
xor ecx, ecx
mov ecx, 88
.loop:
mov ebx, first
;add ebx, ecx
mov [ebx], ecx
;add ebx, '0'
;mov [first + ecx], ebx
;jmp looo
inc ecx
;cmp 99, ecx
jb .loop
looo:
push first
push strarg
call printf
add esp, 8
ret
main:
;mov ebx, height
mov ebx, [esp + 4] ; argc
mov [height], ebx
mov ebx, first
mov [ebx], esp
sub esp, 12
call init_stacks
add esp, 12
;pop eax
;mov eax, 11
push eax
push argcc
call printf
add esp, 8
ret
mov eax, esi ; argv[0]
sub eax, 0x04
push eax
push strarg
call printf
add esp, 8
push esi ; callee-save registers
push edi
ret
mov esi, 1 ; current value
mov edi, 31 ; counter
L1:
push esi ; push value to print
push format ; push address of format string
call printf
add esp, 8 ; pop off parameters passed to printf
add esi, esi ; double value
dec edi ; keep counting
jne L1
pop edi ; restore callee-save registers
pop esi
ret