Reputation: 3018
I have tried to get the fibonnacci series up to a given number. But won't print correctly. Here is my code. num is the given number.
proc getFibo
mov al,num
mov cl,0
mov bl,1
mov dl,cl
add dl,48
mov ah,02h
int 21h
getNext:
mov dl,bl
add dl,48
mov ah,02h
int 21h
add cl,bl
mov dl,cl
add dl,48
mov ah,02h
int 21h
add bl,cl
mov cl,bl
add bl,1
cmp bl,num
jl getNext
ret
endp
Someone help me please.Thanks in advance..!
Upvotes: 0
Views: 14839
Reputation: 1
Or try the following code which produces the results shown below:
my $N = 11; # The number of Fibonacci numbers to generate
Mov r13, 0; # First Fibonacci number
Mov r14, 1; # Second Fibonacci
PrintOutStringNL " i Fibonacci"; # The title of the piece
V(N => $N)->for(sub # Generate each fionacci numbr by adding tehtwo previous ones together
{my ($index, $start, $next, $end) = @_;
$index->outRightInDec(2); # Index
Mov rax, r13; PrintOutRaxRightInDecNL 12; # Fibonacci number at this index
Mov r15, r14; # Next number is the sum of the two previous ones
Add r15, r13;
Mov r13, r14; # Move up
Mov r14, r15;
});
ok Assemble eq => <<END; # Assemble and show expected output
i Fibonacci
0 0
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55
END
Upvotes: 0
Reputation: 3018
proc getFibo
mov al,f1
mov bl,f2
;mov cl,count
;cmp cl,num
;je exitFibo
mov dl,al
add dl,48
mov ah,02h
int 21h
mov cl,count
add cl,1
mov count,cl
mov dl,bl
add dl,48
mov ah,02h
int 21h
mov cl,count
add cl,1
mov count,cl
calcFibo:
mov al,f1
add al,f2
mov f1,al
mov dl,f1
add dl,48
mov ah,02h
int 21h
mov cl,count
add cl,1
mov count,cl
mov cl,count
cmp cl,num
je exitFibo
mov bl,f2
add bl,f1
mov f2,bl
mov dl,f2
add dl,48
mov ah,02h
int 21h
mov cl,count
add cl,1
mov count,cl
mov cl,count
cmp cl,num
je exitFibo
jmp calcFibo
exitFibo:
ret
endp
I have found the answer. Thank u all.
Upvotes: 0
Reputation: 173
Won't take you long to run out of register space on a 16 bit system...
I've updated my answer, it's still not perfect though
mov bp,sp
mov ax,1
mov bx,2
.again ;fibonacci bit
add ax,bx
push ax
add bx,ax
jc putnumsonstack ;finishes when the fibonacci number bigger than the register
push bx
jmp again
.putnumsonstack ;Turns hex into decimal
mov bx,A ;and puts individual decimal numbers onto stack
.again2
cmp ax,0 ;tells you the division by A is finished
jz print ;and the decimal number is ready to print
div bx ;divide a hex num by hex-A and the dx carry gets the decimal
add dl,30 ;The dx carry is what we print out, so we add hex-30
push dx ;and put it on the stack for printing
mov dx,0 ;then clear dx for the next DIV bx
jmp again2
.print ;prints out the decimal numbers
pop ax ;strips the stack back towards the next hex number
cmp ah,0 ;tells you the decimals are all printed
jnz putnumsonstack ;jumps to the next decimal numbers stackload
;PRINT OUT AL HERE
cmp sp,bp
jae finished
jmp print
.finished
Upvotes: 0
Reputation: 22318
The loop condition at the end is incorrect:
mov cl,bl # this is skipping a value in the F-series. F(i-2) == F(i-1)
add bl,1 # this is just wrong for the F-series. F(i) = F(i-1) + 1 + F(i-2)
cmp bl,num # ok - `bl` is the next value printed if < num.
jl getNext
Those first two lines should go. If your intent was to loop if <= num, use: jle
.
Since you're only printing a character, this isn't going to work correctly after: 0112358
Upvotes: 1