0
.model small
.stack 100h
.data
    buffer db 8 dup("$")
.code
    main proc
    mov ax,@data
    mov ds,ax

    LEA dx,buffer
    mov ah,0Ah
    int 21h

    LEA dx,buffer
    mov ah,09h
    int 21h
    
    LEA dx,buffer
    mov ah,09h
    int 21h
    
    mov ah,4ch
    int 21h
main endp
end main

I took input from user by storing 0A in ah register and then I am trying to output the string on screen twice, but the program printing the string only once. What can be the problem?

  • You set up DS twice, unnecessarily, but you only make one `int 21h` / `AH=09h` system call to output the string. Your program isn't even trying to print it twice. – Peter Cordes Sep 15 '22 at 06:55
  • @PeterCordes I edited the code now, can you please tell why it's not printing the string twice? – Ashutosh Deshmukh Sep 15 '22 at 07:10
  • 1
    You don't need to keep setting DS to the same value; `int 21h` won't modify it. It's not doing any harm, but it's cluttering up your code. Anyway, did you single-step with a debugger to see what ends up in the buffer? If it ends with a carriage return but not a newline, your output would overwrite itself. Or since you're not skipping the first 2 bytes (buffer length for `int 21h` / `ah=0ah`), you're probably printing the empty string twice as the first byte is still `'$'` (max input length of `'$'` = 36). But you said it was printing once... – Peter Cordes Sep 15 '22 at 07:20
  • 1
    @PeterCordes : The one time it printed was probably not printing at all and were the characters they input that echoed on the screen – Michael Petch Sep 15 '22 at 07:40
  • I checked with the debugger and found out that `0000` is getting moved to `dx` instead of the address of first character of `buffer` in `lea dx,buffer` command. What can be the solution to this? – Ashutosh Deshmukh Sep 15 '22 at 07:55
  • Here is an example that should work: https://pastebin.com/1Ha9Cti8 . I'm too lazy to find the related question on SO although I think Sep Roland has one somewhere. You also need to understand the buffer layout of the Int 21h/ah=0ah as documented here: http://www.ctyme.com/intr/rb-2563.htm – Michael Petch Sep 15 '22 at 08:00
  • `lea dx,buffer` loads the address of buffer into dx (the buffer happens to be at address ds:0x0000). And you want io load the address into DX. An alternative to `lea dx,buffer` is `mov dx, offset buffer` . To get the first byte of buffer you'd use `mov al, buffer` or more clearly written as `mov al, [buffer]` – Michael Petch Sep 15 '22 at 08:04
  • @AshutoshDeshmukh: `0000` is the correct offset for `buffer` relative to the DS segment base; it's the first thing in your `.data`. `int 21h`/`ah=09h` or `0ah` use the logical address `ds:dx`. – Peter Cordes Sep 15 '22 at 16:42

0 Answers0