Simple Hello world using x86 (32bit) assembly language using function

Simple Hello world using x86 (32bit) assembly language using function

here is some hello world function that would just add 10 to whatever number it is given

#Simple adder function in assebmly - just adds 10 the called number 
.code32 #if running the code on 64 bit machine
.section .data
.section .text

.globl _start
_start: #entry point

    #Assign the number to be modified here
    push $9 # using immediate access mode just assign number 9
    call adder10 #call the function adder10 here - basically assign a memory address name

    mov %eax, %ebx #pass the return value to ebx since return value sits on eax
    mov $1, %eax  # call system call exit on register eax
    int $0x80 #yup - interrupt it!

.type adder10, @function # like declare a function here ;)
adder10:
    #save the current base pointer first
    push %ebp

    #load the current stack pointer to the base pointer and use the new base pointer in the function
    mov %esp, %ebp

    #load the paramter on register ecx
    mov 8(%ebp), %ecx #access it using base index access method
    add $10, %ecx #do the magic

    #the return value will be on %eax
    mov %ecx, %eax

    #return the original values to the ebp and esp
    mov %ebp, %esp
    pop %ebp
    ret

Save the above file as adder.s
to run this if you are on 64 bit machine use

gcc -m32 -nostartfiles -o adder adder.s

otherwise

as -o adder.o adder.s
ld -o adder adder.o

and run it as

./adder

to see the result

echo $?

If all ok the above should print 19

See how you would solve these known algorithm problems

String Ordered Permutation Algorithm Problem

Check if two strings are anagrams or not

Array reversal in Recurrsion

testing k complementary pairs algorithm with junit

Find longest palindrom from sequence of characters

binary tree problems with solution

Kadane’s algorithm in C – Dynamic Programming

Check if there are three numbers a, b, c giving a total T from array A

Finding missing numbers from billion sequential number list file

Java solution for checking anagram strings – tell if phrases are anagrams

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*