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 

Assembly Online Compiler

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.

About Assembly

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.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

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.

Procedures

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