section .bss number: resb 1 temp: resb 1 num: resb 1 n: resw 1 nod: resb 1 section .data msg1 db "Enter the number : " size1 equ $-msg1 msg2 db "factorial of number: " size2 equ $-msg2 section .text global _start _start: ;Printing the message to enter the number mov eax, 4 mov ebx, 1 mov ecx, msg1 mov edx, size1 int 80h ;Reading the number mov eax, 3 mov ebx, 0 mov ecx, number mov edx, 1 int 80h sub byte[number], 30h mov al, byte[number] mov ah,0 mov word[n],ax call fact mov word[num],cx mov byte[nod], 0 ;No of digits... ;to display msg2 mov eax, 4 mov ebx, 1 mov ecx, msg2 mov edx, size2 int 80h extract_no: cmp word[num], 0 je print_no inc byte[nod] mov dx, 0 mov ax, word[num] mov bx, 10 div bx push dx mov word[num], ax jmp extract_no print_no: cmp byte[nod], 0 je end_print dec byte[nod] pop dx mov byte[temp], dl add byte[temp], 30h mov eax, 4 mov ebx, 1 mov ecx, temp mov edx, 1 int 80h jmp print_no end_print: ; for new line mov eax, 1 mov ebx, 0 int 80h fact: mov ax, word[n] cmp ax, 0 je terminate push word[n] dec word[n] call fact pop word[n] mov dx, word[n] mov ax, cx mul dx mov cx, ax jmp exit terminate: mov cx, 1 exit: 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