Skip to content

Instantly share code, notes, and snippets.

@marciojrtorres
Created April 25, 2016 19:15
Show Gist options
  • Save marciojrtorres/a3c5f2057a4ed15ee9564f1a69ab65c8 to your computer and use it in GitHub Desktop.
Save marciojrtorres/a3c5f2057a4ed15ee9564f1a69ab65c8 to your computer and use it in GitHub Desktop.
AED - Trabalho 1
; Factorial ITERATIVE
JMP main
number: DB 5 ; input number
main:
MOV C, [number]
CALL factorial
HLT
;((0,1),(1,1),(2,2),(3,6),(4,24),(5,120))
factorial:
PUSH 1
PUSH 1
.loop:
POP A
POP B
MUL B
INC B
PUSH B
PUSH A
CMP B, C
JBE .loop
POP B
POP B
RET
; Factorial RECURSIVE
JMP main
number: DB 5 ; input number
main:
MOV C, [number]
CALL fact
HLT
fact:
PUSH C
CMP C, 1
JE .basecase
DEC C
CALL fact
.return:
POP B
MUL B
RET
.basecase:
MOV A, 1
JMP .return
; Fibonacci ITERATIVE
JMP main
number: DB 5 ; input number
main:
MOV B, [number]
CALL fib
HLT
; 0 1 2 3 4 5 6 7 8 9 10
; 1 1 2 3 5 8 13 21 34 55 89
fib:
PUSH 0 ; j
PUSH 1 ; i
.loop:
POP C
POP D
ADD D, C
PUSH C
PUSH D
DEC B
CMP B, 0
JNE .loop
RET
; Fibonacci RECURSIVE - NOT WORKING YET!
JMP main
number: DB 5 ; input number to fib calc
main:
MOV B, [number] ; point to input number
CALL fib
HLT
; 0 1 2 3 4 5 6 7 8 9 10
; 1 1 2 3 5 8 13 21 34 55 89
fib:
PUSH B
CMP B, 2
JBE .base
DEC B
CALL fib
DEC B
DEC B
CALL fib
.return:
POP B
ADD C, B
RET
.base:
POP C
RET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment