whenever I use int 21h
or int 80h
The int
instruction is a special variant of a call
instruction which is calling some function in the operating system.
This means of course that the int
instruction behaves differently in different operating systems:
int 21h
Interrupt 21h was used in MS-DOS and 16-bit Windows (Windows 3.x). Therefore this instruction could be used in MS-DOS and 16-bit Windows programs only.
The interrupt is not supported in 32-bit (or 64-bit) Windows programs. Linux does also not support this interrupt.
int 80h
This interrupt is supported in 32-bit Linux programs. 64-bit Linux versions can run 32-bit Linux programs (but you'll have to ensure that the program you are creating really is a 32-bit program and not a 64-bit program).
other interrupts (such as int 10h
)
... are neither supported by Linux nor by recent Windows versions. (They were supported in 16-bit Windows.)
int 80h
... it returns a segmentation fault.
Under Linux you may run the strace
command to see what is happening with the int 80h
system call.
I did this with your program and got the following output:
$ strace ./x.x
execve("./x.x", ["./x.x"], [/* 54 vars */]) = 0
strace: [ Process PID=3789 runs in 32 bit mode. ]
write(1, "", 0) = 0
--- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=NULL} ---
You can see that int 80h
does not generate a fault but it is executed correctly.
However the edx
register has the value 0. Therefore int 80h
will output the first 0 bytes (= nothing) of your "Hello World".
You'll have to add the instruction mov edx, 13
before the int 80h
instruction.
The segmentation fault happens later!
As a beginner of assembly language you should first realize what assembler is: Each assembler instruction represents some bytes in RAM memory.
The instruction mov eax, 4
for example represents the bytes 184, 4, 0, 0, 0
or the instruction int 80h
represents the bytes 205, 128
.
Your assembler program ends after the instruction int 80h
. However the RAM memory does of course not end after the bytes 205, 128
. The RAM memory will contain random data after the bytes 205, 128
.
Maybe the bytes in RAM found after that bytes are 160, 0, 0, 0, 0
which equals mov al, [0]
. This would cause a segmentation fault.
You'll have to add some instructions after the int 80h
instructions that will stop your program. Otherwise the CPU will interpret the bytes in RAM following the int 80h
instruction as instructions and execute them...