Skip to content

Factorial Calculation Algorithm #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions algorithms/math/factorial.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
section .data
hexoutdata times 8 db 0, 10, 0
hexoutdatalength equ $-hexoutdata

section .text
global _start
_start:
mov eax, 4

call Factorial
bswap eax ;Fix little endian for proper hexidecimal readout
call HexConvert

mov eax, 4 ;write to file
mov ebx, 1 ;STDOUT handle
mov ecx, hexoutdata ;Message pointer
mov edx, hexoutdatalength ;Size of the message
int 80h ;System call

xor ebx, ebx ;Exit code is 0
mov eax, 1 ;Terminate process code
int 80h ;System call


HexConvert:
bswap eax
mov ecx, 8

hexloop:
mov ebx, eax
and ebx, 1111b

cmp ebx, 10
jb hexlessthanten
add ebx, 'a' - 10
jmp hexdone

hexlessthanten:
add ebx, '0'
hexdone:
mov edx, hexoutdata ;Base pointer
add edx, ecx ;Pointer offset
dec edx ;Correct to zero-based offset
mov esi, edx
mov [esi], bl
shr eax, 4
loop hexloop

mov eax, hexoutdata
add eax, 8
mov esi, eax
mov al, 0
mov [esi], al
ret

Factorial:
mov ebx, 1
fact_top:
imul ebx, eax
cmp eax, 2
dec eax
jne fact_top
mov eax, ebx
ret