Friday, February 3, 2012

Functions In Assembly


Functions in Assembly
 * Defining a function in Assembly is as follows
.type MyFirstFunction, @fuction

MyFirstFunction:

ret


 * Function is called using "call MyFirstFunction"


Passing Arguments & Returing Values


 * Passing Arguments to Function
- Registers
- Global Memory locations
- Stack
 * Returning Value from a function
- Registers
- Global Memory locations


Program Starts here


.data
FirstString:
.asciz "firstfunctioncall\n"
SecondString:
.asciz "secondfunctioncall\n"
.text
.globl _start
.type MyFirstFunction , @function
MyFirstFunction: #String ptr & length will be added by caller
movl $4, %eax
movl $1, %ebx
int $0x80
ret
_start:
nop
#We will print firstfunctioncall here
movl $FirstString, %ecx
movl $18, %edx
call MyFirstFunction
#We will print secondfunctioncall here
movl $SecondString, %ecx
movl $19, %edx
call MyFirstFunction
#Now we will exit the program
movl $1, %eax
movl $0, %ebx
int $0x80


bala@bala-laptop:~/ASM$ as -ggstabs -o Function.o Function.s 
bala@bala-laptop:~/ASM$ ld -o Function Function.o
bala@bala-laptop:~/ASM$ ./Function 
firstfunctioncall
secondfunctioncall
bala@bala-laptop:~/ASM$ gdb ./Function 
(gdb) break *_start+1
Breakpoint 1 at 0x4000be: file Function.s, line 17.
(gdb) run
Starting program: /home/bala/ASM/Function 
Breakpoint 1, _start () at Function.s:17
17 movl $FirstString, %ecx
(gdb) print /x &FirstString 
$1 = 0x6000e8
(gdb) s
18 movl $18, %edx
(gdb) print /x $rcx
$3 = 0x6000e8
(gdb) s
19 call MyFirstFunction
(gdb) disassemble MyFirstFunction 
Dump of assembler code for function MyFirstFunction:
   0x00000000004000b0 <+0>: mov    $0x4,%eax
   0x00000000004000b5 <+5>: mov    $0x1,%ebx
   0x00000000004000ba <+10>: int    $0x80
   0x00000000004000bc <+12>: retq   
End of assembler dump.
(gdb) print /x $rip
$4 = 0x4000c8
(gdb) s
MyFirstFunction () at Function.s:10
10 movl $4, %eax
(gdb) print /x $rip
$5 = 0x4000b0
(gdb) s
11 movl $1, %ebx
(gdb) s
12 int $0x80
(gdb) s
firstfunctioncall
MyFirstFunction () at Function.s:13
13 ret
(gdb) s
_start () at Function.s:21
21 movl $SecondString, %ecx
(gdb) print /x $rip
$6 = 0x4000cd
(gdb) c
Continuing.
secondfunctioncall
Program exited normally.
(gdb) 


Program Starts here


.data
FirstString:
.asciz "firstfunctioncall\n"
SecondString:
.asciz "secondfunctioncall\n"
.bss
.lcomm StringPtr, 4
.lcomm StringLen, 4
.text
.globl _start
.type MyFirstFunction , @function
MyFirstFunction: #String ptr & length will be added by caller
movl $4, %eax
movl $1, %ebx
movl StringPtr, %ecx
movl StringLen, %edx
int $0x80
ret
_start:
nop
#We will print firstfunctioncall here
movl $FirstString, StringPtr
movl $18, StringLen
call MyFirstFunction
#We will print secondfunctioncall here
movl $SecondString, StringPtr
movl $19, StringLen
call MyFirstFunction
#Now we will exit the program
ExitCall:
movl $1, %eax
movl $0, %ebx
int $0x80


bala@bala-laptop:~/ASM$ as -ggstabs -o Function2.o Function2.s 
bala@bala-laptop:~/ASM$ ld -o Function2 Function2.o
bala@bala-laptop:~/ASM$ ./Function2
firstfunctioncall
secondfunctioncall
bala@bala-laptop:~/ASM$ gdb ./Function2
(gdb) break *_start+1
Breakpoint 1 at 0x4000cc: file Function2.s, line 22.
(gdb) run
Starting program: /home/bala/ASM/Function2 
Breakpoint 1, _start () at Function2.s:22
warning: Source file is more recent than executable.
22 movl $FirstString, StringPtr
(gdb) print /x &FirstString 
$1 = 0x600110
(gdb) x /1xw &StringPtr 
0x600138 : 0x00000000
(gdb) x /1xw &StringLen 
0x60013c : 0x00000000
(gdb) s
23 movl $18, StringLen
(gdb) s
24 call MyFirstFunction
(gdb) x /1xw &StringPtr 
0x600138 : 0x00600110
(gdb) x /1xw &StringLen 
0x60013c : 0x00000012
(gdb) x /1dw &StringLen 
0x60013c : 18
(gdb) s
MyFirstFunction () at Function2.s:13
13 movl $4, %eax
(gdb) s
14 movl $1, %ebx
(gdb) s
15 movl StringPtr, %ecx
(gdb) s
16 movl StringLen, %edx
(gdb) s
17 int $0x80
(gdb) s
firstfunctioncall
MyFirstFunction () at Function2.s:18
18 ret
(gdb) c
Continuing.
secondfunctioncall
Program exited normally.
(gdb) 

No comments:

Post a Comment