Monday, September 8, 2014

Get the factorial of given Number NASM

Get the factorial of given Number NASM(64 bit Linux):
%macro print 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2                                                             
syscall
%endmacro
%macro read 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2                                                             
syscall
%endmacro

SECTION .data
msg: db "Enter the number for the factorial::",0xA
len: equ $-msg

msg1: db "The factorial of the given number is::",0xA
len1: equ $-msg1

SECTION .bss
number : resb 10
result : resb 4
temp : resb 4

SECTION .text
global _start:
_start:

print msg,len
read number,4

convert:
        mov bx,00
        mov esi,number
        mov ecx,4
        loop1:
            rol bx,04
            mov al,[esi]
            cmp al,39h
            jbe loop2
            sub al,07h
        loop2:
            sub al,30h
            add bx,ax
            inc esi
           
        loop loop1
;call convert_hex_ascii   
Factorial:
    mov eax,ebx
    mov ecx,eax
   
    fact:
        dec ecx
        mul ecx
        cmp ecx,0001
    jne fact
        mov ebx,eax
        call convert_hex_ascii       
   
mov rax,60
mov rdi,0
syscall               
                   
convert_hex_ascii:
                mov ecx,8
                mov esi,result
                loop3:
                    rol ebx,04
                    mov ax,bx
                    and ax,0Fh
                    cmp ax,09h
                    jbe loop4
                    add ax,07h
                loop4:
                    add ax,30h
                    mov [esi],ax
                    inc esi
                loop loop3
        print msg1,len1
    print result,8

RET                   
   



No comments:

Post a Comment

Home Java Python PHP