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

TASM write to stdout vs write to file behaviour difference when using print macro

这是我写入 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,cs
      mov  ds,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.
在代码中将 mov bx, file_out_handle 移到更高的位置。