Click to See Complete Forum and Search --> : what am i missing ?


Master.
February 16th, 2011, 10:36 PM
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
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

Master.
February 16th, 2011, 11:53 PM
i think i found the probs (tanx eri) :)
please sb confirm that .


INCLUDE IO.H
;**************************EXIT MACRO*******************
EXIT MACRO
MOV AH , 07H
INT 21H
MOV AX, 4C00H
INT 21H
ENDM
;**************************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 : ',"$"
STARTMSG2 DB 'Ok ',"$"
endMSG2 DB 'end ',"$"
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


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

MOV AH,09H
MOV DX,OFFSET STARTMSG2
INT 21H

CALL MOVE

ADD SP, 8


MOV AH,09H
MOV DX,OFFSET STARTMSG2
INT 21H


POP DS
POP AX
RET

_Hanoi_ ENDP

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


CALL _Hanoi_

ADD SP, 8
EXIT


CODESG ENDS
END START

Eri523
February 17th, 2011, 07:52 AM
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... :D

Master.
February 18th, 2011, 02:49 AM
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... :D
silly me :D
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 ):D
i would like to know more on these stuff .( you said sth about the slick way of sth ? ! what could that mean ? )
thanks eri523 :wave:

Eri523
February 18th, 2011, 06:30 AM
i would like to know more on these stuff .( you said sth about the slick way of sth ? ! what could that mean ? )

As I said, I can imagine fancy things being done that way but I don't have an example ready. If one comes to mind I'll come back and post it. :)

thanks eri523 :wave:

You're welcome. :)

Master.
February 21st, 2011, 10:17 PM
thanks again :)