CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Location
    somwhere nearby
    Posts
    150

    what am i missing ?

    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
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2007
    Location
    somwhere nearby
    Posts
    150

    Re: what am i missing ?

    i think i found the probs (tanx eri)
    please sb confirm that .

    Code:
    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

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: what am i missing ?

    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.

  4. #4
    Join Date
    Jul 2007
    Location
    somwhere nearby
    Posts
    150

    Re: what am i missing ?

    Quote Originally Posted by Eri523 View Post
    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

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: what am i missing ?

    Quote Originally Posted by Master. View Post
    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
    You're welcome.
    Last edited by Eri523; February 22nd, 2011 at 10:19 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.

  6. #6
    Join Date
    Jul 2007
    Location
    somwhere nearby
    Posts
    150

    Re: what am i missing ?

    thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured