section .data
num1: dw 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
prime: times 16 dw 0
odd: times 16 dw 0
avg: dw 0
sum: dw 0
section .text
global _start
_start:
mov ecx,16 ; number of elements in array num1
mov esi,num1 ; address of first element of num1 array
mov edi,prime ; address of first element of prime array
prime_loop:
mov eax,[esi] ; move the current element of num1 to eax register
cmp eax,1 ; check if the number is less than or equal to one
jle not_prime ; if yes then jump to not_prime label
mov ebx,eax ; move the current element of num1 to ebx register
dec ebx ; decrement ebx by one
prime_check_loop:
cmp ebx,1 ; check if ebx is less than or equal to one
jle prime_store ; if yes then jump to prime_store label
mov edx,0 ; clear edx register
mov eax,[esi] ; move the current element of num1 to eax register
div ebx ; divide eax by ebx and store remainder in edx register
cmp edx,0 ; check if remainder is zero or not
je not_prime ; if yes then jump to not_prime label
dec ebx ; decrement ebx by one
jmp prime_check_loop
not_prime:
add esi,2 ; increment esi by two (to skip even numbers)
loop prime_loop
prime_store:
mov [edi],eax ; store the current element of num1 in prime array
add edi,2 ; increment edi by two (to store next element in next memory location)
add esi,2 ; increment esi by two (to skip even numbers)
loop prime_loop
odd_loop:
mov ecx,16 ; number of elements in array num1
mov esi,num1 ; address of first element of num1 array
mov edi,odd ; address of first element of odd array
odd_loop_start:
mov eax,[esi] ; move the current element of num1 to eax register
cmp eax,-1 ; check if the number is less than zero (negative)
je odd_skip_negative_number; if yes then jump to odd_skip_negative_number label
mov edx,eax; move the current element of num1 to edx register
and edx,01h; perform bitwise AND operation with value '01h' (00000001b) and store result in edx register
; check if the last bit is set or not (if set then it's an odd number)
; if it's an odd number then store it in odd array otherwise skip it.
cmp edx,01h;
jne odd_skip_even_number;
mov [edi],eax; store the current element of num1 in odd array.
add edi,2; increment edi by two (to store next element in next memory location).
add dword [sum],eax; add the current element value into sum variable.
odd_skip_even_number:
add esi,2; increment esi by two (to skip even numbers).
loop odd_loop_start;
odd_skip_negative_number:
add esi,2; increment esi by two (to skip negative numbers).
loop odd_loop_start;
mov ecx,[edi]; get the count of elements stored in odd array.
mov eax,[sum]; get the sum value.
cdq; sign extend eax into edx.
idiv ecx; divide sum with count and store result into eax.
mov [avg],eax; store average value into avg variable.
section .data
section .bss
section .text 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