Advertise

Monday 16 September 2013

Assembly Language Primer For Hackers (Part 10) Functions



Description: This is Part 10 of the "
Assembly Language Primer for Hackers" video series. Please start here by watching Part 1 of this series, if you have not already done so. In this video we will look at how to write functions in Assembly Language. The most important step in writing functions in assembly is to understand how to pass arguments to them and then read their return values. We will look at 2 techniques - using registers and using global memory locations to understand how this can be done. In this demo we will use our familiar "Hello World" program to demonstrate how to code a simple function using the "write()" syscall. We will use the Function.s program 

Code:
.data

    HelloWorld:
        .asciz "Hello World!\n"

    HelloFunction:
        .asciz "Hello Function!\n"

.text

    .globl _start

    .type MyFunction, @function

    MyFunction:    # String pointer and len to be added by caller

            movl $4, %eax
            movl $1, %ebx
            int $0x80
            ret

    _start :
        nop
        # Print the Hello World String
      
        leal HelloWorld, %ecx
        movl $14, %edx
        call MyFunction

        # Print Hello Function

        leal HelloFunction, %ecx
        movl $17, %edx
        call MyFunction

        # Exit Routine
      
    ExitCall:
        movl $1, %eax
        movl $0, %ebx
        int $0x80
to demonstrate argument passing using the CPU registers and Function2.s to demo argument passing using global memory location in the .BSS segment. 

Code:
.data

    HelloWorld:
        .asciz "Hello World!\n"

    HelloFunction:
        .asciz "Hello Function!\n"

.bss
    .lcomm StringPointer, 4
    .lcomm StringLength, 4

.text

    .globl _start

    .type MyFunction, @function

    MyFunction:    # String pointer and len to be added by caller

            movl $4, %eax
            movl $1, %ebx
            movl StringPointer, %ecx
            movl StringLength, %edx
            int $0x80
            ret

    _start :
        nop

        # Print the Hello World String
        movl $HelloWorld, StringPointer
        movl $14, StringLength  
        call MyFunction

        # Print Hello Function
        movl $HelloFunction, StringPointer
        movl $17, StringLength
        call MyFunction

        # Exit Routine
      
    ExitCall:
        movl $1, %eax
        movl $0, %ebx
        int $0x80

Disclaimer: We are a infosec video aggregator and this video is linked from an external website. The original author may be different from the user re-posting/linking it here. Please do not assume the authors to be same without verifying. 

Security tube is also providing linux Assembly course for 99$ here -> http://securitytube-training.com/online-...ly-expert/
 
World of Hacker © 2011 Creative Commons License
World of Hacker by KroKite is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at http://www.worldofhacker.com.
Permissions beyond the scope of this license may be available at https://groups.google.com/forum/#!newtopic/hackerforum.