-3

I am trying to write the following loop into Assembly Language. This code reads a user input and outputs the Fibonacci numbers. For Example, if a user inputs 10, the program will display the first 10 numbers of the Fibonacci sequence. If you can, please help!

  while (i <= n) {
   if (i == n)
    System.out.print(num1);
   else
    System.out.print(num1 + " + ");

   int sum = num1 + num2;
   num1 = num2;
   num2 = sum;
   i++;
roziebear
  • 1
  • 2
  • 1
    To improve your question consider adding which architecture and assembler you are using, what problems you are having, and the assembly code you have so far. Also, checkout this related question: "Fibonacci sequence in assembly language" https://stackoverflow.com/q/16409574/12378826 – Kuma Jun 10 '20 at 14:23

1 Answers1

0

ENVIRONMENT

  • HLA (High Level Assembler - HLABE back end, POLINK linker) Version 2.16 build 4413 (prototype)
  • Windows 10

NOTE

  • This example below is written in HLA (High Level Assembly), this allows for the use of high level features like for loops and the input/output provided by the standard library. Therefore some of this code needs to be translated to assembly for a pure assembly solution.
  • This example is also assuming x86 or similar variant.
  • Example starts with the 1st element as 0, as in the user inputs 1, and 0 is printed.
  • This example will break after the 47th element of the Fibonacci sequence.

EXAMPLE

program Fibonacci;
#include("stdlib.hhf");

static
    Loops:   int32;
    Number1: int32:= 0;
    Number2: int32:= 1;

begin Fibonacci;
    // Prompt user for input
    stdout.put("Enter a number: ");

    // Get user input
    stdin.get(Loops);

    // Loop for the requested amount
    for (mov(0, ECX); ECX < Loops; inc(ECX)) do

        // Print the Fn element
        stdout.put(Number1, nl);

        // Calculate the Fn+2 element
        mov(Number1, EAX);
        add(EAX, Number2);

        // Store the Fn+1 element as the next Fn element
        mov(Number2, Number1);

        // Store the Fn+3 element as the Fn+2 element
        mov(EAX, Number2);
    endfor;   
end Fibonacci;

Without Static Variables

program Fibonacci;
#include("stdlib.hhf");

begin Fibonacci;
    xor(EBX, EBX); 
    mov(1, EDX);
    stdout.put("Enter a number: ");
    stdin.geti32();
    for (mov(0, ECX); ECX < EAX; inc(ECX)) do
        stdout.put((type int32 EBX), nl);
        mov(EDX, EBP);
        add(EBX, EDX);
        mov(EBP, EBX);
    endfor;   
end Fibonacci;
Kuma
  • 427
  • 5
  • 17
  • 1
    Does HLA really let you `mov` between two memory variables? What register does it use for temp storage? (`mov [mem], [mem]` is not encodeable as a single machine instruction, only stuff like `movsd` or `push [mem]` with implicit memory operands). It would probably be easier as well as more efficient to use call-preserved registers, not memory, anyway. The OP presumably had local variables, not global / static variables like you're using. – Peter Cordes Jun 10 '20 at 15:22
  • Yes, HLA supports `mov` between two memory variables. No register is used for temp storage, instead the stack is used with a `push [mem]` then `pop [mem]`. In this specific example the resulting code was: `push dword [0x4002004]` `pop dword [0x4002000]`. Good point, call-preserved registers and static variables would probably be preferred. – Kuma Jun 10 '20 at 16:12