Skip to main content

Posts

Writing if statements in assembly language

Programs become more and more interesting when you have dynamic elements in them.  On such way of bringing your program to life is by adding logic.  In assembly, the task can seem dubious and awkward, but once you get a grip on the concept, it will be but second nature. So lets get started! //We want to write an equivalent program to this in assembly #include <stdio.h> int main(){ int x = 40; if( x > 10){ printf("x is greater than 10\n"); }else{ printf("x is lesser than 10\n"); } return 0; } To write this in assembly, consider the following: ;an equivalent program to this in assembly SECTION .data x: dd 40 msg1: db "x is greater than 10", 10, 0 msg2: db "x is lesser than 10", 10, 0 SECTION .text Here, all we did is create our variable x, and the respective message that we will display depending on the result of our if statement. Continually: ;an equivalent program to this in assembly SECTION .data x: dd 40 msg1: db "

NASM Assembly - Hello World

Whenever you start programming, there is usually the first program that prints the phrase "Hello world" to the screen.  Well, let us keep that tradition and write an entire assembly program that print that message to the screen. ;Our Assembly Program file SECTION .data SECTION .bss SECTION .text The preceding is the standard file format of an assembly program using the Netwide assembler, or NASM. To write something to the screen, we first need to store the value of what we want to render to the screen by declaring variables. ;Our Assembly Program file SECTION .data ourHelloMsg: db "Hello world, we are in assembly", 10, 0 ;our simple message SECTION .bss SECTION .text Next, we want to use some real world practical assembly coding to print this message to the screen.  We could simple using the Linux int80h instruction to tell the operating system to print this message (if you aren't sure what I mean by this, do not worry), however we will use the printf

NASM Programming

Many of you, if you are like me, might be interested in how assembly works.  You will be very surprised that assembly is very very easy, especially after you write a couple of simple programs.  But don't get me wrong, you will be frustrated at first, however that frustration, if you channel it right, will lead to serious life long learning and will give you a deeper appreciation of the beauty of assembly. For more tutorial on assembly and visualization of these information, visit my youtube channel . Okay so lets get started. We will be using Netwide Assembler (NASM) to write our program. The general format of NASM file is this: ;This is a comment SECTION .data ;declare variable here SECTION .bss ;declare actual, dynamic variable SECTION .text ;where your program code/assembly code lives ; Working with Data Section In your .data section, you can declare variables like this: nameOfVariable: db 32 ;this declares a variable names nameOfVariable with byte valu

C++ Smart Pointer

The following is the finalized code for a simple C++ smart pointer as demonstrated by my youtube video:  here Usage: void main(){      ...     Pointer<ObjectType> pointer(new ObjectType);     ... } /*  * Pointer.h  *  *  Created on: Mar 16, 2012  *      Author: ukaku  */ #ifndef POINTER_H_ #define POINTER_H_ #include <iostream> using namespace std ; /**  * Reference represents a counter that will be incremented or decremented as  * the number of references to a pecticular pointer is made;  *  * where data means: a symbol used to point to, or associate to another object  */ class ReferenceCounter { private :     int counter ; public :     ReferenceCounter ( ) {         counter = 0 ;   //initially set to zero     }     void increase ( ) {         counter ++;   //increase it     }     int decrease ( ) {         -- counter ;   //decrease it         if ( counter < 0 ) {             cout << "ReferenceCouner is <