大家好:
我正在在练习解决除法溢出的习题。我的解体思路是想通过栈来传值。所以我想把除数、被除数先压入栈,然后CALL divDW, 在进入divDV程序段后,在将栈内的数值POP到寄存器内。
可是在DEBUG内运行我的程序, 我观察在经过CALL divDW是,SP的数值会自动的减去2,不明白什么原因。 而且栈里的数值本来是全部初始为同一个数,在运行几条语句后,没有用到栈的空间就会混入其他的数值,也是不知是什么原因。
后面附有图片和程序代码,
请高手指点。
说明问题“SP的数值会自动的减去2”的图片
<img src="https://docs.google.com/leaf?id=0B8PvEMuJQOSyNGYwZGRiZjUtZTc1Ni00YWEwLTkzYTktZjRjNDM2MmM1ZTIy&sort=name&layout=list&num=50" >
说明问题“没有用到栈的空间就会混入其他的数值”的图片
<img src="https://docs.google.com/leaf?id=0B8PvEMuJQOSyYWE2NTc4NzctYjFjZi00NDk2LTk3ZDUtNjhkNjcyYzA3MmVm&sort=name&layout=list&num=50" >
以下是程序源代码:
assume cs:codesg,ds:data,ss:stack
stack segment
dw 8 dup(4) ;
stack ends
data segment
dw 8, 8, 8, 8, 8, 8, 8, 8
data ends
codesg segment
start:
;Stack SS:SP
mov ax, stack
mov ss, ax
mov sp, 16
mov cx, 0
push cx
mov cx, 2
push cx
mov cx, 32
push cx
call divide
pop CX ;32
pop AX ; 2
pop DX ; 0
mov ax, 4c00h
int 21h
divide: ;chapter: 8.7 Page 157
;divisor 除数
;divisor 除数 can 8 bits or 16 bits and can be in register or memory
;dividend 被除数 , have to be AX or DX and AX,
;divisor is 8 bits, then dividend is in AX. | the result is AL = quotient 商, AH = remainder 余
;divisor is 16 bits, then dividend is in DX and AX | the result is AX = quotient 商, DX = remainder 余
;call storeReg
pop CX ;32
pop AX ; 2
pop DX ; 0
div CX
push dx
push ax
;call restoreReg
RET
storeReg:
push ax
push dx
push bx
RET
restoreReg:
pop bx
pop dx
pop ax
RET
codesg ends
end start |