Interrupts

So what is an interrupt? You could have noticed that all those instructions aren't very useful for everyday operations, e.g. displaying strings,opening files, whatever.
You could have thought: "How am I going then to do anything? Am I to code all this directly throught ports, or whatever it is called?"
You can feel relieved. Interrupts are for assembler, what library functions are for C, pascal, Perl or any HLL you can think about...
Ok, now some more info. Ready?
Do you know what an OS is? If in general and closely to our needs, it provides the interrupts(functions too, though(eg. WinApi in Windows)) for us to use. The primary interrupts are provided by BIOS, which is some kind of a place, where there are some microprograms stored, which are used for direct access to the hardware devices. But they aren't enough, so because of this, OS provides more interrupts. The most well known in this is DOS. It provides a whole bunch of them...
But the most important for newbies is:
int 21h - this one is responsible for displaying, reading, writing, whatever... Now how are we calling an interrupt?
First we choose a function we want to use... We place the special values into ax(it can be considered as a name of the function). And then pass parameters in registers.
First some of the functions:

  1. Display string
          ah - 09h
          dx - offset of the string(string ends with $ sign)
          Result:
              String is displayed...
  2. Read character with echo
          ah - 01h
          Result:
              The char you've entered is in al...

  3. Write character
          ah - 02h
          dl - character to write
          Result:
              The char is displayed...

  4. Terminate program
          ah - 4Ch
          Result:
              Program is terminated...

int 20h - terminate com program...

Some examples

All these programs are to be compiled with TASM. I'm not sure if it compiles with MASM...

The smallest program ever written

Now do you have a hex editor? Yes? If no, get it, you would need it anyway...
Now open it and just enter the following: C3
Save the file as 'exit.com' or smth. Now run it!
What, it does nothing? Yes, it should, it just exits! The main thing it didn't lock up your computer!
Now let's look at the size: 1 byte. Awesome!
You are great optimisers. You have managed to write a working program which fits in 1 byte!
No one asks, what it does, do they? ;)

Skeleton program

;===================================================================
; .Com declaration
;===================================================================
.model tiny
;===================================================================
; Data
;===================================================================
.data

.code
.386
org 100h
;===================================================================
; Code segment
;===================================================================
start:
    ret
end start

It's my own template... Ok, what needs explaining here? Everything? Hm,ok...
The strings beginning with ; indicate a comment.
.model tiny Indicates that would be .com program
.data indicates that data will go after it
.code code approaches
.386 instructions of the 386 processor are used(eg. 32 bit registers used)
org 100h this is required for com program, it indicates that program starts at offset 100h in memory...
start: just a label indicating that code will start executing immediately after it...
end start program ends Compile it like this:
       tasm test.asm
       tlink /t test.obj
Now you have test.com which is also exactly 1 byte long!

Hello, world!

;===================================================================
; .Com declaration
;===================================================================
.model tiny
;===================================================================
; Data
;===================================================================
.data
hello db "Hello, World$"
.code
.386
org 100h
;===================================================================
; Code segment
;===================================================================
start:
    mov dx,offset hello
    int 21h
    ret
end start

Ok, now some explanations...
DB - is so called pseudoopcode, which indicates that the following is data and must be input as is, each symbol takes a byte. Other examples:
     db 129h
     dw 130h  DW here indicates that the a word must be reserved
     dd 131h  DD double word reserved
What are words then? Ok, I hope you know that sequence of 8 bits is called a byte, do you?
Thus a sequence of 16 bits, or 2 bytes are called a word...
Then a sequence of 32 bits, or 4 bytes are... yes, that's right - a double word!

Exercise

1. Write a program, which would display a welcome message, ask some Y/N question and then continue or exit, depending on the answer!
2. A more advanced one. You will need to make some research. The program should display current date and time. The rest is up to you!
    P.S. This one also includes converting a number in register to a number in data!

P.S. There are always answers. You just need to look for them!