【问题标题】:Colored string is printing in reverse order not forward彩色字符串以相反的顺序打印而不是向前
【发布时间】:2022-01-17 07:51:04
【问题描述】:

在汇编语言中,我想以正序打印彩色字符串
"-- WELCOME TO E-VOTING MANAGEMENT SYSTEM --",但该字符串以相反的顺序打印。这是output
以下是我的代码:

;print screen
mov cx,45
mov si,-1
c1:
inc si
mov al,msg3[si]
mov ah,9
mov bh,0
mov bl,00110100b
int 10h
loop c1

【问题讨论】:

  • 效果不错:) int10/9 函数不移动光标。但是,它确实需要在CX 中重复计数。您打印第一个字符 45 次,然后打印第二个字符 44 次,依此类推。这意味着最后的重复仍然可见,因此明显的反转。

标签: string assembly x86 bios


【解决方案1】:

此 BIOS.WriteCharacterWithAttribute 函数 09h 不会使光标前进。为此,您可以使用 BIOS.Teletype 函数 0Eh。

由于 BIOS.WriteCharacterWithAttribute 函数 09h 使用 CX 寄存器作为重复计数,因此最好不要将 CX 用于循环计数器。下面的代码使用DI。此代码还避免使用因速度慢而臭名昭著的LOOP 指令!见Why is LOOP so slow?

;print screen
  mov  di, 45
  mov  si, -1
  mov  cx, 1       ; Repetition count
  mov  bx, 0034h   ; DisplayPage 0, Color RedOnCyan
TheLoop:
  inc  si
  mov  al, msg3[si]
  mov  ah, 09h
  int  10h
  mov  ah, 0Eh
  int  10h
  dec  di
  jnz  TheLoop

可以使用偏移量SI 来处理字符作为循环计数器。在一个有 45 个字符的字符串中,最后一个字符的偏移量为 44,所以这是我们在循环退出时需要达到的值:

;print screen
  mov  si, -1
  mov  cx, 1       ; Repetition count
  mov  bx, 0034h   ; DisplayPage 0, Color RedOnCyan
TheLoop:
  inc  si
  mov  al, msg3[si]
  mov  ah, 09h
  int  10h
  mov  ah, 0Eh
  int  10h
  cmp  si, 44
  jne  TheLoop

进一步的改进是使用 xor si, si 的一字节短编码,将偏移量 SI 从 0 开始。这会影响我们检查循环退出的方式!

;print screen
  xor  si, si
  mov  cx, 1       ; Repetition count
  mov  bx, 0034h   ; DisplayPage 0, Color RedOnCyan
TheLoop:
  mov  al, msg3[si]
  mov  ah, 09h
  int  10h
  mov  ah, 0Eh
  int  10h
  inc  si
  cmp  si, 45
  jb   TheLoop

除了使用固定的字符串长度计数,您还可以轻松地使用像零这样的字符串终止符。

;print screen
  xor  si, si
  mov  cx, 1       ; Repetition count
  mov  bx, 0034h   ; DisplayPage 0, Color RedOnCyan
TheLoop:
  mov  al, msg3[si]
  test al, al
  jz   Done
  mov  ah, 09h
  int  10h
  mov  ah, 0Eh
  int  10h
  inc  si
  jmp  TheLoop
Done:

  ...

msg3 db "--  WELCOME TO E-VOTING MANAGEMENT SYSTEM  --", 0

【讨论】:

  • 您已经有 SI 作为循环计数器,您可以只使用cmp si, 45cmp si,di 而不是同时倒计时另一个寄存器。
猜你喜欢
  • 1970-01-01
  • 2022-08-20
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 2016-02-17
  • 1970-01-01
  • 2019-12-19
相关资源
最近更新 更多