# using syscall and bios interrupt's in asm



## SIFE (Nov 24, 2009)

Salamo Alikom
i am trying to learn asm ,so i create tow simple program ,the first use syscall and secont use interrupt's :
*syscall :*

```
segment .data
 ftest db 'Learn ASM',0
 nbytes equ $-ftest
segment .text
 global _start
 _start:
 mov eax,4 ;write number syscall
 mov ebx,1 ;file descriptor ,output to screen
 mov ecx,ftest ;pointer to text
 mov edx,nbytes ;number of bytes
 int 0x80 ;call kernel
 mov eax,1 ;exit syscall
 mov ebx,0 
 int 80h ;call kernel
```


```
nasm -f elf -o write.o write.asm
ld -s -o write write.o
./write
```
output is empty here .
*interrupt :*

```
segment .data
 ftest db 'Learn ASM',0
 nbytes equ $-ftest
segment .text
 global _start
 _start:
 mov ah,09 ;bios video service
 mov dx,ftest ;ptr to text
 int 21h ;call bios service
 mov eax,1 ;exit syscall
 mov ebx,0
 int 80h ;call kernel
```


```
nasm -f elf -o write.o write.asm
ld -s -o write write.o
```
here i get this msg :

```
write.o(.text+0x1a): In function `_start':
: relocation truncated to fit: R_386_16 .data
```


----------



## expl (Nov 24, 2009)

You should read http://www.freebsd.org/doc/en_US.ISO8859-1/books/developers-handbook/x86.html

This manual will cover your problems.


----------



## Beastie (Nov 24, 2009)

FreeBSD doesn't expect parameters to be passed in registers. It expects them in the stack. You're using the Linux calling convention which needs... you've guessed it, the Linux compat layer, to work. Try this instead:

```
push nbytes
push ftest
push 1
push 0
mov eax,4
int 0x80
push 0
mov eax,1
int 0x80
```

BTW, ftest doesn't need to be zero-terminated as int 0x80 function 4 uses the string length you pass to it and doesn't calculate it itself.
On the other hand, it might use an (aesthetic) "0xa".

As for your second code, you're not using BIOS interrupts. You're using MS-DOS interrupts. No wonder it's not working.
But even if you used genuine BIOS interrupts, it shouldn't work on anything like a modern operating system running under protected mode (e.g. FreeBSD), unless you introduce some black magic in there.


----------



## SIFE (Nov 25, 2009)

now it is work ,where about linking 16bit program in 32bit .


----------



## cpghost (Jan 5, 2010)

Check out the FreeBSD/i386 and FreeBSD/amd64 ABI (i.e. calling conventions of the kernel). They aren't the same as under Linux.

Here's a FreeBSD assembler tutorial that covers hello world for both platforms and uses GNU as(1) that you may find useful.


----------

