在Assembly中的另一个过程内调用过程

时间:2014-12-25 17:31:30

标签: assembly x86 procedure

我正在使用tasm,我正试图在另一个程序中调用一个程序。但是,当程序从第二个程序返回时,不是返回到它被调用的点,而是将IP设置为0000.有人可以解释一下为什么会发生这种情况吗? 以下是代码的一部分:

主要程序:

MAIN    PROC    FAR ; main proc MUST be FAR

ASSUME  CS:COD1,DS:DATA1,SS:STCK
PUSH    DS      ;save DS on stack for OS return
XOR     AX,AX   ;
PUSH    AX      ;put 0 on stack for OS return
MOV     AX,DATA1
MOV     DS,AX   ;load data Segement register
CALL    PLAY
RET             ; return to OS

MAIN    ENDP

首先称为程序:

PLAY PROC NEAR
RPT:

MOV     AH,00H ;read ch from stdin
INT     16H
CMP     AH,48H
JE      UP
CMP     AH,4BH
JE      LEFT
CMP     AH,50H
JE      DOWN
CMP     AH,4DH
JE      RIGHT
CMP     AH,01H
JE      ESCAPE

UP:
CALL    MOVEUP
JMP     RPT

DOWN:
CALL    MOVEDOWN
JMP     RPT

LEFT:
CALL MOVELEFT
JMP RPT

RIGHT:
CALL MOVERIGHT
JMP RPT

ESCAPE:
RET

PLAY ENDP

第二个称为程序的模型:

MOVEUP PROC NEAR    ;tries to update position of player if down key pressed
PUSH    DX
PUSH    AX
PUSH    SI  ;save all general registers to stack

MOV     AL,X    ;compute index for current position
MOV     DL,15   
MUL     DL
MOV     DL,Y    
MOV     DH,0
ADD     AX,DX
MOV     SI,AX
PUSH    SI  ;save it for later


MOV     DL,  X  ;current x coord
MOV     DH,  Y  ;current y coord
SUB     DL,  1
MOV     NEWX,DL ;new x coord
MOV     NEWY,DH ;new y coord
MOV     AL,NEWX ;store new x coord in AL
MOV     AH,0    ;make ah 0 to "extend" new x on 2B
CMP     AL,0    ;check if new x in bounds (>=0)
JL      NOT_OK_UP

MOV     DL,15   ;compute index of position: index=15*x+y
MUL     DL
MOV     DL,NEWY 
MOV     DH,0
ADD     AX,DX
MOV     SI,AX
MOV     DL,MAZE[SI] ;fetch element on position newx,newy
CMP     DL,1        ;if wall, go to end of procedure(nop)

JE      NOT_OK_UP

MOV     DL,NEWX
MOV     DH,NEWY
MOV     X,DL    ;update x
MOV     Y,DH    ;update y
MOV     MAZE[SI],2  ;update position in maze
POP     SI
MOV     MAZE[SI],0  ;clean up old position
NOT_OK_UP:
POP     SI
POP     AX
POP     DX
RET
MOVEUP ENDP

0 个答案:

没有答案