Search code examples
windowsassemblytasm

TASM Print characters of string


I'm trying to print a string character by character, iterating through it. This is what I've got:

.MODEL SMALL
.STACK 64
.DATA
    string DB 'Something',0
    len equ $-string

.CODE

    xor bx, bx    
    mov si, offset string
Char:
    mov al, byte[si + bx]
    inc bx
    cmp bx, len
    je Fin

    mov ah, 2
    mov dl, al
    int 21h

    jmp Char

Fin:
    mov ax, 4c00h
    int 21h
END

I don't know if I'm getting the right mem reference of string, because it only shows me weird symbols. I tried adding 30 to dl, thinking it was because of the ascii representation.

How can I print char by char?


Solution

  • Here is a working example with Tasm, that is not mangling the begining of the string.

    It has one less jump due to the moving of the increment later and the replacement of the je with a jnz

    .MODEL SMALL
    .STACK 64
    .DATA
        string DB 'Something'
        len equ $-string
    
    .CODE
    
    Entry:
        mov ax, @data   ;make DS point to our DATA segment
        mov ds, ax
    
        xor bx, bx      ;bx <-- 0
        mov si, offset string ;address of string to print
    
        mov ah, 2       ;we will use repeatedly service #2 of int 21h
    
    Char:
        mov dl, [si + bx] ;dl is the char to print
        int 21h         ;ask DOS to output a single char
    
        inc bx          ;point to next char
        cmp bx, len     
        jnz Char        ;loop if string not finished
    
        mov ax, 4c00h
        int 21h         ;DOS exit program with al = exit code
    
    END Entry