. : : Assembly Language : : .  |  首页  |  我提出的问题  |  我参与的问题  |  我的收藏  |  消息中心   |  游客  登录  | 
刷新 | 提问 | 未解决 | 已解决 | 精华区 | 搜索 |
  《汇编语言》论坛 ->CALL和RET指令
  管理员: assembly   [回复本贴] [收藏本贴] [管理本贴] [关闭窗口]
主题 : :  求高手指点,为什CALL之后SP的值会自动减2了。 这个问题卡住我三天了!  [已解决] 回复[ 4次 ]   点击[ 364次 ]  
yangbin990
[帖 主]   [ 发表时间:2011-08-31 04:47 ]   [引用]   [回复]   [ top ] 
荣誉值:4
信誉值:2
注册日期:2011-08-30 04:54
大家好:
我正在在练习解决除法溢出的习题。我的解体思路是想通过栈来传值。所以我想把除数、被除数先压入栈,然后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
yangbin990
[第1楼]   [ 回复时间:2011-08-31 04:50 ]   [引用]   [回复]   [ top ] 
荣誉值:4
信誉值:2
注册日期:2011-08-30 04:54
发现图片不能显示,请直接在浏览器访问这两个连接,谢谢

说明问题“SP的数值会自动的减去2”的图片  
https://docs.google.com/leaf?id=0B8PvEMuJQOSyNGYwZGRiZjUtZTc1Ni00YWEwLTkzYTktZjRjNDM2MmM1ZTIy&sort=name&layout=list&num=50

说明问题“没有用到栈的空间就会混入其他的数值”的图片   
https://docs.google.com/leaf?id=0B8PvEMuJQOSyYWE2NTc4NzctYjFjZi00NDk2LTk3ZDUtNjhkNjcyYzA3MmVm&sort=name&layout=list&num=50
chinatree
[第2楼]   [ 回复时间:2011-08-31 11:44 ]   [引用]   [回复]   [ top ] 
荣誉值:118
信誉值:0
注册日期:2011-07-07 22:59
当然会把sp减2了,因为在call时会把ip压栈,子程序返回时记得要堆栈平衡,如果ret时Pop的不是正确的IP,你的程序就飞了。
yangbin990
[第3楼]   [ 回复时间:2011-09-02 04:15 ]   [引用]   [回复]   [ top ] 
荣誉值:4
信誉值:2
注册日期:2011-08-30 04:54
谢谢,现在明白了。
yangbin990
[第4楼]   [ 回复时间:2011-09-02 04:16 ]   [引用]   [回复]   [ top ] 
荣誉值:4
信誉值:2
注册日期:2011-08-30 04:54
此贴由 贴主 于 [ 2011-09-02 04:16 ] 结贴。 结贴原因:问题已解决
得分情况: 2楼(chinatree):6分  
此问题已结贴!
    Copyright © 2006-2024   ASMEDU.NET  All Rights Reserved