- Joined
- Feb 11, 2011
- Messages
- 1,860
Hi guys,
I hope someone here knows assembler language. I have to do this assignment for university which has to display the first few terms of the Fibonacci Sequence (3 to 14 terms). I can't seem to get it right. It displays the correct number of terms, but their values are really strange.
Please note that I am very new to this and we don't get taught anything - we just have to figure it out ourselves.
Hopefully someone can help!
Thanks,
Mr_Bean
I hope someone here knows assembler language. I have to do this assignment for university which has to display the first few terms of the Fibonacci Sequence (3 to 14 terms). I can't seem to get it right. It displays the correct number of terms, but their values are really strange.
Code:
.model small
.stack
.data
msg_num db 10, 13, "Enter how many Fibonacci numbers must be displayed: $"
msg_oper db 10, 13, "Enter which operator must be used: $"
msg_inv db 10, 10, "Number must be between 03 and 14 (inclusive)!"
crlf db 10, 13, "$"
char1 db ?
char2 db ?
num db ?
cur db '2'
fib1 db '0'
fib2 db '1'
ans db ?
.code
start:
;****************************************;
; Load data. *;
;****************************************;
mov ax, @data
mov ds, ax
;****************************************;
; Prompts for reading in number. *;
;****************************************;
@GET_INPUT:
mov ah, 09
mov dx, offset msg_num
int 21h
mov ah, 01
int 21h
mov char1, al
mov ah, 01
int 21h
mov char2, al
cmp char1, '0'
je @SINGLE_DIGIT
cmp char1, '1'
je @DOUBLE_DIGIT
jmp @INVALID
@SINGLE_DIGIT:
cmp char2, '3'
jl @INVALID
mov al, char2
mov num, al
jmp @PROCEED
@DOUBLE_DIGIT:
cmp char2, '4'
jg @INVALID
add char2, 10
mov al, char2
mov num, al
jmp @PROCEED
@INVALID:
mov ah, 09
mov dx, offset msg_inv
int 21h
jmp @GET_INPUT
@PROCEED:
;=== Prints first 2 Fibonacci numbers ===;
mov ah, 09
mov dx, offset crlf
int 21h
mov ah, 02
mov dl, fib1
int 21h
mov ah, 09
mov dx, offset crlf
int 21h
mov ah, 02
mov dl, fib2
int 21h
@WHILE:
mov al, num
cmp cur, al
jl @ADD
jmp @EXIT
@ADD:
; ans = fib1 + fib2
mov al, fib2
add al, fib1
mov ans, al
; fib1 = fib2
mov al, fib2
mov fib1, al
; fib2 = ans
mov al, ans
mov fib2, al
add cur, 1
; new line:
mov ah, 09
mov dx, offset crlf
int 21h
; print answer:
mov ah, 02
sub ans, 48
mov dl, ans
int 21h
jmp @WHILE
;****************************************;
; Exits the program. *;
;****************************************;
@EXIT:
mov ax, 4c00h
int 21h
end start
Hopefully someone can help!
Thanks,
Mr_Bean