Created
November 21, 2020 11:57
-
-
Save KeyC0de/e0dbdc72ed677c34d4d2ea457cbe4584 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;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