flat assembler
Message board for the users of flat assembler.

Index > DOS > Hello World Help :? (MZ EXE, 16-vs-32-bit, segments)

Author
Thread Post new topic Reply to topic
clonemaster



Joined: 18 Feb 2013
Posts: 4
Location: United States
clonemaster 18 Feb 2013, 16:18
Over the weekend I read and watched a few tutorials on 80x86 Assembly, and want to learn everything there is. Very Happy Here's my first app:

Code:
format MZ

org 100h

use32

segment .data

        msg db "Hello, World!", 13, 10, "$"

segment .code

        mov dx, msg
        mov ah, 09h ; Write string to STDOUT
        int 21h

        mov ax, 4Ch ; Exit program
        int 21h
    


It assembles, but I don't know how to tell weather it's a true 32-bit program or not. Also I don't know if my use of segments are correct.

Please let me know how to improve.
Post 18 Feb 2013, 16:18
View user's profile Send private message AIM Address Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 18 Feb 2013, 16:45
It is 16bit program. And it will not work because you didn't set properly DS.
Better use COM format following way. In this format the OS (DOS) sets the segment registers for you:

Code:
        org 100h

        mov dx, msg
        mov ah, 09h ; Write string to STDOUT
        int 21h

        mov ax, 4Ch ; Exit program
        int 21h

        msg db "Hello, World!", 13, 10, "$"
    

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
Post 18 Feb 2013, 16:45
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
clonemaster



Joined: 18 Feb 2013
Posts: 4
Location: United States
clonemaster 18 Feb 2013, 16:51
JohnFound wrote:

Code:
        org 100h

        mov dx, msg
        mov ah, 09h ; Write string to STDOUT
        int 21h

        mov ax, 4Ch ; Exit program
        int 21h

        msg db "Hello, World!", 13, 10, "$"
    


Is it better to put msg db after mov dx, msg? If so, why?
Post 18 Feb 2013, 16:51
View user's profile Send private message AIM Address Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 18 Feb 2013, 17:51
Because in the COM file, the execution goes from the first address ( 100h) down. You don't want to execute "Hello, World" string. It is data. So, I put it where it will be never executed, because the program terminates earlier.
Post 18 Feb 2013, 17:51
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
clonemaster



Joined: 18 Feb 2013
Posts: 4
Location: United States
clonemaster 18 Feb 2013, 19:13
Okay, got it! Is this how I add more lines to msg db?
Code:
org 100h
use16

        mov dx, msg
        mov ah, 09h ; Write string to STDOUT
        int 21h

        mov ax, 4Ch ; Exit program
        int 21h

msg db "Hello, World!", 13, 10, "$"
    db "Hello, World!", 13, 10, "$"
    db "Hello, World!", 13, 10, "$"
    
Post 18 Feb 2013, 19:13
View user's profile Send private message AIM Address Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1657
Location: Toronto, Canada
AsmGuru62 18 Feb 2013, 21:13
If you want to write all three lines in one operation - you need to remove the extra '$'.
Like so:
Code:
msg db "Hello, World!", 13, 10
    db "Hello, World!", 13, 10
    db "Hello, World!", 13, 10, "$"   ; <-- Only final '$' matters!

    
Post 18 Feb 2013, 21:13
View user's profile Send private message Send e-mail Reply with quote
freecrac



Joined: 19 Oct 2011
Posts: 117
Location: Germany Hamburg
freecrac 19 Feb 2013, 07:02
The next lesson is to write a string to the textscreen without using software interrupts.
Code:
org 100h
use16
        mov ax, 0B800h   ; Segmentaddress of the textscreen
        mov es, ax
        mov si, msg
        mov di, 17*160   ; target position inside of the textscreen(screensize of 80 rows and 25 lines: ASCII,color,ASCII,color, etc...)
        mov cl, Mlen     ; number of ASCIIs

P1:     mov al, [si]     ; load an ASCII from DS:SI
        inc si
        mov es:[di], al  ; write an ASCII to ES:DI
        add di, 2        ; next ASCII + next color
        dec cl           ; decrease our counter and if it is zero, then the zero-Flag will be set
        jnz P1           ; conditional jump if the zero-flag is not set

        mov ax, 4Ch ; Exit program
        int 21h

msg db "Hello, World!"
Mlen = ($-msg)           ; $ = placeholder/substituent of the current offset address (for to calculate the number of bytes between the address above)
    


A small variation of the code (with using some of the string instructions):
Code:
org 100h
use16
        mov ax, 0B800h  ; Segmentaddress of the textscreen
        mov es, ax
        mov si, msg
        mov di, 17*160  ; target position
        mov cl, Mlen    ; number of ASCIIs
        cld             ; clear direction flag

P1:     lodsb           ; load an ASCII from DS:SI + increase SI by one
        stosb           ; write an ASCII to ES:DI + increase DI by one
        inc di          ; step over color
        dec cl
        jnz P1

        mov ax, 4Ch ; Exit program
        int 21h

msg db "Hello, World!"
Mlen = ($-msg)
    

An other variation is to replace lodsb + stosb with movsb.

And if we add to each ASCII a color-attribut, then it is possible to use a repeat instruction in combination with the string-instruction:
"mov cx, (Mlen/2)" ....."repz movsw".....msg db "H", 7 , "e" ,7, "l", 7, "l", 7, "o" ,7....

Dirk
Post 19 Feb 2013, 07:02
View user's profile Send private message Send e-mail Reply with quote
clonemaster



Joined: 18 Feb 2013
Posts: 4
Location: United States
clonemaster 19 Feb 2013, 14:46
It's going to take me awhile to assimilate all this Dirk Wink
Post 19 Feb 2013, 14:46
View user's profile Send private message AIM Address Reply with quote
freecrac



Joined: 19 Oct 2011
Posts: 117
Location: Germany Hamburg
freecrac 20 Feb 2013, 19:52
clonemaster wrote:
It's going to take me awhile to assimilate all this Dirk Wink

This is no problem.

I hope that a found a balance of information in the remarks trying to prevent an overload. But if you think there is something to much obscure, please ask again.

Dirk
Post 20 Feb 2013, 19:52
View user's profile Send private message Send e-mail 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.