Feeds:
Posts
Comments

Archive for the ‘Nasm – Netwide Assembler’ Category

The ‘error: operation size not specified’ is a common error that occur when you try to copy a value into a location of a memory without specify the size of data that you want to copy.

I will show what it is.
Type the codes below and save it to ‘test.asm’.

1 section .text
2 global _start
3 _start:
4
5 mov ebx,33 ;copy '33' to ebx register
6 mov [x],5 ;copy '5' to location of address
7 ;pointer x
8
9 mov eax,1
10 int 0x80
11
12 section .data
13 x db 0

Compile it.

$ nasm -f elf32 test.asm -o test.o && ld test.o -o testtest.asm:5: error: operation size not specified
$

As you see the this error occur.

Now change the code:

1 section .text
2 global _start
3 _start:
4
5 mov ebx,33 ;copy '33' to ebx register
6 mov byte [x],5 ;copy '5' to location of address
7 ;pointer x
8
9 mov eax,1
10 int 0x80
11
12 section .data
13 x db 0

Compile it again.

$ nasm -f elf32 test.asm -o test.o && ld test.o -o test
$

When I specify the size of data (mov byte [x],5), there is no error now.

Read Full Post »

You can call C function from assembly but you can’t compile the object file with ‘ld’ command. You have to use ‘GCC’. If you want to use ‘GCC’ then you have to change the ‘_start’ with ‘main’.
Mostly, you will use:

section .text
global _start
_start:

But, if you use ‘gcc’ then you have to change it to become:

section .text
global _main
main:

Let’s try.
Type the codes below and save it to ‘hello.asm’.

1 extern printf
2
3 section .text
4 global main
5
6 main:
7 push ebp
8 mov ebp,esp
9
10 push msg ;move msg to stack
11 call printf ;call printf
12
13 mov esp,ebp
14 pop ebp
15 ret
16
17 section .data
18 msg db 'Hello, world.',0xA
19

Assemble it with ‘nasm’ to get the object file.

$ nasm -f elf32 hello.asm -o hello.o

Then compile it with gcc.

$ gcc hello.o -o hello
$ ./hello
Hello, world.
$

Type the code below and save it to ‘hello2.asm’.

1 extern printf
2
3 section .text
4 global main
5
6 main:
7 sub esp,4 ;allocate 4 byte in stack
8
9 lea eax,[mystring]
10 mov [esp],eax ; pointer to string
11
12 call printf ; call printf
13
14 add esp,4
15
16 ret
17
18 section .data
19 mystring db 'Hello World',0xA
20

Assemble the code to get the object code.

$ nasm -f elf32 hello2.asm -o hello2.o

Compile the object code with gcc

$ gcc hello2.o -o hello2
$ ./hello2
Hello World
$

 

Read Full Post »

Assembly Language is the most difficult programming language to learn, to code and to maintain. It’s not portable because it depends on the type processors and operating system. Instruction code in Intel will be different with PowerPC, Motorola any other processors. Although AMD processor is Intel Compatible but not all codes in Intel can run on AMD.
Programming will take longer time because it will take more codes for just simple print ‘Hello word’. More codes mean more difficult to debug when there is a problem. I will show you later.

So, if it’s

-difficult to develop
-difficult to maintain
-not portable

Why do we still need programming in Assembly?

There are 2 main reasons:

-Assembly Language is Very efficient in term of space in Hard disk, space in Memory and Speed.
-Direct access to hardware

Assembly is very efficient in time consumption when related with speed. It take less memory space and hard disk space compare to other programs which is written in high level language that perform the same task.

Then people will say, now, hard disk and memory are getting cheaper, so space of hard disk and memory are not the issue. And compilers are getting “smarter”. It can generate codes that much more efficient in space and time efficiency.
So, why still assembly.
(more…)

Read Full Post »

In this article, I will show you assembly command for arithmetic function.

INC
It’s increase ‘operand’ by 1 (one).
Syntax

INC destination

Type the codes below and save it to ‘inc.asm’. It’s a very simple program. It will increase the value in variable num by 1 from 2 to 3.

1 section .text
2 global _start
3 _start:
4
5 ;put number in num
6 mov eax,2
7 mov [num],eax
8 inc byte [num]
9
10 ;exit
11 mov eax,1
12 mov ebx,[num]
13 int 0x80
14
15 section .data
16 num db 0
17

Compile and run the program.

$ nasm -f elf32 inc.asm -o inc.o
$ ld inc.o -o inc
$ echo $?
0
$ ./inc
$ echo $?
3
$


(more…)

Read Full Post »

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
2
3 global printmsg
4
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
12
13 section .data
14 msg db 'Mixing assembly with c.',0xA
15 lenmsg equ $ - msg
16

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:
eax=6
ebx=file descriptor

(more…)

Read Full Post »

In assembly, register only know ASCII (American Standard Code for Information Interchange) characters. It mean, If you are working with number and need to display the result by calling syscall number 4 (32 bit x86), you can’t just put the result address into ecx register. Because, the ecx register will assume that your result in Hex number.
For example, your result is 7. ecx will assume that it’s 7hexadesimal not 7decimal meanwhile 7 is 37hexadesimal in ASCII table.

Let’s try in coding to see how it works.
It’s very simple program. I just do simple math, 4+2=6. I add 4 into eax register and 2 into ebx register. Then add ebx register to eax register, so the result will be:
eax = eax + ebx.
= 4 + 2
= 6

1 section .text
2 global _start
3 _start:
4
5 mov eax,4
6 mov ebx,2
7
8 add eax,ebx
9 mov [result],eax
10
11 mov eax,4
12 mov ebx,1
13 mov ecx,result
14 mov edx,1
15 int 0x80
16
17 mov eax,4
18 mov ebx,1
19 mov ecx,newline
20 mov edx,1
21 int 0x80
22
23 mov eax,1
24 int 0x80
25
26 section .data
27 newline db 0xA
28
29 section .bss
30 result resb 1

$ nasm -f elf32 add.asm -o add.o
$ ld add.o -o add
$ ./add
?
$

(more…)

Read Full Post »

« Newer Posts - Older Posts »