You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

汇编程序问题:如何将屏幕右侧的字符移回左侧?

问题分析与修正

你的回移逻辑存在两个核心问题:

  1. 光标与字符清除逻辑错误:回移时未正确清除上一个位置的字符,导致每次打印的字符累积,最终填充整行。
  2. 循环计数器初始值与方向错误:for_3循环开始时cx为0,采用递增方式到79,与回移方向不匹配,且未对应正确的移动步数。

修正后的代码

include PCMAC.INC
.model small
.586
.stack 100h
.data
prompt          DB "Please enter a character to be printed: $" 
userCh          DB ?            
.code
delay           proc
                push cx ;;save the caller's CX register
                mov ecx,150000
for_2:          nop
                dec ecx
                jnz for_2
                pop cx ;; restore caller's CX
                ret
delay           endp

main            proc
                _Begin
                _PutStr prompt
                _GetCh
                mov userCh, al
                _PutCh 13, 10
                
                mov cx,79
for_1:          _PutCh userCh      ;;print left to right
                call delay
                _PutCh 8,32
                dec cx
                jnz for_1
                
                mov cx,79          ;; 重置计数器,对应79次左移
for_3:          ;; print right to left
                _PutCh userCh      ;; 在当前光标位置打印字符
                call delay
                _PutCh 8,32        ;; 退格并打印空格,清除当前字符
                _PutCh 8           ;; 再次退格,将光标左移一位
                dec cx
                jnz for_3

                _Exit 0
main            endp
                end main

关键修正点说明

  • 回移逻辑调整:

    1. 先在当前光标位置打印字符,延迟后执行_PutCh 8,32——退格回到字符位置,用空格覆盖清除字符。
    2. 额外执行一次_PutCh 8,将光标左移一位,为下一次打印做准备。
      这样每次循环只会保留当前位置的字符,上一个位置被清除,避免整行填充。
  • 循环计数器修正:
    进入for_3前将cx重置为79,采用递减方式循环79次,对应从最右侧(第79列)到最左侧(第0列)的移动步数,与左到右的移动次数对称。

内容的提问来源于stack exchange,提问作者Learning2Code

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 13:30:51