.data; fibonacci_seq: .space 400 // Array of integers to hold Fibonacci sequence values format_input: .asciz "Enter a Fibonacci index: " // Formatted string for user input prompt format_entered: .asciz "Entered Fibonacci index: %d\n" // Formatted string for showing the user entered index format_value: .asciz "Fibonacci value: %d\n" // Formatted string for showing the Fibonacci value format_sequence:.asciz "Fibonacci sequence up to index %d:\n" // Formatted string for showing the Fibonacci sequence format_scan: .asciz "%d" // Formatted string for scanning user input format_string: .asciz "Fibonacci sequence value at index %d: %d\n" fib_index: .word 0 // Integer variable to hold user entered Fibonacci index fibonacci_seq: .space 400 // Allocate space for the integer array /* * Code segment */ .text //Define the code segment using assembler directive .global main //Define the name of the function global using assembler directive main: str lr, [sp, #-16]! //Save the return address onto stack and adjust the stack pointer ldr x0, =str0 //Load the address of 'str' into x0 bl printf //Call 'printf' function to print 'str' content ldr x0 =str1 ldr x1, =n //Initialize ARM regigeter x0 with the address of correct format string bl scanf //Call printf() function to ask user to enter a Fibonacci Index ldr x0, =str2 ldr x1, =n bl printf // Initialize ARM register x0 with the address of the format_input string ldr x0, =format_input // Initialize ARM register x1 with the address of integer variable fib_index ldr x1, =fib_index // Call scanf() function to take user input (fibonacci index) bl scanf // Initialize ARM register x0 with the address of the format_entered string ldr x0, =format_entered // Initialize ARM register x2 with the address of integer variable fib_index ldr x2, =fib_index // Initialize ARM register x1 with the value of integer variable fib_index ldr w1, [x2] // Call printf() function to show user entered Fibonacci Index bl printf // Initialize ARM register x1 with the address of integer variable fib_index ldr x1, =fib_index // Initialize ARM register x0 with the value of integer variable fib_index ldr w0, [x1] // Call fibonacci_dynamic() function to get the fibonacci Value of user entered index using dynamic programming technique bl fibonacci_dynamic // Copy the return value of fibonacci_dynamic() function from ARM x0 register to x2 register // in order to pass it as the third parameter to next printf() function call. mov x2, x0 // Initialize ARM register x0 with the address of the format_value string ldr x0, =format_value // Call printf() function to show the fibonacci value of the user entered Index bl printf // Initialize ARM register x1 with the address of integer variable fib_index ldr x1, =fib_index // Initialize ARM register w0 with the value of integer variable fib_index ldr w0, [x1] // Call fibonacci_recursive function to get the fibonacci value of user entered index using recursive function call technique. bl fibonacci_recursive // Copy the return value of fibonacci_recursive() function from ARM x0 register to x2 register // in order to pass it as the third parameter to next printf() function call. mov x2, x0 // Initialize ARM register x0 with the address of the format_value string ldr x0, =format_value // Call printf() function to show the fibonacci value of the user entered Index bl printf // Initialize ARM register x0 with the address of the integer array fibonacci_seq ldr x0, =fibonacci_seq // Initialize ARM register x2 with the address of integer variable fib_index ldr x2, =fib_index // Initialize ARM register w1 with the value of integer variable fib_index ldr w1, [x2] // Call fibonacci_sequence() function to get the sequence of fibonacci values for all indices starting from 0 and ending at user entered index bl fibonacci_sequence // Initialize ARM register x0 with the address of the format_sequence string ldr x0, =format_sequence // Call printf() function to print a header before showing the fibonacci sequence values from the array bl printf // Initialize ARM register x0 with the address of the integer array fibonacci_seq ldr x0, =fibonacci_seq // Initialize ARM register x2 with the address of integer variable fib_index ldr x2, =fib_index // Initialize ARM register w1 with the value of integer variable fib_index ldr w1, [x2] // Call show_fibonacci_seq() function to show the sequence of fibonacci values for all indices starting from 0 and ending at user entered index bl show_fibonacci_seq mov x0, xzr //Assign zero to x0 in order to return zero from the function ldr lr, [sp], #16 //Adjust the stack pointer before returning from the function ret lr //Return from the function fibonacci_sequence: str lr, [sp, #-4]! // Save the return address onto stack mov w8, w1 // Move max_index value from x1 to w8 cbz w8, done // Branch to 'done' if max_index is equal to zero cmp w8, #1 // Compare max_index value with 1 beq one // Branch to 'one' if max_index is equal to one mov w9, #1 // Use w9 as the loop counter and initialize it to 1 mov w2, #0 // Use w2 to keep track of fib[n-2], initialize it to 0 str w2, [x0], #4 // Place w2 value (0) at position 0 of the array mov w1, #1 // Use w1 to keep track of fib[n-1], initialize it to 1 str w1, [x0], #4 // Place w1 value (1) at position 1 of the array loop: cmp w9, w8 // Compare loop counter with max_index value beq done // Branch to 'done' if they are equal add w3, w1, w2 // Compute fib[n] = fib[n-1] + fib[n-2] mov w2, w1 // Move fib[n-1] to fib[n-2] mov w1, w3 // Move fib[n] to fib[n-1] str w3, [x0], #4 // Place fib[n] at the appropriate position in the array add w9, w9, #1 // Increment the loop counter b loop // Branch to the beginning of the loop one: str w1, [x0], #4 // Place the value 1 at position 1 of the array b done // Branch to 'done' done: ldr lr, [sp], #4 // Retrieve the return address from the stack ret // Return from the function show_fibonacci_seq: // Save the return address onto the stack str lr, [sp, #-4]! // Copy the array address (x0) to x3 mov x3, x0 // Copy the max index (x1) to x4 mov x4, x1 // Use x1 as the loop counter and initialize it to zero mov x1, #0 loop_start: // Compare loop counter value (x1) with the max index (x4) cmp x1, x4 // Branch to the end of the function if they are equal beq end_func // Assign the address of the formatted string to x0 ldr x0, =format_string // Assign x2 the fibonacci sequence value pointed by index x1 from the array ldr x2, [x3, x1, lsl #2] // Save the current index value (x1) onto the stack before calling printf str x1, [sp, #-8]! // Save the address of the fibonacci sequence array (x3) onto the stack before calling printf str x3, [sp, #-8]! // Save fibonacci sequence array max index (x4) onto stack before calling printf str x4, [sp, #-8]! // Call printf() function bl printf // Retrieve fibonacci sequence array index limit from the stack into x4 ldr x4, [sp], #8 // Retrieve the address of fibonacci sequence array from the stack into x3 ldr x3, [sp], #8 // Retrieve current loop counter value from the stack into x1 ldr x1, [sp], #8 // Increment the loop counter add x1, x1, #1 // Branch to the beginning of the loop b loop_start end_func: // Retrieve the return address from the stack ldr lr, [sp], #4 // Return from the function ret
Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly
and start coding.
Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.
Every assembler may have it's own assembly language designed for a specific computers or an operating system.
Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.
Assembly language usually consists of three sections,
Data section
To initialize variables and constants, buffer size these values doesn't change at runtime.
bss section
To declare variables
text section
_start
specifies the starting of this section where the actually code is written.
There are various define directives to allocate space for variables for both initialized and uninitialized data.
variable-name define-directive initial-value
Define Directive | Description | Allocated Space |
---|---|---|
DB | Define Byte | 1 byte |
DW | Define Word | 2 bytes |
DD | Define Doubleword | 4 bytes |
DQ | Define Quadword | 8 bytes |
DT | Define Ten Bytes | 10 bytes |
Define Directive | Description |
---|---|
RESB | Reserve a Byte |
RESW | Reserve a Word |
RESD | Reserve a Doubleword |
RESQ | Reserve a Quadword |
REST | Reserve a Ten Bytes |
Constants can be defined using
CONSTANT_NAME EQU regular-exp or value
%assign constant_name value
%define constant_name value
Loops are used to iterate a set of statements for a specific number of times.
mov ECX,n
L1:
;<loop body>
loop L1
where n specifies the no of times loops should iterate.
Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
procedure_name:
;procedure body
ret