Skip to content

Instantly share code, notes, and snippets.

@MahraibFatima
Created December 22, 2023 07:56
Show Gist options
  • Save MahraibFatima/f3f52278de93d4cfa790d4832bc45491 to your computer and use it in GitHub Desktop.
Save MahraibFatima/f3f52278de93d4cfa790d4832bc45491 to your computer and use it in GitHub Desktop.
calculate fabonacci series of numbers in assembly code
.data
num dw 8 ; Variable to store the number of Fibonacci sequence terms
sum dw 0 ; Variable to store the sum of Fibonacci sequence terms
res dw 0 ; Variable to store the result of the Fibonacci sequence
.code
main proc
mov ax, 0 ; Initialize first Fibonacci term to 0
mov bx, 1 ; Initialize second Fibonacci term to 1
mov cx, num ; Load the number of Fibonacci terms into cx register
fabonacci_calculate:
add sum, bx ; Add the current Fibonacci term to the sum
mov ax, bx ; Move the value of bx (current term) to ax
mov bx, sum ; Move the sum to bx to get the next Fibonacci term
loop fabonacci_calculate ; Loop until cx becomes zero, decrementing cx each time
; Store the result in the variable 'res'
mov res, ax
; Your code should end with 'ret' or 'int 20h' or similar
ret
end proc
main endp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment