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.obala@bala-laptop:~/ASM$ ./Function firstfunctioncallsecondfunctioncallbala@bala-laptop:~/ASM$ gdb ./Function (gdb) break *_start+1Breakpoint 1 at 0x4000be: file Function.s, line 17.(gdb) runStarting program: /home/bala/ASM/Function Breakpoint 1, _start () at Function.s:1717 movl $FirstString, %ecx(gdb) print /x &FirstString $1 = 0x6000e8(gdb) s18 movl $18, %edx(gdb) print /x $rcx$3 = 0x6000e8(gdb) s19 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) sMyFirstFunction () at Function.s:1010 movl $4, %eax(gdb) print /x $rip$5 = 0x4000b0(gdb) s11 movl $1, %ebx(gdb) s12 int $0x80(gdb) sfirstfunctioncallMyFirstFunction () at Function.s:1313 ret(gdb) s_start () at Function.s:2121 movl $SecondString, %ecx(gdb) print /x $rip$6 = 0x4000cd(gdb) cContinuing.secondfunctioncallProgram 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.obala@bala-laptop:~/ASM$ ./Function2firstfunctioncallsecondfunctioncallbala@bala-laptop:~/ASM$ gdb ./Function2(gdb) break *_start+1Breakpoint 1 at 0x4000cc: file Function2.s, line 22.(gdb) runStarting program: /home/bala/ASM/Function2 Breakpoint 1, _start () at Function2.s:22warning: 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) s23 movl $18, StringLen(gdb) s24 call MyFirstFunction(gdb) x /1xw &StringPtr 0x600138 : 0x00600110 (gdb) x /1xw &StringLen 0x60013c : 0x00000012 (gdb) x /1dw &StringLen 0x60013c : 18 (gdb) sMyFirstFunction () at Function2.s:1313 movl $4, %eax(gdb) s14 movl $1, %ebx(gdb) s15 movl StringPtr, %ecx(gdb) s16 movl StringLen, %edx(gdb) s17 int $0x80(gdb) sfirstfunctioncallMyFirstFunction () at Function2.s:1818 ret(gdb) cContinuing.secondfunctioncallProgram exited normally.(gdb)
No comments:
Post a Comment