Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SYSEXIT = 1
- SYSREAD = 3
- SYSWRITE = 4
- STDOUT = 1
- SUCCESS_CODE = 0
- MAX_BYTES = 256
- N = 55
- .bss
- .comm prev, 256 #previous element of Fibonacci sequence
- .comm curr, 256 #current element of Fibonacci sequence
- .text
- .global _start
- _start:
- mov $MAX_BYTES, %eax # memory register
- dec %eax
- movb $0, %dh # first fib number
- movb $1, %dl # second fib number
- movb %dh, prev(,%eax,1) # move numbers to memory
- movb %dl, curr(,%eax,1)
- mov $1, %ecx # start from 2 (%ecx will be incremented before 1st loop)
- jmp fibonacci
- fibonacci:
- cmp $N, %ecx # check if Nth element have been found
- je exit # exit
- inc %ecx # else increment the counter and add prev to curr
- jmp setup_add
- setup_add:
- mov $MAX_BYTES, %eax # position counter
- mov $0, %ebx # reset carry
- mov $1, %edi # carry value
- add_loop:
- sub $1, %eax
- movb prev(,%eax,1), %dh # copy previous number to %dh (buffor)
- movb curr(,%eax,1), %dl # copy current number to %dl (buffor)
- movb %dl, prev(,%eax,1)
- add %bl, %dl # add carry do destination
- mov $0, %ebx # reset carry
- cmovc %edi, %ebx # set carry if flag is set to do so
- addb %dh, %dl # add previous number to current
- cmovc %edi, %ebx # set carry if flag is set to do so
- movb %dl, curr(,%eax,1) # save results on current position
- cmp $0, %eax # exit if possition = zero
- je exit_add
- jmp add_loop # else continue to the next digit
- exit_add:
- jmp fibonacci
- exit:
- mov $SYSEXIT, %eax
- mov $0, %ebx
- int $0x80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement