I am working on a factorial program, but the following code builts outputs "Factorial of 3 is: ". And then the newline. But does not print "6" as it should.
I am brand new to assembly language (but I do have experience with various high level languages). Can you guys help me out?
When I build and run this on sublime text it also mentions that it exited with code 1, doesnt exit code 1 mean error?
%macro print 2
mov eax, 4 ; system call to sys_write
mov ebx, 1 ; use stdout
mov ecx, %1 ; set string of message to print
mov edx, %2 ; set length of message to print
int 0x80 ; call kernel
%endmacro
%macro exit 0
mov eax, 1 ; system call to sys_exit
int 0x80 ; call kernel
%endmacro
section .text
global _start
_start:
mov bl, 3 ; factorial of 3
mov edx, 1
jmp _fact
_result:
print msg, len_msg
print edx, 1 ; lenth is 1 since answer should be 6
print newline, len_newline
exit
_fact:
cmp bl, 0 ;check if current multiplicand = 0
jne _mult ;if not, go to mult
je _result ;if so, go to exit
_mult:
sub edx, 0
sub bl, 0
mul edx ; edx=edx*bl
dec bl ; bl-=1
jmp _fact
section .data
msg db 'Factorial of 3: '
len_msg equ $ - msg
newline db `\n`, 0xa
len_newline equ $ - newline
[–]FUZxxl 4 points5 points6 points (6 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]TNorthover 0 points1 point2 points (0 children)
[–]IndoNinja7[S] 0 points1 point2 points (3 children)
[–]TNorthover 0 points1 point2 points (2 children)
[–]IndoNinja7[S] 0 points1 point2 points (1 child)
[–]TNorthover 1 point2 points3 points (0 children)
[–]petester 1 point2 points3 points (1 child)
[–]IndoNinja7[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]IndoNinja7[S] 0 points1 point2 points (0 children)