山东大学汇编语言上机实验 - 实验五 - 图文 下载本文

输入:键盘接受盘子数N

输出:把转换为二进制的N值存入BX寄存器当中。

功能:把从键盘接受的十进制装换为二进制数并存入BX寄存器当中。

3:模块名:HANOI

输入:从BX寄存器中取得盘子数N 输出:显示盘子的移动办法

功能:用递归算法计算并显示HANOI的结果

4模块名:CTRF

功能:显示回车,换行

5:模块名:save

功能:保存NX,Y,Z,入站

6:模块名:restore 功能:恢复N,X,Y,Z 7:模块名:PRINT

功能:显示XNZ

调用子程序BINIDEC把N值从二进制转换为十进制并在屏幕上显示出来 调用子程序CRLF回车换行

8:模块名:BINIDEC

输入:从BX寄存器中取得N值

输出:把N值以是十进制形式在屏幕上显示出来

功能:把BX中的二进制N值转换为十进制形式,并在屏幕上显示出来。

源程序如下:;********************************************** datarea segment

message1 db 'N=?',0ah,0dh,'$'

message2 db 'What is the name of spindle X?' db 0ah,0dh,'$'

message3 db 'What is the name of spindle Y?' db 0ah,0dh,'$'

message4 db 'What is the name if spindle Z?' db 0ah,0dh,'$' flag dw 0

constant dw 10000,1000,100,10,1

datarea ends

;********************************************** prognam segment

;---------------------------------------------- main proc far

assume cs:prognam,ds:datarea start:

;set up stack for return push ds sub ax,ax push ax

;set DS register to current data segment mov ax,datarea mov ds,ax

;main part of program goes here lea dx,message1 mov ah,09h int 21h

call decibin call crlf ;

cmp bx,0 jz exit ;

lea dx,message2 mov ah,09h int 21h mov ah,01h int 21h mov ah,0 mov cx,ax call crlf ;

lea dx,message3 mov ah,09h int 21h mov ah,01h int 21h mov ah,0 mov si,ax call crlf ;

lea dx,message4 mov ah,09h

int 21h

mov ah,01h int 21h mov ah,0 mov di,ax call crlf ;

call hanoi ;

exit: ret ;return to DOS ;

main endp

;---------------------------------------- hanoi proc near ;define subprocedure ;Solves tower of HANOI puzzle

;Argement:(BX)=N,(CX)=X,(SI)=Y,(DI)=Z cmp bx,1 je basis call save dec bx

xchg si,di call hanoi call restor call print dec bx

xchg cx,si call hanoi jmp return

basis: call print return: ret hanoi endp

;--------------------------------- print proc near ;print xnz mov dx,cx mov ah,02h int 21h

call binidec mov dx,di mov ah,02h int 21h

call crlf ret

print endp

;----------------------------------------- save proc near

;push N,X,Y,Zonto stack pop bp push bx push cx push si push di push bp ret

save endp

;-------------------------------------------- restor proc near pop bp pop di pop si pop cx pop bx push bp ret

restor endp

;----------------------------------------------- decibin proc near mov bx,0 newchar: mov ah,1 int 21h sub al,30h jl exit1 cmp al,9d jg exit1 cbw ; ;

xchg ax,bx mov cx,10d mul cx

xchg ax,bx ;

add bx,ax jmp newchar exit1: ret

decibin endp

;----------------------------------------------