Can someone help me undestand each line of code.
I used emu8086 for this program..
Any help will be appreciated!

Title Fibonacci series
; Fibonacci series
;
; Write a program that generates the first 15 integers
; of the Fibonacci series {1,2,3,5,8,...}. Beginning with 
; the third element, each number is the sum of the previous 
; two numbers. Store the numbers in an array.

.model small
.stack 100h

.data
fArray dw 15 dup(?)

.code
main  proc

      mov    ax,@data
      mov    ds,ax

; Store the first fibonacci numbers in the array...
mov bx, offset fArray
mov dx, 0001
mov [bx], dx

; Store the first fibonacci numbers in the array...
add bx, 2
mov dx, 0002
mov [bx], dx

; Store the remaining fibonacci numbers in the array...
mov cx, 13

Fibonacci:

    ; Add the last two fibonacci numbers
    mov dx, [bx-2] 
    add dx, [bx] ; The sum of the numbers is on dx

    add bx, 2 ; increment the array index

    mov [bx], dx ; Append the new fibonacci number on the array
loop Fibonacci

;return control to DOS...
      mov    ah,4Ch 
      int    21h

main  endp
end   main

I just don't get these codes.. I tried my very best to understand these codes but I think I just lack experience on this language..
Can someone please explain these codes to me?
Thanks in advance!
=)

Fibonacci:


    mov dx, [bx-2] 
    add dx, [bx] 

    add bx, 2 

    mov [bx], dx 
loop Fibonacci

Dani AI

Generated

Brief answer: the program builds a 15‑element Fibonacci array in memory. BX is used as a byte offset into the word array, DX is a temporary place to add two previous numbers, and CX counts how many more numbers to generate after the first two are stored.

Step-by-step (plain language)

  • The code sets DS so memory references to the array work, then stores the first two 16‑bit values manually.
  • Because the array was declared as words (2 bytes each), each element occupies 2 bytes; the pointer in BX is advanced by 2 to move from one element to the next.
  • Inside the loop the routine reads the two previous elements, adds them into the temporary register, advances the BX pointer to the next empty slot, and writes the sum there. The LOOP instruction decrements CX and repeats until the required count is produced. That is why CX is initialized to 13 (15 total elements minus the 2 already written).

Mapping to a high‑level view (helps see what the assembly is doing):

int f[15];
f[0] = 1;
f[1] = 2;
for (i = 2; i < 15; ++i) {
    f[i] = f[i-1] + f[i-2];
}

Troubleshooting tips (as suggested): step through the loop with the emulator debugger and watch DS:BX, CX and the two memory words being read. Inspect memory as 16‑bit words so you don’t get confused by byte offsets. Confirm DS points to your data segment before the first store, and check that BX is advanced by exactly 2 each iteration (off‑by‑2 bugs will corrupt the sequence). If the sequence looks wrong, print or dump the array after each iteration to see where values diverge.

Visualization is probably one of the best methods to comprehension. Invoke DEBUG and then step through the program one instruction at a time and just observe what's going on. OLLYBUG is even better as it lets you visualize registers memory and opcodes in a cleaner fashion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.