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

如何在汇编语言中实现类似C++ cout效果打印输入字符并转大写

8086 DOS汇编实现小写字母输入转大写输出方案

前置说明

本实现基于MASM/TASM汇编语法,适配DOS实模式运行环境,使用DOS系统中断int 21h完成输入输出操作,匹配你给出的$结尾的字符串定义规范。

核心逻辑说明

你给出的字符串中<the lowercase letter the user inputted>是动态内容占位符,无法直接写死在静态字符串里,需要完全对齐C++的分段输出逻辑实现:

  1. 先输出固定前缀提示字符串
  2. 单独输出用户输入的小写字符
  3. 输出中间段固定字符串
  4. 最后输出转换完成的大写字符

小写转大写核心规则:小写字母a-z的ASCII码范围是0x61~0x7A,对应大写ASCII码比小写小0x20,直接减去0x20或者用AND AL, 0DFh清零第5位即可完成转换,如需兼容非法输入可以额外加范围判断逻辑。

完整实现代码

assume cs:code, ds:data

data segment
    lowercase  DB  "Input a letter: $"
    upper_pre  DB  "The uppercase equivalent of letter $"
    upper_suf  DB  " is: $"
data ends

code segment
start:
    mov ax, data
    mov ds, ax

    ; 输出输入提示字符串
    mov ah, 09h
    mov dx, offset lowercase
    int 21h

    ; 读取用户输入的单个字符,结果存入AL,自动回显
    mov ah, 01h
    int 21h
    mov cl, al  ; 暂存原始小写字符到CL
    sub al, 20h ; 小写转大写
    mov ch, al  ; 暂存转换后的大写字符到CH

    ; 输出换行优化显示效果
    mov ah, 02h
    mov dl, 0Dh
    int 21h
    mov dl, 0Ah
    int 21h

    ; 输出大写提示前缀
    mov ah, 09h
    mov dx, offset upper_pre
    int 21h

    ; 输出用户输入的小写字符
    mov ah, 02h
    mov dl, cl
    int 21h

    ; 输出中间固定字符串
    mov ah, 09h
    mov dx, offset upper_suf
    int 21h

    ; 输出转换后的大写字符
    mov ah, 02h
    mov dl, ch
    int 21h

    ; 程序正常退出
    mov ah, 4Ch
    int 21h
code ends
end start

运行效果说明

汇编链接后运行程序,会先打印Input a letter: 等待输入,输入任意小写字母后,会输出类似The uppercase equivalent of letter a is: A的结果,完全匹配需求的输出格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 09:36:07