Click to See Complete Forum and Search --> : Whats wrong with this code?


heuristic
June 7th, 2006, 01:12 PM
Hi!
I am very new to assembly programming. I wrote this piece of code to display a string using dos int21h.


data_seg segment
mystr db 32, 'hello, world!$'
data_seg ends

code_seg segment
assume cs:code_seg, ds:data_seg

MAIN:
; init data segment
mov ax, data_seg
mov ds, ax

xor ax,ax
mov ah, 02h ; dos function to print
mov bx, 0 ; bx is the index

PRINTLOOP:
mov dl,[mystr+bx]
inc bx
int 21h ; output the char
cmp dl, '$' ; $ marks the end of the string
jnz PRINTLOOP


code_seg ends

END MAIN



I am printing the string character by character. I know about dos function 09h which prints the string.

But whats wrong with the above code??
I use TASM 4.1 on windows xp.

Any help will be appriciated.
thanks in advance
heuristic

olivthill
June 8th, 2006, 07:38 AM
It is safer to fill ah just before calling int 21h, although this may not be compulsory.
The addressing mode in the line mov dl,[mystr+bx] does not exist. Another adressing mode should be used, e.g.
Try :
data_seg segment
mystr db 32, 'hello, world!$'
data_seg ends

code_seg segment
assume cs:code_seg, ds:data_seg

MAIN:
; init data segment
mov ax, data_seg
mov ds, ax

xor ax,ax
mov bx, 0 ; bx is the index

mov bx, OFFSET mystr

PRINTLOOP:
mov dl, BYTE PTR [bx]
inc bx
mov ah, 02h ; dos function to print
int 21h ; output the char
cmp dl, '$' ; $ marks the end of the string
jnz PRINTLOOP

mov ah,4ch ; terminate
int 21h
code_seg ends

END MAIN

heuristic
June 9th, 2006, 02:15 AM
d'oh
thanks for pointing that out.

btw, should you explicitly call the 4ch function? doesnt the program automatically terminate when there is no more code?

olivthill
June 9th, 2006, 02:02 PM
I have added the two lines for the exit, because without them, and after having assembled and linked the program, I run it and got an "abnormal program termniation" message. Besides, I remember having often seen these two exit lines in source codes.

I have used my own assembly and linkedit softwares, called OTasm and OTlink, which are following Microsoft and Intel specifications.
Maybe, Tasm 4.1 does a better job, and include the lines for the exit when they are not present in the progam. I don't know.