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 _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 square: 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