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

Linux下x86_64汇编实现光标移动:文本编辑器开发问题

在x86_64汇编中实现终端光标移动(非规范模式)

在关闭ICANON和ECHO的终端非规范模式下,移动光标最直接的方式是向标准输出写入ANSI终端转义序列,终端会自动识别这些序列并执行对应的光标移动操作。

对应方向的ANSI转义序列

每个方向键对应的光标移动转义序列(十六进制表示):

  • 右移:0x1b 0x5b 0x43(即ESC [ C)
  • 左移:0x1b 0x5b 0x44(即ESC [ D)
  • 上移:0x1b 0x5b 0x41(即ESC [ A)
  • 下移:0x1b 0x5b 0x42(即ESC [ B)

修改后的汇编代码

先在数据段定义这些转义序列,再在每个光标移动分支中调用sys_write系统调用输出对应序列:

section .data
    ; 定义光标移动的ANSI转义序列
    cursor_right db 0x1b, 0x5b, 0x43
    cursor_left  db 0x1b, 0x5b, 0x44
    cursor_up    db 0x1b, 0x5b, 0x41
    cursor_down  db 0x1b, 0x5b, 0x42
    seq_len      equ $ - cursor_right  ; 所有序列长度均为3字节

section .text
    ; ... 你的其他代码(如termios配置、输入读取逻辑) ...

    check_cursor_1:
        mov ah, [input + 1]
        cmp ah, 0x5b
        je check_cursor_2 

    check_cursor_2:
        mov ah, [input + 2]
        cmp ah, 0x43
        je right_cursor 

        mov ah, [input + 2]
        cmp ah, 0x44
        je left_cursor 

        mov ah, [input + 2]
        cmp ah, 0x42
        je down_cursor

        mov ah, [input + 2]
        cmp ah, 0x41
        je up_cursor
    
    right_cursor:
        mov rax, 1          ; x86_64 Linux下sys_write系统调用号
        mov rdi, 1          ; 文件描述符:stdout
        mov rsi, cursor_right
        mov rdx, seq_len
        syscall
        jmp exit
    
    left_cursor:
        mov rax, 1
        mov rdi, 1
        mov rsi, cursor_left
        mov rdx, seq_len
        syscall
        jmp exit

    up_cursor:
        mov rax, 1
        mov rdi, 1
        mov rsi, cursor_up
        mov rdx, seq_len
        syscall
        jmp exit
    
    down_cursor:
        mov rax, 1
        mov rdi, 1
        mov rsi, cursor_down
        mov rdx, seq_len
        syscall
        jmp exit

    ; ... 你的exit逻辑 ...

额外注意事项

  • 如果编辑器需要精确跟踪光标位置(比如插入/删除文本时定位),需自行维护cursor_x和cursor_y变量,每次移动后更新:
    • 右移时cursor_x += 1(需判断是否到达当前行尾)
    • 左移时cursor_x -= 1(需保证cursor_x >= 0)
    • 上下移动时调整cursor_y,同时可能需要根据目标行长度修正cursor_x
  • 务必在程序退出前恢复原终端配置,避免终端状态异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 04:12:40