Assembly Language, C language, and implementation- --

Source: Internet
Author: User
Tags stack pop

Assembly Language, C language, and implementation- --
Imwtr assembly language, and C language, implementation- -- meaning Description: use an assembly language to implement. You only need to display the order of the disks to be moved without displaying the size of the disks to be moved, for example, X> Z, X> Y, Z> Y, X> Z ,..... (N-order Hanoi tower problem) assume there are three towers named X, Y, and Z respectively, there are n Different Diameter values inserted on tower X, ranging from small to large: 1, 2 ,..., N disc. It is required that n disks on the X axis be moved to the tower Z and stacked in the same order. The following rules must be observed when the disc is moved: 1) only one disc can be moved at a time; 2) the disc can be inserted in any tower of X, Y, and Z. 3) a large disc cannot be pressed on a small disc at any time. The implementation of Jakarta is interpreted in C language as the implementation of recursive function calls. If it is converted to Assembly implementation, you can directly go to the stack to perform corresponding operations (of course, you can also use the assembler language to implement advanced recursive calls ..) c language: copy the code void move (char one, char three) {// one to thre printf ("% c ---> % c", one, three );} void HANOI (int n, char one, char two, char three) {if (n = 1) {// if there is only one disc, directly move the disc from one to three move (one, three);} else {// if it is larger than a disk HANOI (n-1, one, three, two ); // first move n-1 data from one to two, and then move (one, three) The Largest Disc left on one ); // move the Largest Disc from one to three HANOI (n- 1, two, one, three); // move n-1 records from two to three.} // End of void HANOI (5, 'x', 'y', 'z '); // you can move the fifth-order tower from disk X to disk Z to copy the code and perform recursive operations. This way, the stack operation becomes clearer, you can use the assembly language to implement it. (You can use the bp Stack pointer operation to push the stack pop to implement the corresponding recursive call ). The compilation code is implemented as follows: Copy code 1 data segment 2 n db? 3 msg db 0dh, 0ah, 'Enter the number you want: $'4 msg1 db 0dh, 0ah, 'hanoi-MOVE Procedure with: $ ', 0ah, 0dh 5 to db' ---> $'6 count dw 0; 7 data ends 8 9 code segment 10 assume cs: CODE, DS: DATA 11 START: 12 mov ax, DATA 13 mov ds, AX 14 15 lea dx, msg 16 CALL intro 17 KEYIN: 18 mov ah, 01 H; character Input and echo 19 INT 21 H 20 MOV byte ptr [n], Al; receive the input n value 21 22 lea dx, msg1 23 CALL intro 24 mov dl, 0ah 25 CALL DISPLAY 26 27 mov al, byte ptr [n] 28 sub al, 30 H 29 CBW 30 mov dx, AX 31 mov ax, 'x' 32 mov bx, 'y' 33 mov cx, 'z' 34; mov dx, [n] 35 36 push dx 37 push cx 38 push bx 39 push ax 40 41 CALL HANOI_MOVE 42 add sp, 8 43 44 mov dl, 0ah 45 call display 46 lea dx, msg 47 CALL intro 48 jmp keyin 49 50 GroupMake: 51 INC [count] 52 mov ax, [count] 53 mov bl, 5; 5 groups 54 div bl 55 cmp ah, 0 56 jne space 57 mov dl, 0ah 58 Call display; output line feed 59 jmp close 60 SPACE: 61 mov dx, 20 H 62 call display; Output SPACE 63 CLOSE: 64 RET 65 66 STEP:; Output STEP number 67 mov ax, [count]; prevent incorrect output of count multiple digits -- hexTOdec 68 mov cx, 0 69 mov bx, 10 70 DISP1: 71 mov dx, 0 72 div bx 73 push dx 74 inc cx 75 or ax, AX 76 JNZ DISP1 77 78 mov dl, 5BH; Output DISPLAY [79 call display 80 DISP2: 81 pop dx 82 add dl, 30 H; Output DISPLAY corresponding to 83 call display 84 LOOP DISP2 85 86 mov dl, 5DH; Output DISPLAY] 87 call display 88 RET 89 90 intro proc near; prompt: 91 mov ah, 09 H 92 INT 21 H 93 ret 94 intro endp 95 96 DISPLAY proc near; output characters 97 mov ah, 02 H 98 INT 21 H 99 RET100 DISPLAY endp101 102 HANOI_MOVE proc near103 mov bp, SP104 mov ax, [BP + 8] 105 cmp ax, 1106 JG moreThanOne; if n is not equal to 1, jump to 107 CALL STEP108 mov dx, [BP + 2]; take the first value 109 CALL DISPLAY110 lea dx, to 111 CALL intro112 mov dx, [BP + 6]; The third value is 113 call display 11. 4 CALL GroupMake115 116 RET117 118 119 moreThanOne: 120 mov ax, [BP + 2] 121 mov bx, [BP + 4] 122 mov cx, [BP + 6] 123 mov dx, [BP + 8] 124 dec dx; n-1125 PUSH DX126 PUSH BX127 PUSH CX128 PUSH AX129 CALL HANOI_MOVE; recursion once, 130 add sp, 8131 mov bp, SP133 CALL STEP134 mov dx, [BP + 2]; take the first value 135 CALL DISPLAY136 lea dx, to 137 CALL intro138 mov dx, [BP + 6]; take the third value 139 call display 140 CALL GroupMake141 142 mov ax, [BP + 2] 143 mov bx, [BP + 4] 144 mov cx, [BP + 6] 145 mov dx, [BP + 8] 146 dec dx; n-1147 PUSH DX148 PUSH CX149 PUSH AX150 PUSH BX151 CALL HANOI_MOVE; recursion once, for the next loop 152 add sp, 8153 154 RET155 HANOI_MOVE endp156 157 EXIT: mov ah, 4CH; exit the system 158 INT 21 H 159 CODE ENDS160 end start and copy the CODE patiently. This is not very difficult. The first parameter of the stack starts from bp + 2. Do not make a mistake.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.