Archive for December 2nd, 2018

You can combining Assembly Language and C language for flexible programming. In this tutorial I will show a simple code to display a message that is written in assembly then it will be called from c.

Let’s start.
Type the code below and save it to ‘hello.asm’.

1 section .text
3 global printmsg
5 printmsg:
6 mov eax,4 ;sys_call write
7 mov ebx,1 ;file descriptor write
8 mov ecx,msg ;pointer to msg
9 mov edx,lenmsg ;len msg
10 int 0x80 ;call interrupt
11 ret ;return back to main program
13 section .data
14 msg db 'Mixing assembly with c.',0xA
15 lenmsg equ $ - msg

Compile it to be object file.

$ nasm -f elf32 hello.asm -o hello.o
$ ls -l hello.o
-rw-rw-r-- 1 darklinux darklinux 640 2018-12-01 11:09 hello.o

Type the codes below and save it to ‘hi.c’.

1 #include
2 main()
3 {
4 extern hello();
5 printmsg();
6 }

Compile the code with the assembly object file, then run the program.

$ gcc hi.c hello.o -o hello
$ ./hello
Mixing assembly with c.

It works.

Read Full Post »

In this article, I will show you how to:
-Creating a file
-Opening a file
-Reading a file
-Writing a file
-Closing a file

Creating a File

The registers used to create new file:
eax =8 (sys_create)
ebx = file name
ecx = file permission (4=read, 2=write, 1=executable). Set for UOG (user,owner,group).

Writing a file

The register used to write on file:
eax=4 (sys_write)
ebx=file descriptor
ecx=pointed to data buffer
edx=length of data

Opening a file

eax=5 (sys_open)
ebx=file name
ecx=access mode (0=read only, 1=write only, 2=read and write)
edx=file permission

Reading a file

eax=3 (sys_read)
ebx=file descriptor
ecx=pointer to data buffer
edx=buffer size

Closing a file

The registers used to close the file:
ebx=file descriptor


Read Full Post »