Tuesday, March 11, 2008

GNU as a.k.a. Gas

My first assembly program ..
Ah it sux, but demonstrating how to use Gas, gdb etc. with a program which is too simple, anyone would understand..
varmathe@opensuse103-vm:/temp/varmathe> cat test.S
/*
* First gas assembly program by Varghese Mathew !
* simply to add two numbers on the x86
* Purpose: demonstrate the usage of gas, gdb etc
*/

.global main /* define the main to be callable from libc */
.text /* text segment */
main: /* the prodigal main function */
movl $100, %eax # load 100 in eax
movl $200, %ebx # load 200 in ebc
addl %eax, %ebx # add the two and store in ebx

/* the end */

varmathe@opensuse103-vm:/temp/varmathe>
varmathe@opensuse103-vm:/temp/varmathe>
varmathe@opensuse103-vm:/temp/varmathe> gcc -g test.S
varmathe@opensuse103-vm:/temp/varmathe>
varmathe@opensuse103-vm:/temp/varmathe> gdb a.out
GNU gdb 6.6.50.20070726-cvs
Copyright (C) 2007 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i586-suse-linux"...
Using host libthread_db library "/lib/libthread_db.so.1".
(gdb)
(gdb) break main
Breakpoint 1 at 0x8048394: file test.S, line 10.
(gdb) run
Starting program: /temp/varmathe/a.out

Breakpoint 1, main () at test.S:10
10 movl $100, %eax # load 100 in eax
Current language: auto; currently asm
(gdb) info registers
eax 0x1 1
ecx 0xbfa61264 -1079635356
edx 0xbfa61200 -1079635456
ebx 0xb7edbff4 -1209155596
esp 0xbfa611dc 0xbfa611dc
ebp 0xbfa61238 0xbfa61238
esi 0xb7f25ca0 -1208853344
edi 0x0 0
eip 0x8048394 0x8048394

eflags 0x200246 [ PF ZF IF ID ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
(gdb) info reg eax
eax 0x1 1
(gdb) info reg ebx
ebx 0xb7edbff4 -1209155596
(gdb) n
11 movl $200, %ebx # load 200 in ebc
(gdb) info reg eax
eax 0x64 100
(gdb) info reg ebx
ebx 0xb7edbff4 -1209155596
(gdb) n
12 addl %eax, %ebx # add the two and store in ebx
(gdb) info reg eax
eax 0x64 100
(gdb) info reg ebx
ebx 0xc8 200
(gdb) n
0x080483a0 in __libc_csu_fini ()
(gdb) info reg eax
eax 0x64 100
(gdb) info reg ebx
ebx 0x12c 300
(gdb) c
Continuing.

Program exited with code 0144.
(gdb) q
varmathe@opensuse103-vm:/temp/varmathe>


.

No comments: