Trying output previously input strings two in a row in TASM

894 views Asked by At

Well, title says it all: if for example I input "a", then I get this "antered string: a" (sic!). I do something wrong and I can not figure out what is happening.

model tiny
.code
org     0100h
start:
  mov     ax,cs           ;set data seg == code seg
  mov     ds,ax 

  mov dx,offset tm
  mov ah,0ah
  int 21h
  mov dx,offset testm
  mov ah,09h
  int 21h
  mov dx,offset tm
  add dx,2h
  mov ah,09h
  int 21h
  mov dx,offset tm
  add dx,2h
  mov ah,09h
  int 21h
  ret

tm  db 255,255,255 dup("$")
testm   db "Entered string: $"

End start
1

There are 1 answers

2
rkhb On

Int 21h/0Ah stores the pressed Enter key as 0Dh. This is for Int 21h/09h a "carriage return" and only a "carriage return". You need an additional "line feed". You can add the line feed (10h) to the end of the tm string or add a further Int 21h/09 procedure which points to a string only containing the two line feed characters and a terminating $ (crlf db 0Dh, 0Ah, "$").

Instead of adding a line feed you can change the 0Dh in tm to '$' to get the "raw" string:

MODEL tiny
.CODE
.386                                    ; for `movzx`
ORG 0100h

start:
    mov dx, offset tm
    mov ah, 0ah
    int 21h

    mov dx, offset crlf                 ; Carriage Return & Line Feed & '$'
    mov ah, 09h
    int 21h

    movzx di, byte ptr [tm + 1]         ; Length of string w/o the last 0Dh
    add di, OFFSET tm + 2               ; Plus start offset of string -> DI points to 0Dh
    mov byte ptr [di], '$'              ; Change 0Dh to '$'

    mov dx,offset testm
    mov ah,09h
    int 21h
    mov dx,offset tm
    add dx,2h
    mov ah,09h
    int 21h
    mov dx,offset tm
    add dx,2h
    mov ah,09h
    int 21h
    ret

tm      db 255,255,255 dup("$")
testm   db "Entered string: $"
crlf    db 0Dh, 0Ah, "$"

END start

An elegant method is to utilize the WriteFile function Int21h/40h:

model tiny
.code
org     0100h

start:
    mov dx, offset tm
    mov ah, 0ah
    int 21h

    mov dx, offset crlf
    mov ah, 09h
    int 21h

    mov dx, offset testm
    mov ah, 09h
    int 21h

    mov dx, offset tm + 2
    xor ch, ch
    mov cl, tm + 1              ; Length of tm = number of bytes to write
    mov ah, 40h
    mov bx, 1                   ; Handle 1: StdOut
    int 21h                     ; BX, CX & DX not changed

    mov ah, 40h                 ; Once more.
    mov bx, 1
    int 21h

    ret

tm      db 255,255,255 dup("$")
testm   db "Entered string: $"
crlf    db 0Dh, 0Ah, "$"

End start