Feb 10, 2011

Assembly macro - Some useful stuffs

Hello :)

After running through the nuisance of printing output and debugging, I have came up with these macro. It might be useful for you somehow :)

All bug free :) I hope the code is self-explained. But if there are any errors or things you want to add, please leave a comments :)

    ;useful macro call
    ;make new line: printChar 10 printChar 13
    ;add a break point in your code that stop TurboDebugger when you hit F9: addBreakPoint

    addBreakPoint macro
        ;save register
        push AX
  
        pushf                                                        ;AX=PSW
        pop AX                                                   ;
       
        or AX,0100h                                           ;set 8th bit (trap flag)
       
        push AX                                                  ;PSW=AX
        popf                                                         ;
       
        ;restore register
        pop AX
    endm

    printChar macro    character                        ;print a character
    ;save register
        push DX
        push AX
                   
        mov DL, character                                    ;
        mov AH, 02h                                    ;
        int 21h                                        ;
       
        ;restore register
        pop AX
        pop DX
    endm
   
    printMsg macro msgString                        ;print a message
        ;save register
        push DX
        push AX
                                   
        mov DX, offset msgString                    ;print q1_1 string
        mov AH, 09h                                    ;
        int 21h                                        ;           
       
        ;restore register
        pop AX
        pop DX
    endm
Some quick thing to note:
  • I tried doing CMP with 8-bit register and use the "less than" jump command, but it does not turn out as expected. Instead, JL only works when CMP is called for 2 16-bit register.

Feb 8, 2011

Printing integer with assembly

Assembly is really a trouble some language when it comes to performing "thought-to-be-simple" task. Printing an integer is an example.

Having completed the code for that, I hope posting it here will help those who are looking for away to do it :)
The code is not optimized, nevertheless, so you should use it as a reference only.

I made use of the 21, 9 interrupt, which prints a string delimited by the '$' to the screen.


;count the number of digit of a 1-byte integer
;number stored in AX (AL actually, since it's 1-byte)
;count value stored in CX

;convert the number into a string and print out

myStackSeg segment stack      
                    db 128 dup (?)                    ;space for stack
    tos                label word                        ;top of stack
myStackSeg ends


myDataSeg segment  
    _dec_10 db 10                                    ;declare decimal value 10
    intStr    db 4 dup(?)                                ;1-byte integer -> 3 digit max -> 4 space (1 for '$')
    myNumber equ 176
    _eos db '$'
myDataSeg ends


myCodeSeg segment
assume CS:myCodeSeg, DS:myDataSeg, SS: myStackSeg

start:
    ;initialize segment register
    mov    AX,    seg myDataSeg                            ;
    mov    DS,    AX                                        ;initialize DS
    mov AX, seg myStackSeg                            ;
    mov SS, AX                                        ;
    mov SP, offset tos                                ;initialize SS

  
    ;convert the digits into string
    mov DX, myNumber
    mov BX, offset intStr
  
    ;count the number of digit of a number in AX      
    mov AX, DX                                        ;AX store the number
    xor CX, CX                                        ;clear CX
    jmp countDigit                                    ;start counting
  
    nextDigit:                                        ;still got digit remain
        mov AH, 00h                                    ;clear remainder
        jmp countDigit                                ;count again
  
    countDigit:                                        ;count and div 10
        div _dec_10                                    ;div 10; AL=remainder, AH=div result
        inc CX                                        ;at least 1 digit      
        cmp AL, 0                                    ;if (div result)!=0
        jne nextDigit                                ;prepare to count again

    ;CX hold no of digit
    mov DL, CL                                        ;
    add DL, 48                                        ;convert to corresponding ASCII character
    mov AH, 02h                                        ;print character to screen
    int 21h                                            ;
  
    mov DI, BX                                        ;DI=@intPtr (array storing the characters to print)
    add DI, CX                                        ;move DI to end of string (eos)
    mov AH, _eos                                    ;copy the string delimiter '$'
    mov [DI], AH                                    ;to then end of string
    mov AX, myNumber                                ;AX=number to print
    ;jmp convertDigit                                ;start printing
  
    convertDigit:
        div _dec_10                                    ;div 10 AX
        add  AH, 48                                    ;AH=AX mod 10. convert it to ASCII character
        mov [DI], AH                                ;copy the character to its position in the output str
        mov AH, 00h                                    ;clear the character -> AX=AX div 10
        dec DI                                        ;move back 1 space in the output str
        loop convertDigit                            ;loop till all digits are converted
      
    mov DX, BX                                        ;print the converted string
    mov AH, 09h                                        ;
    int 21h                                            ;
  
exit:
    mov AH, 4Ch                                        ;exit
    int 21h                                            ;
myCodeSeg ends
end start

Jan 17, 2011

Assembly language for Intel 8086 - The intro

To many of us (especially those who studies CG2007 and haven't touched assembly language before, like myself 3 hours ago), the word "assembly" might be a bit scary. As we all know, it's one of the language that is most closed to machine code (which is not readable, if you are human =.=" ) and hence, one of the hardest to learn, understand and code.

It took me a while to get my first Assembly program running, as well as numerous search on Google. books and help file. And in the end, all I did was just outputting some text to the screen.

However, I would like to sum up all the steps here, for all those who haven't tried to get a better start.
 
Not to delay any more, we are going to build our first assembly program, which is a "simple" Hello World program.

The assembler
First, you will need to get some tools. In CG2007, we are assigned to use the TurboASM to assemble the code. However, as far as I had look through the sample code, it's quite complicated (especially, if you 've just started to learn like me). Hence, found another assembler that might do the trick: the A86 macro assembler.

To start, we will download the assembler here and put on to one of your directories on your HDD.
Next, go to the DOS command prompt (Start menu - Programs - Accessories - Command Prompt) and move to the directory you have extracted the A86 compiler (H:\A86 for me).
 
Tips:
When you open the command prompt, it will look something like this:
C:\>
You will move to another directory with the "cd" command. Type:
cd H:\A86
and the result will look like
H:\A86>


So you are now ready to compile your program.

Coding assembly

Next, you need to code. At this point, coding with assembly might seem challenging, because the syntax is quite different from any other high-level language like C, C++, C#, Java, etc.
Typically, a command in Assembly will have 3 component (not always, as you will soon see)
[command] [operand1] [operand2]

The command will determine the type of action, operand1 and 2 are registers. Since we are coding for the Intel 8086 chip, which offers 4 general 16-bit register (AX, BX, CX, DX) or 8 8-bit registers split from those (AH, AL, BH, BL, CH, CL, DH, DL), we will only be able to use these names (For more information about register, you will need to read through my lecturer's note or an external guide that I will provide at the end of the post).

Next, you will need a text editor. Notepad of Notepad++ will do.

Now, enter the following line code. I hope it's self-explained:

;An assemble comment is denoted with a semi-colon at the beginning
;it extends till the end of the line

MOV AH,02
;notice that the command doesn't end with anything, and there are 3 components
;    with a comma between the second and the third

;MOV is the command to move data. AH and 02(number 02)
;The commands move the DATA from the SECOND operand to the FIRST.
;In this case, since 02 is data already, it will move the VALUE 02 to the register AH
;You can have ~MOV AH, BL~ (w/o the ~), which moves data from register BL to AH
;You can also have ~MOV CH, "A"~, which moves the LETTER A (ASCII value 65 or 41H) to CH
;Doing ~MOV 12, CL is not acceptable, as the first operand is the destination register
;    which can't be a value

;By moving the value 02 to AH, we call a subfunction to handle outputing a character
;    this character is stored in DL

MOV DL,"H"
;here, we store the "H" into DL. The program hasn't output anything at this point

INT 21h       
;Here, 21h means number 21 in hexadecimal
;INT is the command to call the interrupt. the number after it defines the iterrupt to call
;    in this case, it's a dos interrupt, which calls the subfunctions about
;    to display the character

MOV AH,04Ch   
;Again, we call a sub-function to exit the program

MOV AL,00
;AL will store the exit code

INT 21h
;call the DOS interrupt, executing the sub-function above and exit

As you can see, to call a functions, we don't use function names but rather very obscure number. Hence, it would help to have a list of functions and interrupt available for reference. You can look at some commonly used functions here.

Compile and assemble

Then, we need to compile our program. As your DOS command prompt is in the directory of the A86 already, save the code file there also (example: H:\A86\test1.asm).

Then, from the DOS command prompt, type
A86 test1.asm
(or replace the filename you have chosen)
The assembly source code will get assembled into a .COM file.
Then, in type this to execute it in the command prompt
test1

You should see a single character H on screen.

Things to note

In conclusion, you will need these three command to write a single ASCII letter:

MOV AH,02
;This is to select the display character sub-function. It must be loaded to AH
MOV DL,"H"
;copy the data to be used. refer to the documents to know which register to store it
INT 21h
;call the interrupts that handles the sub-function.

For your pratice, try writing out the rest of the "ello World!" text.


I hope this guide helps as the first and easy step to assembly. Be wary that if you are coding with TurboASM, it's really different. I am working hard to understand it, so perhaps we will have an another guide soon.

Sincerely yours,


This guide was based on reference from:
  • http://www.skynet.ie/~darkstar/assembler

Dec 14, 2010

Why some filters are disabled in Photoshop


Well, admittedly, filter is one of the best features available in Photoshop is filters, which enable us to create pretty cool effect like turning a photograph into a water color painting, a mosaic picture (not really "mosaic" in a sense, but more like pixelating it)...

However, you might be surprised when got into such situation, when many of the filter is not available anymore.
By default, this shouldn't happen, which mean some settings in Photoshop. But what is it ?

I also went through the same problem, and luckily, I found out why. So I will be sharing it with you today.

Well, the problem is actually the color mode. Normally, you would be in will have 8 bits per channel color, hich mean for each of the RGB (red, green, blue) color, you will get 2^8=256 degree, and combining them together in RGB will give you 2^24, approximately 16 million colors.

Meanwhile, there are 16 bits/channel and 32 bits/channel also, which makes your picture more "real", in a sense that it can represent more color.
However, this makes some of the filter unusable, as they are not designed to work with such number of color.

So, to get all your filter backs, just return to 8 bits mode. But beware that if you have constrains about coloring, then it might make your photo much more uglier than you expect

To change, simply go to menu Image -> Mode -> 8 bit / Channel , and you are done.

Hope it helps


Dec 9, 2010

Source Code versioning with Google Code

Hello there,

Today, I am going to share my experiences about source code versioning, a very useful technique used in coding project to keep track of what you have done in your code.


First, what is source code versioning? Well, it's simply a method to store your code files and the changes you have made on them at specific points in time. It sounds simple and it is indeed simple. Basically, you just need to copy your code in to a folder and take note of what your changes in the files. Not only can you avoid situations where you unconsciously screw up something and create an uncountable number of errors in your projects, but you can also take advantage of the most powerful features version control: reverting to previous version of the project, team collaboration, to name a few. While this is not much of concern for small projects, where files are manageable, it is a huge help for big projects of thousands of files. In fact, big projects like Mozilla, NetBeans, OpenOffice benefits from this a lot.

If you are interested in existing programs for version control, please read the next section, otherwise, jump to "Project hosting with Google Code".

A little about Version Control Systems program (VCS)

There are two methods for version control: centralized and distributed. Basically, in the centralized method, all the code is hosted on a single sever and all the collaborators need to access the server to download the code, update changes… The benefit is a easy to manage and uniform project. However, every time changes are made, collaborators will need to upload it to the server, and this is inconvenient, especially when the server is down.

The counterpart of centralized technology is distributed, where code is divided onto local machines of developers. Here, they will update small changes on their own storage, called a "repository". After all changes and verified to work, developers will push all those changes onto the server. This help to keep the server updated with important changes, instead of being overloaded with small fries. Also, developer don't need internet access all the time to work.

To visualize it, you can think of the centralized technology as a single warehouse, where all developers are salesmen that only come in to take / deposit goods as they required. And these salesmen don't actually possess any goods.

Distributed system, on the other hand is a franchise. The central warehouse will distribute / receive goods from stores, which also have their own storages. The central will not care whatever the stores do, as long as the goods are sold.

In reality, software like SVN falls under the category of centralized version control, while Git and Mercurial falls under the distributed category.

Now, let's move on to hosting your project with Google.

Project hosting with Google Code

Google provide an open source environment to host your project. You will need a Google Account to create a 2GB hosting repository for you project. Also, you can join other existing projects as well.

While you can search for existing open source projects to join here, I will focus on creating a new project and usage of Mercurial Hg (for Windows).

The reason this guide is made is while learning to use Mercurial, I found out that the guide book "Mercurial: The Definitive Guide" is a bit overwhelmed for first time user, in my opinion, that is, those who have never heard of the term version control before, it's very demotivating. Also, after a few hours searching, I only found guides with for the command shell Mercurial, not the GUI version of Mercurial Hg, which is much more intuitive to use.

Not to delay you anymore, we will now create a project on Google Code by visiting this link:

http://code.google.com/hosting/createProject

(note, please click on the images for a larger preview)

You will need to fill in the project name, summary and description. Your version control system should be Mercurial. About the license, you may choose depend on your purpose (you can refer here). In case you don't know what to choose, then go for "GNU General Public License v2". Also, add in some labels to indentify your project. Click "Create project".

After finished, you will see your homepage like this. Congrats, now you have a repository for your own ^^.










Next step is created a clone of your repository. Basically, one or many clones will be allocated to a developers and he will be coding on that clones. After some iterations and verification, the (supposedly) satisfied developer will push his changes on to the main repository.

To create a clone, from the homepage, go the the "Source" tabs, then "Clones" and "Create a clone".

You will be prompted for information about this clone. By default, your Google account's name is associated with the clones.

Enter the details and click "Create repository clone". Again, you will see a familiar interface.





There is a couple of things to explore here:

  • "Source – Checkout" will give information to setup the local repository on your computer.

 

  • "Source – Browse" provides you with a list of files and directories currently inside your clone.

  • "Source – Changes" gives a visual overview of the changes you made. If you are the only developer, all the version goes in a straight line. However, if there are multiples developers involved, most of the times the code will be going in different branches.
  • "Source – Clones" list all the clones you have. I hope it should be simple enough to understand :P
Now, with the server setup, you will need to download Mercurial Hg for Windows at http://mercurial.selenic.com/downloads/

Follow the instruction to install it and restart your machine. Now you are ready.
Go to where you want to create your repository clones. After that, right-click and select "TortoiseHg -> Clone…"

Go back to Google code. Under the link Checkout of the clone you have just created, you will see a command, something like "hg clone [project url] [clone name]". Copy the project url and clone name to the source properties of the clone dialog

Finally, click Clone and you have now have a the codes on your hard drive. Of couse, at the moment, it is empty since we haven't added anything to it. But we will.

Add caption
On your hard drive, you should see another folder appearing. Go inside it and add all the code files you want. For me, to test out, I just add some random .txt and .c files.



Go back out to the folder, right-click on your clone and choose "Hg Commit". This will create a revision of your code on the hard drive for you to roll back and revise later. However, keep in mind that by committing, you only make LOCAL changes. The server clone is not updated.


After debugging (what we always do :P) and improve your code's readability, to ensure the quality, you can upload it to the server via an action called "push".

To do so, right-click on your local clone and choose "TortoiseHg -> Repository Explorer". Inside, find the Push icon and click it. You will then be prompted for username and password. The username is you Google Account username, while password is a generated string that you can find in your clones' Checkout tab. (There is a link of "googlecode.com password" inside).


That's it. Now, if you check your clone, you will see that the files are updated.


There are a few things you can also do: if you want to revert to the previous version of the code, in the Repository Explorer, choose that revision and right-click, choose "Revert". Confirm with Mercurial again and you are back to where you start. The beautiful thing is that after you have committed, you can always go back to that revision again.

In addition, say another user updated the directory clone, you will need to update his code. You will need to perform a Pull operation (the opposite of Push, as expected) in the Repository Explorer. The command is in the same toolbar as push, so I will not be including it here.








And now you can do version control :P It is really useful, since even for my 2nd year project in a semester, our group updated like 700+ revision of code. Imagine how hard it would be to manage those.



More reading will be available at inside the "Mercurial: The Definite Guide". I hope that the intro here is good enough for you to go on, read and understand the book. It lacks the GUI help, but the explanations are good, so, you just need to figure out where to execute that command in GUI J

Thank you for reading and see you next time J
Sincerely yours!