Assignment 9
section .data
msg1 db "Factorial Of Given Number : "
len1 equ -msg2
msg0 db "Factorial of 0 : 1",10
len0 equ $-msg0
newline db 10,13
factorial dd 1
section .bss
final resb 8
result resb 4
counter resb 4
num resb 4
numh resb 4
numlen resb 4
%macro print 2
mov eax, 4
mov ebx, 1
mov ecx, %1
mov edx, %2
int 0x80
%endmacro
%macro read 2
mov eax, 3
mov ebx, 0
mov ecx, %1
mov edx, %2
int 0x80
%endmacro
section .text
global _start
_start:
xor eax, eax
mov dword[num], eax
print msg2,len2
pop ecx
pop ecx
pop ecx
mov edx, 00
up:
cmp byte[ecx + edx], 0
jz l1
inc edx
jmp up
l1:
mov esi, ecx
mov eax, dword[esi]
mov dword[num],eax
mov dword[numlen], edx
print num,dword[numlen]
print newline, 1
mov esi, num
call packnum ;convert ascii to hex
mov dword[numh], ebx
cmp dword[numh], 0
jne next2
print msg0, len0
jmp exit
next2:
mov ecx, dword[numh]
call facto
mov edx, dword[factorial]
mov dword[result],edx
call ascii
print msg1, len1
print final, 8
print newline, 1
exit:
mov eax, 1
mov ebx, 0
int 0x80
packnum:
mov eax, 0
mov ecx, dword[numlen]
mov dword[counter],ecx
mov ebx, 0
back:
mov al, [esi]
rol ebx, 4
cmp al, 39h
jbe next
sub al, 7h
next:
sub al,30h
or bl, al
inc esi
dec dword[counter]
jnz back
ret
ascii:
mov esi, final
mov dword[counter], 8
mov eax, dword[result]
bck2:
mov ebx, eax
rol ebx, 04
mov eax, ebx
and bl, 0fh
cmp bl, 9h
jbe next1
add bl, 7h
next1:
add bl, 30h
mov [esi], ebx
inc esi
dec dword[counter]
jnz bck2
ret
facto:
push ecx
cmp ecx, 01
jne next3
jmp exit1
next3:
dec ecx
call facto
exit1:
pop ecx
mov eax, ecx
mul dword[factorial]
mov dword[factorial], eax
ret