Skip to content

Instantly share code, notes, and snippets.

@KeyC0de
Created November 21, 2020 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KeyC0de/e0dbdc72ed677c34d4d2ea457cbe4584 to your computer and use it in GitHub Desktop.
Save KeyC0de/e0dbdc72ed677c34d4d2ea457cbe4584 to your computer and use it in GitHub Desktop.
;This program calculates and displays the factorial of the number 3, using a recursive procedure
;Fact (n) = n * fact (n-1) for n > 0
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov bx, 3 ;for calculating factorial 3
call proc_fact
add ax, 30h
mov [fact], ax
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov edx,1 ;message length
mov ecx,fact ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
proc_fact:
cmp bl, 1
jg do_calculation
mov ax, 1
ret
do_calculation:
dec bl
call proc_fact
inc bl
mul bl ;ax = al * bl
ret
section .data
msg db 'Factorial 3 is:',0xa ; or 0
len equ $ - msg
section .bss
fact resb 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment