Fatorial (0 ao 9)

Thiago_ · May 04, 2021 · Assembly
section .data
    msg0 db "0! = 1 "
    msg1 db "1! = 1 "
    msg2 db "2! = 2 "
    msg3 db "3! = 6 "     
    msg4 db "4! = 24 "
    msg5 db "5! = 120 "
    msg6 db "6! = 720 "
    msg7 db "7! = 5.040 "
    msg8 db "8! = 40.320 "
    msg9 db "9! = 362.880 "
    
section .bss
    value resb 3

section .text
    global _start

_start:
    call getValue
    call validador0
    call validador1
    call validador2
    call validador3
    call validador4
    call validador5
    call validador6
    call validador7
    call validador8
    call validador9
    
    mov rax, 60
    mov rdi, 0
    syscall

getValue:
    mov rax, 0
    mov rdi, 0
    mov rsi, value
    mov rdx, 3
    syscall
    mov rax, [value]
    sub rax, '0'
    mov [value], rax
    ret

validador0:
    mov ax, [value]
    cmp ax, 0
    je printMsg0
    syscall
    ret
printMsg0:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg0
    mov rdx, 6
    syscall
    ret
        
validador1:
    mov ax, [value]
    cmp ax, 1
    je printMsg1
    syscall
    ret
printMsg1:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg1
    mov rdx, 6
    syscall
    ret
    
validador2:
    mov ax, [value]
    cmp ax, 2
    je printMsg2
    syscall
    ret
printMsg2:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg2
    mov rdx, 6
    syscall
    ret
    
validador3:
    mov ax, [value]
    cmp ax, 3
    je printMsg3
    syscall
    ret
printMsg3:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg3
    mov rdx, 6
    syscall
    ret
    
validador4:
    mov ax, [value]
    cmp ax, 4
    je printMsg4
    syscall
    ret
printMsg4:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg4
    mov rdx, 7
    syscall
    ret
    
validador5:
    mov ax, [value]
    cmp ax, 5
    je printMsg5
    syscall
    ret
printMsg5:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg5
    mov rdx, 8
    syscall
    ret
    
validador6:
    mov ax, [value]
    cmp ax, 6
    je printMsg6
    syscall
    ret
printMsg6:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg6
    mov rdx, 8
    syscall
    ret
    
validador7:
    mov ax, [value]
    cmp ax, 7
    je printMsg7
    syscall
    ret
printMsg7:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg7
    mov rdx, 10
    syscall
    ret
    
validador8:
    mov ax, [value]
    cmp ax, 8
    je printMsg8
    syscall
    ret
printMsg8:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg8
    mov rdx, 11
    syscall
    ret
    
validador9:
    mov ax, [value]
    cmp ax, 9
    je printMsg9
    syscall
    ret
    
printMsg9:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg9
    mov rdx, 12
    syscall
    ret

Comments

Please sign up or log in to contribute to the discussion.