Feeds:
Posts
Comments

Archive for the ‘Uncategorized’ Category

The other function of GDB is Text User Interface (TUI). GDB use curses library to show the source file, assembly output, program registers and gdb command in separate windows. The TUI mode is supported only where version of the curses library is available.

Before we start, type the code below and save it to ‘mycode.asm’.

1 section .text
2
3 global _start
4
5 _start:
6
7 mov eax,10
8 mov ebx,20
9 mov ecx,30
10 mov edx,40
11
12 ;exit
13 mov eax,0
14 int 0x80

Then compile with debugger function (-gstab)

$ nasm -f elf32 -gstab mycode.asm -o mycode.o
$ ld mycode.o -o mycode
$

There are few ways to activate TUI.

First you when you open gdb and start TUI directly.

$ gdb -tui

tui03
(more…)

Read Full Post »

Reverse the program process is very useful when you are debugging and realize that you need to go backward. Instead of re-run the whole process, starting GDB version 7, you can go to the previous process.

There are few reverse that you can do:

  1. reverse-continue or rc
  2. reverse-step or reverse-step count
  3. reverse-stepi
  4. next or reverse-next count
  5. reverse-nexti

Before you use ‘reverse’ command, you need to activate the recording process otherwise you will get an error below:
“Target child does not support this command”.

Let’s try.
Type the code below and save it to ‘break.asm’.

1 section .text
2 global _start
3 _start:
4
5 mov eax,1111
6 mov ebx,2222
7 call do_nothing1
8 call do_nothing2
9 call do_nothing3
10 mov ecx,3333
11 mov edx,4444
12 call exit
13
14
15 do_nothing1:
16 nop
17 nop
18 ret
19
20 do_nothing2:
21 nop
22 nop
23 ret
24
25 do_nothing3:
26 nop
27 nop
28 ret
29
30 exit:
31 mov eax,1
32 int 0x80

reverse01
(more…)

Read Full Post »

In this tutorial I will show you how to generate assembly language code from c using gcc.

First, type the c code below, save it to ‘hello.c’.

1 #include
2 main()
3 {
4 printf("Hello World!\n");
5 return 0;
6 }

To generate assembly code use option -S.

$ gcc -S hello.c -o hello.s

Check the result

$ ls -l hello.s
-rwxrwxr-x 1 darklinux darklinux 491 2018-12-29 18:10 hello.s
$

Open with text editor.

1 .file "hello.c"
2 .section .rodata
3 .LC0:
4 .string "Hello World!"
5 .text
6 .globl main
7 .type main, @function
8 main:
9 .LFB0:
10 .cfi_startproc
11 pushl %ebp
12 .cfi_def_cfa_offset 8
13 .cfi_offset 5, -8
14 movl %esp, %ebp
15 .cfi_def_cfa_register 5
16 andl $-16, %esp
17 subl $16, %esp
18 movl $.LC0, (%esp)
19 call puts
20 movl $0, %eax
21 leave
22 .cfi_restore 5
23 .cfi_def_cfa 4, 4
24 ret
25 .cfi_endproc
26 .LFE0:
27 .size main, .-main
28 .ident "GCC: (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1"
29 .section .note.GNU-stack,"",@progbits
30

(more…)

Read Full Post »

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 »

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 »

Older Posts »