使用打印宏时,TASM 写入标准输出与写入文件的行为差异

问题描述

这是我写入 STDOUT 的宏:

; print string (immediate): ex. m_puts "hello!"
m_puts macro string
local @@start,@@data
      push ax dx
      push ds
      jmp short @@start     ; string is being stored
@@data db string,'$'        ; in the code segment
@@start:                    ; so skip over it
      mov  ax,cs
      mov  ds,ax       ; set DS to code segment
      mov  ah,9
      lea  dx,[@@data]
      int  21h
      pop  ds          ; restore registers
      pop dx ax
endm 

它写入 CS 并从中读取(通过临时使用 DS 指向 CS),将读取的内容写入 STDOUT。

用法示例: m_puts 'hello world!'

现在我正在做一些小的更改来写入文件:

m_puts_to_file macro string
local @@start,@@data
      push ax cx dx
      push ds
      jmp short @@start     ; string is being stored
@@data db string            ; in the code segment
@@start:                    ; so,skip over it
      mov  ax,ax            ; set DS to code segment
      mov  ah,40h
      mov  bx,file_out_handle
      mov  cx,si      ; how many bytes to write
      lea  dx,[@@data]
      int  21h
      pop  ds          ; restore registers
      pop dx cx ax
endm 

其中 file_out_handle 是记忆中的一个词。

但是,运行后没有任何内容写入文件:

mov si,12
m_puts_to_file 'hello world!'

如果我直接在数据段中定义 'hello world!',而不使用宏,则写入文件有效。

这里可能是什么情况,我该如何解决?

解决方法

mov  ax,cs
 mov  ds,ax            ; set DS to code segment
 mov  ah,40h
 mov  bx,file_out_handle

您的 file_out_handle 变量位于 DS 段中,但是您在将 DS 更改为指向 CS /strong>.
将代码中的 mov bx,file_out_handle 向上移动。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...