0

The well-known Fibonacci number series, reputedly discovered by Leonardo of Pisa around the year 1200, has been valued for centuries for its universal qualities by artists, mathematicians, and composers. Each number in the series after the number 1 is the sum of the two previous numbers:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55 . . .

Write a program that generates and displays the first 24 numbers in the Fibonacci series, beginning with 1 and ending with 46,368.

This is my code. But the screen just shows blank space but not the integer. Can somebody help me to fix that?

title Assignment 1 (Assignment.asm)

INCLUDELIB irvine.lib
.model small
.stack 100h

.data
num dw 1

.code 
extrn writeint:proc
main proc 
    mov ax,1
start:
    add ax,num
    mov bx,10
    call writeInt
    xchg ax,num
    loop start
main endp
end main 
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Meiyi Zheng
  • 9
  • 2
  • 4
  • Was 16bit assembler case sensitive? writeInt vs writeint could be a problem. And you might want to revisit how the `loop` instruction works. – David Wohlferd Sep 18 '17 at 00:59
  • The next Fibonacci number after `46,368` is above 65535, so you could break out of the loop after `add` produces a carry-out (i.e. set up your loop so `add` / `jnc` is at the bottom). You don't need the `loop` instruction, it's just one of x86's weird complicated and obsolete instructions you don't need to learn right away. (It's kind of like `dec cx` / jnz`). – Peter Cordes Sep 18 '17 at 02:04
  • So what happens when you single-step your code in a debugger? Is it stuck in a loop? Does writeInt work when you don't have a loop? See [mcve] for tips on describing problems accurately. – Peter Cordes Sep 18 '17 at 02:06
  • @PeterCordes I just try simple code in the dosbox. like"mov ax, 5 mov bx,10 call WriteInt " I can compile it, and it shows no error. But the screen shows nothing. So I wonder maybe something wrong about the Writeint. – Meiyi Zheng Sep 18 '17 at 02:16
  • Try some examples from irvine/whoever gave you this runtime environment for the task? If they work as expected. But I'm a bit confused, I think irvine.lib doesn't exist for DOS 16b real mode (or maybe some old version, which is not mentioned on Irvine's web), are you sure you have one supposed to work in DOS? [Here](http://kipirvine.com/asm/examples/index.htm) are only windows examples. Anyway, read+use [this](http://kipirvine.com/asm/debug/index.htm) (and here is DOS mentioned, so it makes sense) – Ped7g Sep 18 '17 at 07:02

1 Answers1

0

A late answer, but I can't see such an easy unanswered question :-)

Your IRVINE.LIB is not the original IRVINE16.LIB by Kip Irvine. Someone modified IRVINE16.asm and maybe FLOATIO.ASM and recompiled it. So, I can't link your code and can't test WriteInt. You can download the whole stuff from Irvine's homepage: http://www.kipirvine.com/asm/gettingStartedVS2017/Irvine.zip

I see three mistakes:

At the beginning of the main procedure you have to initialize DS:

mov ax, @data
mov ds, ax

LOOP loops CX times. So, initialize CX first:

    ...
    mov ax,1
    mov cx, 24
start:
    add ax,num
    ...
    loop start
    ...

At the end of the procedure you have to quit the program:

mov ax, 4C00h
int 21h
rkhb
  • 14,159
  • 7
  • 32
  • 60