In assembler code must be a gcd.s file
So here is the C code I have written originally for the program. My question is how do I convert it to assembly code?
//C code
#include <stdio.h> int fibonacci(int n) { if(n <= 2) { return 1; } else { return fibonacci(n-1) + fibonacci(n-2); } } int main(void) { int n; printf("Enter Fibonacci term: "); scanf("%d", &n); printf("The %dth Fibonacci number is %d\n", n, fibonacci(n)); return 0; }
Instructions
In C language. please display results
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers.
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc.
The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc. Do not use zero-based counting; fibonacci(4)is 3, not 5.
Your assignment is to write an assembler code (Fibonacci.s) that asks the user for the nth term in the Fibonacci sequence. Your program should then calculate the nth Fibonacci number and print it out.
For example, you program should produce the following outputs:
Enter Fibonacci term: 6
The 6th Fibonacci number is 8
. MODEL A
.STACK
.DATA
VL1 DB
VL2 DB
LP DB
V1 DB
V2 DB
NL DB
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV AH, 01H
INT 21H
MOV CL, AL
SUB CL, 30H
SUB CL, 2
MOV AH, 02H
MOV DL, VAL1
ADD DL, 30H
INT 2H
MOV AH, 09H
LEA DX, NL
INT 21H
MOV AH, 02H
MOV DL, VL2
ADD DL, 30H
INT 21H
MOV AH, 09H
LEA DX, NL
INT 21H
DISP:
MOV BL, VL1
ADD BL, VL2
MOV AH, 00H
MOV AL,BL
MOV LP, CL
MOV CL, 10
DIV CL
MOV CL, LP
MOV V1,AL
MOV V2, AH
MOV DL, V1
ADD DL, 30H
MOV AH, 02H
MOV AH, 02H
INT 21H
MOV DL, V2
ADD DL,30H
MOV AH, 02H
INT 21H
MOV DL, VL2
MOV VL1, DL
MOV VL2, BL
MOV AH, 09H
LEA DX, NL
INT 21H
LOOP DISP
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
Get Answers For Free
Most questions answered within 1 hours.