鱼C论坛

 找回密码
 立即注册
查看: 2774|回复: 2

[已解决]实验13第二题问题求助

[复制链接]
发表于 2019-3-16 10:26:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
(2)编写并安装int 7ch 中断例程,功能为完成loop指令的功能。
        参数:(cx)= 循环次数, (bx)= 移位。
        以上中断例程安装成功后,对下面的程序进行单步跟踪,尤其注意观察int、iret 指令执行前后CS、IP和栈中的转态。
        在屏幕中间显示80个‘ !’
  1. assume cs:code

  2. code segment
  3. start:
  4.                 mov ax, 0b800h
  5.                 mov es, ax
  6.                 mov di, 160*12
  7.                 mov bx, offset s - offset se
  8.                 mov cx, 80
  9. s:
  10.                 mov byte ptr es:[di], '!'
  11.                 add di, 2
  12.                 int 7ch
  13. se:
  14.                 nop
  15.                 mov ax, 4c00h
  16.                 int 21h
  17. code ends
  18. end start
复制代码



这道题我一共编写了两个程序但是有一个程序只有执行debug 能执行出来,单独执行exe总是崩溃我感觉这道题我逻辑没有错误为什么就是执行不了呢?
  1. assume cs:code

  2. code segment
  3. start:
  4.                 mov ax, cs
  5.                 mov ds, ax
  6.                 mov si, offset text_1
  7.                
  8.                 mov ax, 0
  9.                 mov es, ax
  10.                 mov di, 200h
  11.                
  12.                 mov cx, offset text_4 - offset text_1
  13.                
  14.                 cld
  15.                 rep movsb
  16.                
  17.                 mov ax, 0
  18.                 mov es, ax
  19.                 mov word ptr es:[7ch * 4], 200h
  20.                 mov word ptr es:[7ch * 4 + 2], 0
  21.                
  22.                 mov ax, 4C00h
  23.                 int 21h

  24. text_1:
  25.                 push bp
  26.                 dec cx
  27.                 jcxz text_3
  28.                
  29.                 mov bp, sp
  30.                 add word ptr ss:[bp + 2], bx
  31.                 pop bp
  32.                 iret
  33. text_3:
  34.                 iret
  35. text_4:
  36.                 nop
  37. code ends
  38. end start
复制代码

最佳答案
2019-3-16 13:05:44
  1. text_1:
  2.         push        bp
  3.         dec        cx
  4.         jcxz        text_3
  5.        
  6.         mov        bp, sp
  7.         add        word ptr ss:[bp + 2], bx
  8.         pop        bp
  9.         iret
  10. text_3:
  11.         iret
  12. text_4:
  13.         nop
复制代码


试想一下这样一种情况
一进来先执行了一个push bp,然后又执行了一个dec cx,然后执行jcxz text_3,条件成立,跳转到text_3执行iret
有没有发现,之前push到堆栈的bp没有pop出来,堆栈不平衡了

  1. assume cs:code

  2. code segment
  3. start:
  4.         mov        ax, cs
  5.         mov        ds, ax
  6.         mov        si, offset text_1
  7.        
  8.         mov        ax, 0
  9.         mov        es, ax
  10.         mov        di, 200h
  11.         mov        cx, offset text_4 - offset text_1
  12.         cld
  13.         rep        movsb
  14.        
  15.         cli
  16.         mov        word ptr es:[7ch * 4], 200h
  17.         mov        word ptr es:[7ch * 4 + 2], 0
  18.         sti
  19.        
  20.         mov        ax, 4C00h
  21.         int        21h
  22.        
  23. text_1:
  24.         push        bp
  25.         mov        bp, sp
  26.         dec        cx
  27.         jcxz        text_3
  28.         add        word ptr ss:[bp + 2], bx
  29. text_3:
  30.         mov        sp, bp
  31.         pop        bp
  32.         iret
  33. text_4:
  34.         nop
  35. code ends
  36. end start
复制代码

  1. assume cs:code

  2. code segment
  3. start:
  4.         mov        ax, 0b800h
  5.         mov        es, ax
  6.         mov        di, 160 * 12
  7.         mov        bx, offset s - offset se
  8.         mov        cx, 80
  9. s:
  10.         mov        byte ptr es:[di], '!'
  11.         add        di, 2
  12.         int        7ch
  13. se:
  14.         mov        ax, 4c00h
  15.         int        21h
  16. code ends
  17. end start
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-16 13:05:44 | 显示全部楼层    本楼为最佳答案   
  1. text_1:
  2.         push        bp
  3.         dec        cx
  4.         jcxz        text_3
  5.        
  6.         mov        bp, sp
  7.         add        word ptr ss:[bp + 2], bx
  8.         pop        bp
  9.         iret
  10. text_3:
  11.         iret
  12. text_4:
  13.         nop
复制代码


试想一下这样一种情况
一进来先执行了一个push bp,然后又执行了一个dec cx,然后执行jcxz text_3,条件成立,跳转到text_3执行iret
有没有发现,之前push到堆栈的bp没有pop出来,堆栈不平衡了

  1. assume cs:code

  2. code segment
  3. start:
  4.         mov        ax, cs
  5.         mov        ds, ax
  6.         mov        si, offset text_1
  7.        
  8.         mov        ax, 0
  9.         mov        es, ax
  10.         mov        di, 200h
  11.         mov        cx, offset text_4 - offset text_1
  12.         cld
  13.         rep        movsb
  14.        
  15.         cli
  16.         mov        word ptr es:[7ch * 4], 200h
  17.         mov        word ptr es:[7ch * 4 + 2], 0
  18.         sti
  19.        
  20.         mov        ax, 4C00h
  21.         int        21h
  22.        
  23. text_1:
  24.         push        bp
  25.         mov        bp, sp
  26.         dec        cx
  27.         jcxz        text_3
  28.         add        word ptr ss:[bp + 2], bx
  29. text_3:
  30.         mov        sp, bp
  31.         pop        bp
  32.         iret
  33. text_4:
  34.         nop
  35. code ends
  36. end start
复制代码

  1. assume cs:code

  2. code segment
  3. start:
  4.         mov        ax, 0b800h
  5.         mov        es, ax
  6.         mov        di, 160 * 12
  7.         mov        bx, offset s - offset se
  8.         mov        cx, 80
  9. s:
  10.         mov        byte ptr es:[di], '!'
  11.         add        di, 2
  12.         int        7ch
  13. se:
  14.         mov        ax, 4c00h
  15.         int        21h
  16. code ends
  17. end start
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-17 16:59:08 | 显示全部楼层
人造人 发表于 2019-3-16 13:05
试想一下这样一种情况
一进来先执行了一个push bp,然后又执行了一个dec cx,然后执行jcxz text_3,条 ...

嗯嗯明白了,因为我的前面的循环都出栈了(pop bp)但是最后这个iret没有把bp从栈理跳转出来,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 00:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表