回 帖 发 新 帖 刷新版面

主题:[原创]帮忙看看这个递归的汉诺塔错在哪里了,

data segment
  from db 41h
  autx db 42h
  to   db 43h
  n    db  0
  laber db '--->$'
  ctrf db  0ah,0dh,'$'                
data ends
code segment
 main proc far
 assume cs:code ,ds:data           

start:
  mov ax,data
  mov ds,ax

  mov ah,01h
  int 21h                ;输入数字
  sub al,30h
  mov n,al           ;
  lea  dx,ctrf            ;回车换行
  mov ah, 9h
  int 21h

  call sort
  mov ah,4ch
  int 21h
  main endp
                      ;-------------------递归函数
sort proc near
  push ax
  push bx
  push cx
  push dx

  mov al,from
  mov bl,autx
  mov cl,to
  mov dl,n
           
  cmp dl,1h
  je print1

  dec n
  mov ch,to
  xchg autx,ch
  mov to,ch
  call sort

print1:
  add dl,30h
  mov ah,2h
  int 21h
  mov dl,20h
  mov ah,2h
  int 21h
  mov dl,al
  mov ah,2h
  int 21h
  mov dx,offset laber
  mov ah,9h
  int 21h
  mov dl,cl
  mov ah,2h
  int 21h

 mov dl,20h
 mov ah,2h
 int 21h

  cmp n,1h
  je exit

  mov ch,to
  xchg from, ch
  mov to,ch
  call sort
exit:  pop dx
  pop cx
  pop bx
  pop ax
  ret
  sort endp

  code ends
  end start


请大家帮帮忙吧,我是新手,自己写的,帮忙改改吧,谢谢!

回复列表 (共4个回复)

沙发

大家不要光看啊,帮忙解决一下啊,谢谢了,

板凳

唉!还是靠我自己吧,

3 楼

我已经解决了啊!!!哈哈哈

贴出来供大家分享

4 楼

[b]本程序功能[/b]:可以输入小于等于16的数(盘子个数),解决汉诺塔问题,然后输出移动过程,并输出移动次数。
[size=3][b]请大家尊重产权,尊重原创。谢谢!!![/b][/size]
data segment                       ;数据段
     space db 13,10,'$'
     space db 13,10,'$'
     arr   db '-->$'
     disk db 'move disk $'
     infor db 'input a number:$'
     num dw 0
     Time dw '  time$'
data ends
code segment                      ;代码段
   main proc far                     ;主程序
   assume cs:code,ds:data
start:
    mov ax,data
    mov ds,ax
     lea dx,infor                    ;输出提示信息
     mov ah,9h
     int 21h
call input                      ;调用input程序输入盘子个数,放ch中
     lea dx,space
     mov ah,9h
     int 21h
     mov    dh,'A'                   ;设置柱子编号
     mov    bl,'B'
     mov    bh,'C'
     call sort                     ;调用sort子程序,解决汉诺塔问题
  call printnum
     lea dx,time 
     mov ah,9h
     int 21h
     mov ax,4c00h
     int 21h
main endp                      ;主程序结束
;****************input子程序**********************
input proc near        
      mov bx,0
new: mov  ah,1h
     int 21h
     sub al,30h
     jl exit1
     cmp al,9d
     jg exit1
     cbw
     xchg ax,bx
     mov cx,10d                  ;bx=bx*10+ax
     mul cx
     xchg ax,bx
     add bx,ax
     jmp new                               ;输入下一个
exit1:
     mov cx,bx
     ret
input endp
;***************sort子程序**********************
sort proc near                     ;sort子程序,   
     dec cx                     ;步骤1)
     cmp cx,0
     je print0
     push cx                     ;保存现场,以备递归所用
     push bx                     ;步骤2)  
    ; push ax
     xchg bl,bh
     call sort
     pop dx
   ;  pop ax
     pop bx
     pop cx
print0:
     inc num
     push dx
     lea dx,disk                ; 步骤3)
     mov ah,9h      ;本步的输出格式为:Move disk 盘号 柱号---->柱号
     int 21h
     pop dx

  ;   push ax
     push cx
     mov ax,cx

     mov cl,10
     div cl

     mov dl,al
     add dl,30h
  push ax
     mov ah,2h
    int 21h
  pop ax
     mov dl,ah
    add dl,30h
    mov ah,2h
     int 21h
     pop cx
;pop ax
     mov dl,20h
     int 21h
     mov dl,dh
     mov ah,2h
     int 21h
     push dx
     lea dx,arr
     mov ah,9h
     int 21h
     pop dx
     mov dl,bh
     mov ah,2h
     int 21h
     push dx
     lea dx,space
     mov ah,9h
     int 21h
     pop dx

     cmp cx,0
     je exit
     push cx                             ;步骤4)
     push bx
     push dx
     xchg dh,bl
     call sort
     pop dx
     pop bx
     pop cx
exit:     ret
sort endp                         ;sort子程序结束
;****************printnum子程序**********************
printnum proc near                 ;输出移动的次数
  mov cx,10000d
  call deca
  mov cx,1000d
  call deca
  mov cx,100d
  call deca
   mov cx,10d
  call deca
  mov cx,1d
  call deca
  ret
 printnum endp
;****************deca子程序**********************
deca proc near
  mov ax,num
  mov dx,0
 div cx
 mov num,dx                   ;将余数放回num
 mov dl,al                     ;商输出
 add dl,30h
 mov ah,2h
 int 21h
 ret
deca endp
code ends
end start                           ;程序结束

我来回复

您尚未登录,请登录后再回复。点此登录或注册