flat assembler
Message board for the users of flat assembler.

Index > DOS > Variables definition placement and length

Author
Thread Post new topic Reply to topic
tagore



Joined: 20 May 2015
Posts: 1
tagore 27 May 2015, 15:56
Hello!

In this simply example of code I got different results, depended on the placement of db and the length of the defined variable.
The program prints “Hello World!”, the only difference is where the definition of the string is located.

Code:
        org 100h

string1 db 'Hello World!',10,0 ;(1)

        lea esi,[string1]

print:  mov dl,[esi]
        cmp dl, 0
        je  finish
        mov ah, 02h
        int 021h
        inc esi
        jmp print


finish: mov ah, 01h
        int 021h
        int 020h
;(2)string1 db 'Hello World!',10,0    


(1) The result is "llo World!" - missing two first characters.

When you put db on the end of the program(2) the result is "Hello World!" - correct one.

I can't see the difference, and why this should be wrong.

When you write "string1 db 'Hello World!', 13, 10, 0" or “string1 db 'Hello World!', 0” works in both cases, but I don't understand why.

From some trials I know that 10(line feed) is not the case, it depends of the string1 length, but why it should make a difference where is placed.
Post 27 May 2015, 15:56
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 27 May 2015, 17:55
The program is executed in the first line !
You have to put data separate (into another segment) or at the end of the program. You could also use jmp print in the first line to skip the data section.

Your code is executed from the beginning regardless if you think this is text or code. The CPU doesn't know and try to execute your data "Hello World" which gives unpredicted results. Wink
Post 27 May 2015, 17:55
View user's profile Send private message Send e-mail Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas 27 May 2015, 18:26
Exactly, you are making a .com executable, right? This format is just a flat binary that starts executing from the first byte, it doesn't have an "entry point". So your data gets executed as if it was code, who knows what kind of crazy instructions result from the "Hello World!" string... You could check with a DOS debugger...
Post 27 May 2015, 18:26
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.