flat assembler
Message board for the users of flat assembler.

Index > DOS > Hello world console program

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Lucy Berie



Joined: 08 Dec 2012
Posts: 13
Lucy Berie
Hello, I'm Lucy Berie and I would like to write a "Hello World" console program by assembly language. I'm using C++ but also I very like assembly. Smile

Edit : Should I post this post to the (DOS or Windows category)?

I want to translate :
Code:
#include <iostream>

int main()
{
printf ("Hello World !");
}
    

To Assembly code...

If you tell me how and the code, it would be very helpful.


Last edited by Lucy Berie on 08 Dec 2012, 11:36; edited 1 time in total
Post 08 Dec 2012, 11:26
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
This is in the DOS release of FASM under examples\exedemo\:
Code:
; fasm example of writing simple EXE program

format MZ

        push    cs
        pop     ds

        mov     ah,9
        mov     dx,hello
        int     21h

        mov     ax,4C00h
        int     21h

hello db 'Hello world!',24h
     
Post 08 Dec 2012, 11:35
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
Lucy: You already have the code for hello world included in "examples" folder in the fasm zip file.

There are also examples for Windows if you are using that instead of DOS.


Last edited by revolution on 08 Dec 2012, 11:51; edited 1 time in total
Post 08 Dec 2012, 11:37
View user's profile Send private message Visit poster's website Reply with quote
Lucy Berie



Joined: 08 Dec 2012
Posts: 13
Lucy Berie
Thanks you, cod3b453. But the error :
Code:
format MZ          


Error : Narrow instructions are not encodable in
I'd like to know what this error says.

How to fix it?
Post 08 Dec 2012, 11:39
View user's profile Send private message Reply with quote
Lucy Berie



Joined: 08 Dec 2012
Posts: 13
Lucy Berie
Sorry, I should post the post to the "Windows" category.
Thanks all!
Post 08 Dec 2012, 11:41
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
You made some mistake. The code provided by cod3b453 compiles without errors.
Provide some details about what you are doing and what is your environment - OS, FASM version, etc.
Post 08 Dec 2012, 11:47
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
Lucy Berie wrote:
Thanks you, cod3b453. But the error :
Code:
format MZ          


Error : Narrow instructions are not encodable in
I'd like to know what this error says.

How to fix it?
This looks like a fasmarm error. Make sure you have the x86 version of fasm.

http://flatassembler.net/download.php
Post 08 Dec 2012, 11:50
View user's profile Send private message Visit poster's website Reply with quote
Lucy Berie



Joined: 08 Dec 2012
Posts: 13
Lucy Berie
Wow, revolution, thanks a bunch!
I redownloaded the latest version of Flat Assembler and it works fine.

My old Flat Assembler : 1.70.03 (I think I chose wrong type)
Windows : 86x (Win32)

Now it worked all right but the console/cmd closes down instantly.

Can you tell me why it is closing down? How to fix it? Thanks you.
Post 08 Dec 2012, 12:20
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
I know of three ways to solve this:
  1. Set the console properties to not close on exit, or
  2. Put a delay in the code, or wait for a key press, or
  3. Manually open a console window and run the file from there.
Post 08 Dec 2012, 12:35
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
Waiting for key press:
Code:
mov ah,0
int 16h
    

It should be inserted before that:
Code:
mov ax,4C00h
int 21h
    
Post 08 Dec 2012, 12:48
View user's profile Send private message Send e-mail Reply with quote
Lucy Berie



Joined: 08 Dec 2012
Posts: 13
Lucy Berie
Quote:

Set the console properties to not close on exit


The console/cmd program closes down instantly, so how to set the console properties? Or anything else?

Quote:
Wait for a key press

So, I'll need to know how to call a function. There are many functions which can do this : getch, getche, scanf etc...

Quote:
Manually open a console window...

Revolution, thanks you so much!

Code:
format MZ

        push    cs
        pop     ds

        mov     ah,9
        mov     dx,hello
        int     21h

        mov     ax,4C00h
        int     21h

hello db 'Hello world!',24h
    


Can you explain this code step-by-step? And how does this code print "Hello World" correctly?
Post 08 Dec 2012, 12:58
View user's profile Send private message Reply with quote
Lucy Berie



Joined: 08 Dec 2012
Posts: 13
Lucy Berie
Thanks AsmGuru62!

I tried your code, but the "Closing" problem still remains... Confused
Code:
mov ax,4C00h
int 21h

mov ah,0
int 16h
    

What do these code mean?
Post 08 Dec 2012, 13:04
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
Lucy Berie, you are writing more than reading. AsmGuru wrote "It should be inserted before that".
Post 08 Dec 2012, 13:16
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1155
ManOfSteel
As AsmGuru already said, you should reverse the code: the ... 16h code should come before the ... 21h one.

The first waits for a key press and the second ends the process with an error code of 0 (i.e. no error).

Interrupt 0x21 is provided by DOS (function 0x9 to print messages, 0x4C to exit, etc.)
Interrupt 0x16 is provided by the BIOS for keyboard processing.
Post 08 Dec 2012, 13:17
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
I think it's more convenient for you to write a windows program with message boxes. That's easy and the text (message box) doesn't disappear automatically. Yes - refer to the examples section, load, compile and execute them and try to understand. Use the easiest or nearest example and change it to your needs. That would be a good way to go deeper into it.

The beer example is a nice one to see what you can do with a few lines. Very Happy

I think you don't really want to write old DOS programs. It seems to be easier but you are learning an obsolete technique. Wink

That's more for retro programmers. Razz
Post 08 Dec 2012, 14:19
View user's profile Send private message Send e-mail Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
Smile
It is nice to see that person tries to learn so fast, that it even causes some trouble!

Lucy, welcome to the forums!

You asking: "How does it print?"

Basically, this is the principle of DOS programming: the interrupts.
These commands "INT 16h" or "INT 21h" called the INTerrupts.

This is exactly the calls to DOS to do something: print text, work with files, work with keyboard, make simple graphics.

What you do is fill registers with the information for DOS.
It is like passing parameters in C++ -- they passed in registers.

Example:
Code:
mov ah, 9
mov dx, hello
int 21h
    


Here:
AH is like the function name -- what is DOS to do with the text passed in DX.
DX is a string itself and text must be terminated with a '$' (24h code you see at the end).
It is like string in C, just instead of zero termination -- '$' sign is used here.
(What if you need to print a dollar sign? DOS has alternative function for this and you can also use colors in that other function)

Translated to C it would look like that:
Code:
#define PRINT_TEXT 9
...
HeyDOS_DoThis (PRINT_TEXT, "Hello World!");
    


How exactly DOS will do it -- that code is located inside the INT 21h code,
which you can see in debugger if you step into that interrupt. This
depends on what is text mode is console in and maybe even some
hardware coding may be there (writing to ports or directly into video memory)

You can find most of what DOS can do here:

http://www.ctyme.com/intr/int.htm

I suggest to start with:

INT 10h -- work with display (setting modes, moving cursor, etc.)
INT 16h -- keyboard
INT 21h -- DOS functions (files, memory, etc.)

And finally:

I think DOS is quite outdated (it is however a good start).
You can learn Win32 console -- it has all the features (even colors) plus much more memory and you can use 32 bit registers!
Post 08 Dec 2012, 15:56
View user's profile Send private message Send e-mail Reply with quote
Lucy Berie



Joined: 08 Dec 2012
Posts: 13
Lucy Berie
This is my code :
Code:
format MZ

        push    cs
        pop     ds

        mov     ah,9
        mov     dx,hello
        int     21h

        mov     ax,4C00h
        int     21h

        ;Pause...
        mov ah,0
        int 16h

        mov ax,4C00h
        int 21h


hello db 'Hello world!',24h
    


But, the problem still stands.... What's wrong?

I discovered :
format MZ
Is this a code which opens up the console screen?

db
This is variable code definition. It defines a variable, IMO... I see the assembly definition code is reversed... Smile

Edit :
Code:
        push    cs
        pop     ds
    


Why does the program need these code? (I see if I removed, it would print invalid characters...)

And, how to set up a "#define" command (looks like C++), I'd like to write :

Code:
#define Printf 21h

        mov     ah,9
        mov     dx,hello

        ;Call
        int     Printf

    


Last edited by Lucy Berie on 09 Dec 2012, 02:08; edited 1 time in total
Post 09 Dec 2012, 01:18
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 22:12; edited 2 times in total
Post 09 Dec 2012, 01:36
View user's profile Send private message Reply with quote
Lucy Berie



Joined: 08 Dec 2012
Posts: 13
Lucy Berie
Oh I think the value 4c00h is the exit code... Smile
Post 09 Dec 2012, 01:59
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
Lucy: You have two "exit"s in your code. One exit is before the pause and another exit is after the pause. Of course the pause and the last exit are never reached because the first exit quits immediately.
Post 09 Dec 2012, 02:23
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.