%macro write 4
    mov rax,%1 ; func num for o/p data
    mov rdi,%2 ; file descriptor ID for monitor
    mov rsi,%3 ; starting address of string
    mov rdx,%4 ; number of bytes to be displayed
    syscall  ; inbulit func
%endmacro
 
section .bss     ;uninitialized variable declaration
    input resb 1   
 
section .data   ;initialized variable declaration
    num db 8
    result db 0
    msg1 db "Enter number:",10
    msglen1 equ $-msg1
    msg2 db 10d,"Factorial of the number is: ",10
    msglen2 equ $-msg2
 
section .text   ; declare code segment
    global _start ; entry point for the program
 
_start:    ;program code starts here
    write 1,1,msg1,msglen1    ;calling macro to display msg 1,i.e "Enter number: "
    write 0,0,input,1         ;read the i/p from the user
    write 1,1,msg2,msglen2    ;calling the macro to display msg 2, i.e "Factorial:(result)"
 
    mov rsi, input        
    call ascii2hex
    mov [num], rax
 
    mov ax, 1
    mov bx, [num]
    call factorial
 
    mov esi,result
    mov cl,8
 
    call hex2ascii
    write 1,1,result, 4
 
    mov rax,60   
    mov rdi,0
    syscall
 
; returns the factorial in rax
factorial:
    cmp bx, 1
    je stop
 
    mul bx
    dec bx
    jmp factorial
 
stop:
    ret
 
 
 
hex2ascii:
    rol ax, 4
    mov dx, ax
    and ax, 0fh
    cmp al, 09
    jbe skip
    add al, 07h
 
skip:
    add al,    30h
    mov [esi], al
    inc esi
    mov ax,dx
    dec cl
    jnz hex2ascii
 
ret
 
; converts an ascii byte in rsi to its numeric value in rax
ascii2hex:
    mov al, byte[rsi]
    sub al, 30h
ret

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: