Register | Login
Forum Index > User Tutorials > [BEGINNER]: Console Hello World!
Author Message
Pages: 1
Admin
Site Admin

avatar

(send private message)

Posts: 923
Topics: 54

Location:
OverHertz Studio
[1379] [BEGINNER]: Console Hello World! - posted: 2013-04-05 22:53:03
Hello readers!

In this tutorial i will go through writing your first simple application in Ziron

-TUTORIAL BEGIN-

For this turorial you will need Ziron 2 setup and ready to assemble files.

Now open up your favourite editor, whether it is notepad, notepad++ or whatever you prefere.

All Ziron 2 applications start off with a declaration of the program type (depending on installed linker/program plugins - we will go into this in another tutorial), for now we will use the supplied plugin. Available types are PE32_GUI, PE32_CUI etc..

so to begin we will write our program declaration:

Code:
program PE32_CUI 'my first application';


1. program allows the assembler to know this is a valid program file (entry source file).
2. PE32_CUI tells the linker plugin we want a PE COFF 32-bit Console User Interface application.
3. The final string is the name of your application, this string is not important and can be anything.

Now with our console we want to output a string "Hello, World!" but to do this, just like any other programming language you will need to include some files.

we will include 2 files.

1. win_def.zir - This file includes some other files for windows API etc.
2. console.zir - This file includes several functions that make our life easier for writing to the console.

Code:
#include 'win_def.zir';
#include 'console.zir';


Before we go any further, an important thing we will add to the bottom of our source is a windows API ExitProcess, i will not go into details about this for this tutorial.

Code:
ExitProcess(0);


place the above code at the bottom of your source file.

Now to move on, to output text to the console we can now use the println function from the console.zir we included.

Code:
println('Hello, World!');


Let us try it now.

Code:
program PE32_CUI 'my first application';

#include 'win_def.zir';
#include 'console.zir';

println('Hello, World!');

ExitProcess(0);


assemble and then run this, your console will pop open and write your text..... however it probably was so fast you didn't get to see it, so we will use the wait_key macro from the console.zir include, we can place this just before our ExitProcess call.

Code:
wait_key();
ExitProcess(0);


now when run the console will wait for a key press before exiting.

You have now written a very basic console Hello, World application.

Code:
program PE32_CUI 'my first application';

#include 'win_def.zir';
#include 'console.zir';

println('Hello, World!');

wait_key();
ExitProcess(0);



To conclude the tutorial i will go a little into the style of programming, as some programmers find it convenient to begin directly within a function such as in the language "c", we will do something similar.

the 3 lines below the includes we will move into a procedure

Code:
entry procedure WINAPI main() {

}


notice this procedure starts with "entry", this tells the assembler we want our application to begin from this point, WINAPI is actually a define which will in this case output stdcall. We can name our procedure anything we like, i have chosen "main".

Now 1 important thing to remember is that when you set an entry point below includes, any of the included files that require some initialization will not be executed, for example our console.zir requires some initialization.

So to solve this, whenever we set an entry we will call the function RTLMain, this will make sure all code outside of the functions will be executed, this should never be called if the entry is not a method as it will end up in a continuous loop or worse crash.

so our code will now look like this:

Code:
program PE32_CUI 'my first application';

#include 'win_def.zir';
#include 'console.zir';

entry procedure WINAPI main() {
  RTLMain();

  println('Hello, World!');

  wait_key();
  ExitProcess(0);
}


Assemble and it will run just the same, the difference being that you can now create local variables in your startup procedure, which we will go into in another tutorial.

-END TUTORIAL-

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Pages: 1
create new reply


Quick reply:

Message:



Currently Active Users:
There are currently 1 user(s) online. 0 member(s) and 1 guest(s)
Most users ever online was 533, May 5, 2016, 7:19 am.


Statistics:
Threads: 223 | Posts: 1835 | Members: 50 | Active Members: 50
Welcome to our newest member, NobodyAtWork
const Copyright = '2011-2017 © OverHertz Ltd. All rights reserved.';
Web development by OverHertz Ltd