hello all ,
there was a code which would solve hanoi probelm , one of my friends asked me to split the code so that it uses two code segments !
and this is where i am stuck ! and i ask for your help .
here is the code .
thanks in advance
Code:
INCLUDE IO.H
;**************************CLS MACRO*******************
CLS MACRO
MOV AX, 6H
MOV BH, 7
MOV CX, 0
MOV DX, 184FH
INT 10H
ENDM
;******************GETCH MACRO*******************
GETCH MACRO
MOV AH,07H
INT 21H
ENDM
;********************PRINT***********************
PRINT MACRO
MOV AH,09H
MOV DX,OFFSET MOVEMSG
INT 21H
ENDM
;*********************************Data segment*******************
SSEG SEGMENT STACK
DW 512 DUP(?)
SSEG ENDS
DATASG SEGMENT
STARTMSG DB 'ENTER DISK NUMBER : ',"$"
NUM DB 2 DUP(0)
MOVEMSG DB 0DH,0AH, ' '
SOURCE DB ?
MSG DB ' ==> '
DEST DB ?
ENDMSG DB "$"
DATASG ENDS
;========================================================
Proc_Code Segment
Assume CS:Proc_Code , DS:datasg , ss:SSEG
_Hanoi_ Proc far
Push AX
Push DS
MOV AX , Seg datasg
MOV DS , AX
CALL MOVE
ADD SP, 8
MOVE PROC
PUSH BP
MOV BP,SP
CMP WORD PTR [BP+10],1
JNE MOVE_STACK
MOV BX, [BP+8]
MOV SOURCE, BL
MOV BX, [BP+6]
MOV DEST, BL
PRINT
GETCH
JMP RETURN
MOVE_STACK:
MOV AX, [BP+10]
DEC AX
PUSH AX
PUSH [BP+08]
PUSH [BP+04]
PUSH [BP+06]
CALL MOVE
ADD SP, 8
MOV AX, 1
PUSH AX
PUSH [BP+08]
PUSH [BP+06]
PUSH [BP+04]
CALL MOVE
ADD SP, 8
MOV AX, [BP+10]
DEC AX
PUSH AX
PUSH [BP+04]
PUSH [BP+06]
PUSH [BP+08]
CALL MOVE
ADD SP, 8
RETURN:
POP BP
RET
MOVE ENDP
POP DS
POP AX
RET
_Hanoi_ ENDP
Proc_Code ENDS
;=========================================================
;********** CODE SEGMENT *************
CODESG SEGMENT
ASSUME CS:CODESG ,DS:DATASG
START:
MOV AX, SEG DATASG
MOV DS, AX
CLS
LOOP1:
MOV AH,09H
MOV DX,OFFSET STARTMSG
INT 21H
MOV AH ,01H
INT 21H
MOV NUM,AL
ATOI NUM
MOV NUM,AX
CMP NUM,0
JZ LOOP1
PUSH NUM
MOV AX, ' A'
PUSH AX
MOV AX, ' B'
PUSH AX
MOV AX, ' C'
PUSH AX
CALL _Hanoi_
ADD SP, 8
CODESG ENDS
END START
Ok, this looks much better! Simple question: Does it work?
Static procedure nesting in Assembly looks a bit like the respective structural feature in Pascal but it's by far not that easy to use. (It can actually be of some use in some situations though I can't give an example of this off-hand either. That probably is the reason why it's not plain illegal.) The assembler translates the instructions exactly as you write them. As a consequence, your original proc _hanoi_ would simply run into the code of MOVE and never have a chance to reach its final three instructions that are intended to return control to the code that called it. (It would probably crash for certain because it would try to return from a far proc, _hanoi_, using the near RET in MOVE. But I'm not 100% sure at the moment that the far property doesn't get inherited by nested procs.)
Aside from that, the main code at the end of the program would run into nirvana after return from _hanoi_ - if it ever had a chance to get there at all.
Oh, wait... I just found something that still keeps the program from running correctly at the very end of the code: the ADD SP,8 before the call to EXIT. It tries to remove the parameters passed to _hanoi_ from the stack after the call, but that was in the previous version, so you are popping something off the stack that isn't there. The program may not even actually crash because of that since you're not attempting to do a RET unsing whatever SP is pointing to now, but that's still asking for trouble.
Well, you accomplished the objective of demonstrating the use of more than one code segments, but that's not a very useful example as the "outer" code in the segment CODESG does close to nothing, i.e. just set up DS and then call the rest in the other segment before exiting. Ok, you can think of it as a really, really tiny runtime environment...
Last edited by Eri523; February 18th, 2011 at 07:33 AM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Ok, this looks much better! Simple question: Does it work?
Static procedure nesting in Assembly looks a bit like the respective structural feature in Pascal but it's by far not that easy to use. (It can actually be of some use in some situations though I can't give an example of this off-hand either. That probably is the reason why it's not plain illegal.) The assembler translates the instructions exactly as you write them. As a consequence, your original proc _hanoi_ would simply run into the code of MOVE and never have a chance to reach its final three instructions that are intended to return control to the code that alled it. (It would probably crash for certain because it would try to return from a far proc, _hanoi_, using the near RET in MOVE. But I'm not 100% sure at the moment that the far property doesn't get inherited by nested procs.)
Aside from that, the main code at the end of the program would run into nirvana after return from _hanoi_ - if it ever had a chance to get there at all.
Oh, wait... I just found something that still keeps the program from running correctly at the very end of the code: the ADD SP,8 before the call to EXIT. It tries to remove the parameters passed to _hanoi_ from the stack after the call, but that was in the previous version, so you are popping something off the stack that isn't there. The program may not even actually crash because of that since you're not attempting to do a RET unsing whatever SP is pointing to now, but that's still asking for trouble.
Well, you accomplished the objective of demonstrating the use of more than one code segments, but that's not a very useful example as the "outer" code in the segment CODESG does close to nothing, i.e. just set up DS and then call the rest in the other segment before exiting. Ok, you can think of it as a really, really tiny runtime environment...
silly me
i was extremely tired , and if it weren't for your help on the proc , i wouldn't find out that i'd committed such bizarre error in using procs and stuff , you just saved me for real Eri523 (from banging my head to the wall you know )
i would like to know more on these stuff .( you said sth about the slick way of sth ? ! what could that mean ? )
thanks eri523
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.