TASM Print characters of string

耗尽温柔 提交于 2019-12-11 01:12:51

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/36731384/tasm-print-characters-of-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!