Tag Archives: getting started

Python || Which Interpreter To Use?

Unlike compiled programming languages such as C, C++ or Java, Python is an interpreted language. Since this is the case, the type of interpreter you use will probably be the same no matter which programming environment you select. You can download the official Python interpreter here. Python version 3 will be featured throughout this site.

If you are using Windows, download the file named “Python 3.X.X Windows x86 MSI Installer“. If you are using Ubuntu, type the following command into the terminal:


sudo apt-get install python3

In terms of which IDE to use, there are a few choices, and your choice should probably be based on what you intend to write with it.

If you intend to program on Windows, the path of least resistance is to try the IDLE which comes bundled with the Windows installation of Python. This provides you with a straightforward IDE with (obviously) good integration with the Windows platform. This IDE is free, but is very basic and is almost like a simple text editor. So if you wish to go this route, there are other alternatives which are more beneficial and practical for use, such as Notepad++ or Gedit.

As far as IDE’s go, Eclipse grouped with PyDev is a popular alternative, as is PyScripter. Both of these will also give you a visual debugger and a build environment.

Ultimately, my opinion is, if you’re just starting out, go with Notepad++ or Gedit. Then when you’re more comfortable with what you’re doing – explore the alternatives.

Personally, I use PyScripter, and all the code that is posted on this site was created using that IDE

Assembly || Hello World!

This page will consist of creating the typical “hello world!” application. if you have never programmed in assembly before, this will be very interesting as the syntax is very different from most high level programming languages.

As noted on the introductory page, the assembly code presented on this site (X86-64) was assembled using The Netwide Assembler (NASM) under the Unix platform (Ubuntu) in association with C/C++ files. The purpose of combining Assembly code in association with C/C++ files is to demonstrate how each language “talks” to each other. Also, more importantly it is because today, it is unusual to create a stand alone program written completely in assembly language. Why is that the case? Because It is much easier and faster to program in a high level language than it is in assembly. So why should you learn assembly? Learning assembly can be most useful to help one gain a deeper understanding of how computers work, aswell as helping one to better understand how compilers and higher level languages like C work.

==== HELLO WORLD ====

All of the programs presented on this site will start with a simple C or C++ driver program like so:

The “driver” file really only has one task, and that is simply to call the assembly function named ‘DisplayHelloWorld()’ as noted on line 39. This is a routine that will be present among all the code on this site.

There are several advantages in using the C driver routine. First, this lets the C system set up the program to run correctly in protected mode. All the segments and their corresponding segment registers will be initialized by C. The assembly code doesn’t need to worry about any of this. Secondly, the C library will also be available to be used by the assembly code.

The following shows a simple assembly program utilizing the C function “printf” to display ‘Hello World’ to the screen.


Line 16 of the program defines a section that specifies memory to be stored in the data segment (whose name is .data). Only initialized data should be defined in this segment. On lines 19 to 21, several strings are declared. They will be printed with the C library, so they must be terminated with a null character (ASCII code 0). Remember there is a big difference between 0 and ’0’. Note, the number 10 is the ASCII code for a newline.

Uninitialized data should be declared in the .bss segment (named .bss on line 25). This segment gets its name from an early UNIX-based assembler operator that meant “block started by symbol.”

The code segment named .text is where instructions are placed. Note that if you are using Windows, the code label for the main routine (line 29 and 33) should have an underscore prefix, so it would be _DisplayHelloWorld. You would also need to do the same for printf (so it would be _printf). This is part of the C calling convention. This convention specifies the rules C uses when compiling code. It is very important to know this convention when interfacing C and assembly. (Note: This rule is specifically for DOS/Windows, the Linux C compiler does not prepend anything to C symbol names.)

The global directive on line 29 tells the assembler to make the asm main label global. Unlike in C, labels have internal scope by default. This means that only code in the same module can use the label. The global directive gives the specified label (or labels) external scope.

And there you have it! After you assemble the above code (see below), you should get this as your output

Welcome to My Programming Notes' Assembly Program.

Control will now be passed to the Assembly file...

--------------------------------------------
Hello World!
--------------------------------------------

Control has now been passed back from the Assembly file to the C file!

The return code is: 0

BYE!

==== ASSEMBLING THE CODE ====

This can be achieved by simply opening the teminal, and doing a copy/paste of the commands listed on the ‘driver.c’ file, lines 16 thru 19. Make sure to compile them in order for the sake of continuity.


Be advised, that the commands to assemble the code is designed to run in 64-bit mode. If you are not running a 64-bit machine, the commands will most likely fail to assemble.

If you are running a Windows computer and would like to assemble the code, look here or here for information.

You will need to change the 64-bit registers to 32-bit registers in the “helloWorld.asm” file, aswell as removing lines 38-52 and lines 80-94 respectively in order to run the program successfully.

Assembly || Which Assembler To Use?

A common question one may wonder is, which assembler should I use? There are a few choices, and your choice should probably be based on the type of platform you decide to operate with.

In terms of Assembly, there is a whole family of languages each specific to a different processor, and each language has several different names for a single language. These are the following designations which are often seen: IA-32, X86-32, X86-i386, 80×86, X86, X86-16, IA-64, X86-64, and so on. Some of those early languages are now obsolete. The extension of Assembly which will be presented on this site is the x86 instruction set, specifically being X86-64 (which is known as IA-64 in some documents). The X86-64 Assembly code will be assembled using the Unix platform (Ubuntu) in association with C/C++ files, demonstrating how each language “talks” to each other.

As far as which assembler to use, there are a few high level ones which are available to choose from. Some high level assemblers are Borland’s TASM, The Netwide Assembler’s NASM, Microsoft’s MASM, IBM’s HLASM (for z/Architecture systems), Alessandro Ghignola’s Linoleum, and Niklaus Wirth’s PL/360.

Of the assemblers listed above, The Netwide Assembler (NASM) is the recommended choice, as it is open source and free of charge. The Microsoft Macro Assembler (MASM), and The Borland Company’s Turbo Assembler (TASM) can also be used to learn Assembly Language. You may use other assemblers if you wish, though it is not guaranteed that the code presented on this site will work for assemblers other than NASM.

==== INSTALLING NASM ====

– On Windows –

(1) Visit the NASM homepage, and click on the tab which says "Downloads"

(2) Click on the link to the most current version of NASM, downloading the most recent archive for NASM (the zip file) located under the "win32" directory

(3) Once you've obtained the appropriate archive for NASM, nasm-XXX-dos.zip or nasm-XXX-win32.zip (where XXX denotes the version number of NASM contained in the archive), unpack it into its own directory (for example c:nasm).

Note: You can alternatively download the installer file located in the "win32" directory which will install NASM for you, forgoing the remaining steps.

(4) The archive will contain a set of executable files: the NASM executable file nasm.exe, the NDISASM executable file ndisasm.exe, and possibly additional utilities to handle the RDOFF file format.

(5) The only file NASM needs to run is its own executable, so copy nasm.exe to a directory on your PATH, or alternatively edit autoexec.bat to add the nasm directory to your PATH (to do that under Windows XP, go to Start > Control Panel > System > Advanced > Environment Variables; these instructions may work under other versions of Windows as well.)

(6) That's it - NASM is installed. You don't need the nasm directory to be present to run NASM (unless you've added it to your PATH), so you can delete it if you need to save space; however, you may want to keep the documentation or test programs.

– On Ubuntu –

NASM is currently located in the Ubuntu repository, so you can install it by simply opening the terminal window and entering the command:


sudo apt-get install nasm

– On Other Versions Of Unix –

(1) Visit the NASM homepage, and click on the tab which says "Downloads"

(2) Once you've obtained the Unix source archive for NASM, nasm-XXX.tar.gz (where XXX denotes the version number of NASM contained in the archive), unpack it into a directory such as /usr/local/src. The archive, when unpacked, will create its own subdirectory nasm-XXX.

(3) NASM is an auto-configuring package: once you've unpacked it, cd to the directory it's been unpacked into and type ./configure. This shell script will find the best C compiler to use for building NASM and set up Makefiles accordingly.

(4) Once NASM has auto-configured, you can type make to build the nasm and ndisasm binaries, and then make install to install them in /usr/local/bin and install the man pages nasm.1 and ndisasm.1 in /usr/local/man/man1. Alternatively, you can give options such as --prefix to the configure script (see the file INSTALL for more details), or install the programs yourself.

(5) NASM also comes with a set of utilities for handling the RDOFF custom object-file format, which are in the rdoff subdirectory of the NASM archive. You can build these with make rdf and install them with make rdf_install, if you want them.

Note: Instructions for installing NASM was taken from the official website located here. If you need further assistance installing NASM onto your computer, check out the help forums.

Java || Using If Statements, Char & String Variables

As previously mentioned, you can use the “int/float/double” data type to store numbers. But what if you want to store letters? Char and Strings help you do that.

===== SINGLE CHAR =====

This example will demonstrate a simple program using char, which checks to see if you entered the correctly predefined letter.

Notice in line 19 I declare the char data type, naming it “userInput.” I also initialized it as an empty variable. In line 26 I used an “If/Else Statement” to determine if the user inputted value matches the predefined letter within the program. I also used the “OR” operator in line 26 to determine if the letter the user inputted was lower or uppercase. Try compiling the program simply using this
if (userInput == 'a') as your if statement, and notice the difference.

The resulting code should give this as output

Please try to guess the letter I am thinking of: A
You have Guessed correctly!

===== CHECK IF LETTER IS UPPER CASE =====

This example is similar to the previous one, and will check if a letter is uppercase

Notice in line 26, an If statement was used, which checked to see if the user inputted data fell between letter A and letter Z. We did that by using the “AND” operator. So that IF statement is basically saying (in plain english)

IF ('userInput' is equal to or greater than 'A') AND ('userInput' is equal to or less than 'Z')

THEN it is an uppercase letter

The resulting code should give this as output

Please enter an UPPERCASE letter: p
p is not an uppercase letter

===== CHECK IF LETTER IS A VOWEL =====

This example will utilize more if statements, checking to see if the user inputted data is a vowel or not. This will be very similar to the previous example, utilizing the OR operator once again.

This program should be very straight forward, and its basically checking to see if the user entered data is the letter A, E, I, O, U or Y.

The resulting code should give the following output

Please enter a vowel: o
Correct, o is a vowel!

===== HELLO WORLD v2 =====

This last example will demonstrate using the string data type to print the line “Hello World!” to the screen.

The resulting code should give following output

Please enter a sentence: Hello World!
You entered: Hello World!

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac YOUR_CLASS_NAME.java

*** To run the compiled program, simply type this command:

java YOUR_CLASS_NAME

Java || Simple Math Using Int & Double

This page will display the use of int and double data types.

==== ADDING TWO NUMBERS TOGETHER ====

To add two numbers together, you will have to first declare your variables by doing something like this.

Notice in lines 14-16, I declared my variables, giving them a name. You can name your variables anything you want, with a rule of thumb as naming them something meaningful to your code (i.e avoid giving your variables arbitrary names like “x” or “y”). In line 29 the actual math process is taking place, storing the sum of “num1” and “num2” in a variable called “sum.” I also initialized my variables to zero. You should always initialize your variables.

I obtained data from the user by using the Scanner Class.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Add.java

*** To run the compiled program, simply type this command:

java Add

The above code should give you the following output:

Please enter the first number: 8
Please enter the second number: 24
The sum of 8 and 24 is: 32

==== SUBTRACTING TWO NUMBERS ====

Subtracting two ints works the same way as the above code, and we would only need to edit the above code in one place to achieve that. In line 29, replace the addition symbol with a subtraction sign, and you should have something like this

Note: In the above example, “cin.nextDouble()” was used on line 26 in place of “nextInt.” The declaration “nextDouble” can be used in place of “nextInt” in case you want to obtain floating point data from the user.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Subtract.java

*** To run the compiled program, simply type this command:

java Subtract

The above code should give you the following output

Please enter the first number: 8
Please enter the second number: 23.99999
The difference between 8 and 23.99999 is: -15.99999

==== MULTIPLYING TWO NUMBERS ====

This can be achieved the same way as the 2 previous methods, simply by editing line 29, and replacing the designated math operator with the star symbol “*”.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Multiply.java

*** To run the compiled program, simply type this command:

java Multiply

The above code should give you the following output

Please enter the first number: 7.999999
Please enter the second number: 24
The product of 7.999999 and 24 is: 191.99997


==== DIVIDING TWO NUMBERS TOGETHER ====

In division, when you divide numbers together, sometimes they end in decimals. Int data types can not store decimal data (try it yourself and see), so here is where the use of the double data type is mandatory.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Divide.java

*** To run the compiled program, simply type this command:

java Divide

The above code should give the following output

Please enter the first number: 7.99999
Please enter the second number: 23.99999
The quotient of 7.99999 and 23.99999 is: 0.33333305

==== MODULUS ====

If you wanted to capture the remainder of the quotient you calculated from the above code, you would use the modulus operator (%).

From the above code, you would only need to edit line 29, from division, to modulus.

===== HOW TO COMPILE CODE USING THE TERMINAL =====

*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)

javac Modulus.java

*** To run the compiled program, simply type this command:

java Modulus

The above code should give the following output

Please enter the first number: 23.99999
Please enter the second number: 8
The remainder of 23.99999 and 8 is: 7.9999905

Java || Which Compiler To Use?

A common question one may wonder is, which compiler should I use? There are a few choices, and your choice should probably be based on what you intend to write with it.

If you intend to program on Windows, a common choice is to download one of the many (free) Integrated Development Environment’s (IDE) which are currently available on the internet. The IDE’s I recommend are:

* NetBeans
* Eclipse

Netbeans and Eclipse are the equivalent of Visual Studios and CodeBlocks for C++. (NOTE: Netbeans and Eclipse is also available for download on Linux).

You will also need to have Java installed on your computer in order to develop Java applications. If you are using Windows, then visit the Oracle website. and download and install either JDK7 or JDK7 with Netbeans. Don’t bother with JRE, as it merely runs Java programs, but does not compile them.

==== LINUX USERS ====

To install Java, open the terminal window and enter the command:

sudo apt-get install openjdk-7-jdk

If you want to add Netbeans to your computer, then use the same terminal window and enter the command below:

sudo apt-get install netbeans

Alternatively, if you want to add Eclipse to your computer, then use the same terminal window and enter the command below:

sudo apt-get install eclipse-platform

Personally, I use a simple text editor (gedit) and the terminal window in Ubuntu to program in Java, and all the source code which is presented on this site was created using that development environment.