如何在汇编中打印两次字符串

how to print string twice in assembly

我是装配新手,所以这可能是一个菜鸟问题。如何在一行中打印两次字符串。我有这段代码,但它只打印一次。这是作业,但我不想作弊,我只是想知道我的代码有什么问题。

dosseg
.model small
.stack
.data
     prompt1 db 13,10,"Enter a String: $"   
     prompt2 db 13, 10, "$"
     str1 db 30 dup("$")
.code
main proc
  mov ax,@data
  mov ds,ax
  mov es, ax

  lea dx,prompt1                
  mov ah,09h
  int 21h

  mov byte ptr str1, 30     
  mov dx, OFFSET str1
  mov ah,0ah
  int 21h

  lea dx,prompt2                
  mov ah,09h
  int 21h

  mov SI, 0002                  
  lea dx, str1[SI]
  int 21h
  int 21h

  mov ax,4c00h
  int 21h

main endp
end

打印了两份,但是两份重叠了。 int21/0a 将用 CR 字符终止输入缓冲区 只有 (不要问为什么),打印时只是将光标移回开头行,但不会前进到下一行。您可以在它们之间打印另一个换行符:

  mov SI, 0002                  
  lea dx, str1[SI]
  int 21h

  lea dx, prompt2                
  int 21h

  lea dx, str1[SI]
  int 21h

Re-reading你的问题,你好像想在同一行打印两次。在那种情况下,砍掉尾随 CR:

movzx si, byte ptr [str1+1]   ; the length
mov byte ptr [str1+si+2], '$' ; chop off CR
lea dx, [str1+2]
int 21h
int 21h