Low-level programming language: Difference between revisions

Content deleted Content added
Khcaliy (talk | contribs)
m C: make title a little bit specific context
Tags: Mobile edit Mobile app edit Android app edit App section source
Line 48:
<syntaxhighlight lang="asm">
fib:
mov rax, rdi ; put theThe argument is stored in rdi, put it into rax
test rdi, rdi ; isIs itthe argument zero?
je .return_from_fib ; yesYes - return 0, which is already in rax
cmp rdi, 2 ; isCompare 2the greater than or equalargument to it?2
jbe .return_1_from_fib ; yes (i.e.,If it's 1is less than or 2)equal -to 2, return 1
mov rcx, rdi ; no -Otherwise, put it in rcx, for use as a counter
mov rdx, 1 ; theThe first previous number in the sequence, which starts out as 1, put it in rds
mov rsi, 1 ; theThe numbersecond beforeprevious that, whichnumber also starts out as 1, put it in rsi
.fib_loop:
lea rax, [rsi + rdx] ; putPut the sum of the previous two numbers into rax
cmp rcx, 2 ; isIs the counter 2?
je .return_from_fib ; yesYes - rax contains the result
mov rsi, rdx ; Otherwise, make the first previous number the number before thesecond previous onenumber
dec rcx ; decrementDecrement the counter
mov rdx, rax ; makeMake the current number the first previous number
jmp .fib_loop ; keepKeep going
.return_1_from_fib:
mov rax, 1 ; setSet the return value to 1
.return_from_fib:
ret ; returnReturn
</syntaxhighlight>