Assembly code example – square a number

Here is another example of assembly code that would square a number:

#Simple square function in assembly language for x86 architecture
.code32 #tell the assembler we are using 32bit

.section .data #we don't have nothing in the data section
.section .text

.globl _start

    push $9 #fixed value of 9
    # The call updates the eip - instruction pointer
    call square 

    mov %eax, %ebx #the return value would be on register %eax, so pass it on %ebx
    mov $1, %eax #system call for exit on eax register - that would be 1
    int $0x80 #interupt

.type square, @function
    mov 4(%esp), %eax #read the value to square basing from the stack pointer (esp)
    imul %eax, %eax # umm.. whatelse? multiply the value and store it on eax
    ret #return the value 

do the following from the command line to run the code

$ gcc -m32 -nostartfiles -o square square.s 
$ ./square 
$ echo $?

Shall produce 81 on the command prompt

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 ;)
    #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

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


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

and run it as


to see the result

echo $?

If all ok the above should print 19

Segmenation fault – core dump error assembly 32bit code on 64bit machine

I have a shiny 64 bit dell machine that tops ubuntu on it.
I working working on some other machine a bit of assemly code and I didn’t have a problem.
But when I run the same code my 64 machine i was getting the a segmentation falut error. Not surprising actually.
here is what I have done to resolve it:

1. Don’t forget to add .code32 at the beginning of your assembly file. [ talking about x86 architecture ]
2. Have the gcc multilib on your machine

sudo apt-get install gcc-4.7-multilib

3. When assembling and linking your file use

gcc -m32 -nostartfiles -o executablename filename.s

Change your executablename and filename.s accordingly

A hello world in Assebly language tha would show the first number that is greater than 80

# Simple program to stop when it gets the first number greater than 80

.section .data
number_store: #fixed name for the address holding
    .long 34,78,23,90,12,234,76,23,1 #Numbers to be dealt with. 
.section .text
.globl _start

    mov $0, %esi #initialize the esi 
    mov number_store(, %esi, 4), %eax # load the first number on the eax register

        cmp $80, %eax #compare the value on eax with 80
        jge exit_door #yup exit if the comparison met..
        inc %esi
        mov number_store(,%esi,4), %eax #load the next number from the array
        jmp begin_loop #go back to the loop

        mov %eax, %ebx #load the last value we have to ebx register
        mov $1, %eax # call the exit call here
        int $0x80 #interubt it

This will be running on the x86 processor.
To run it on the linux do the following from the command line:
The following command would- create the object file:

as greater.asm -o greater.o 

The next one will be the linker – we need it to create the executable

ld -o greater greater.0

and run it as


Now the value would be accumulated on the register ebx.. Since the exit parameter will be stored on the ebx, and we have a value being loaded on the ebx it will be just showing that:

echo $?