Low-level programming language: Difference between revisions

Content deleted Content added
Assembly language: Intel syntax is much easier to read
Line 49:
fib:
mov rax, rdi ; put the argument into %eax
test edirdi, edirdi ; is it zero?
je .return_from_fib ; yes - return 0, which is already in %eax
cmp edirdi, 2 ; is 2 greater than or equal to it?
jbe .return_1_from_fib ; yes (i.e., it's 1 or 2) - return 1
mov rcx, rdi ; no - put it in %ecxrcx, for use as a counter
mov rdx, 1 ; the previous number in the sequence, which starts out as 1
mov rsi, 1 ; the number before that, which also starts out as 1
Line 59:
lea rax, [rsi + rdx] ; put the sum of the previous two numbers into %eax
cmp rcx, 2 ; is the counter 2?
je .return_from_fib ; yes - %eaxrax contains the result
mov rsi, rdx ; make the previous number the number before the previous one
dec ecxrcx ; decrement the counter
mov rdx, rax ; make the current number the previous number
jmp .fib_loop ; keep going