Chapter 5: Integer Sequences and Their Application in Learning

To be a programmer in any language, a person needs more than information. There must exist the motivation of something you want to make. The challenge is that when you are a beginner, it can be easy to get discouraged because you won’t be making anything big or impressive to other people at the start. All you have to work with in most programming languages is displaying text and numbers.

Later on you can learn to use third party libraries or native APIs for your operating system. However, what I have always disliked is that the internals of how they work are hidden or obfuscated so that you don’t know how they work.

But if you love math like I do, you will never have a problem testing your ability by writing small programs to print integer sequences. In this chapter, I will be sharing 3 of my favorite sequences.

If you have the ebook edition of this book, you will be able to click the links above and learn more about these sequences. Either way, I will show you the code that makes printing these sequences easy. even in Assembly Language

Fibonacci numbers

 1 org 100h
 2 main:
 3 
 4 mov word [radix],10
 5 mov word [int_width],1
 6 
 7 mov ax,0
 8 mov bx,1
 9 
10 Fibonacci:
11 
12 call putint
13 add ax,bx
14 push ax
15 mov ax,bx
16 pop bx
17 cmp ax,1000
18 jb Fibonacci
19 
20 mov ax,4C00h
21 int 21h
22 
23 include 'chastelib16.asm'

When executed, that program will output the following sequence

 1 0
 2 1
 3 1
 4 2
 5 3
 6 5
 7 8
 8 13
 9 21
10 34
11 55
12 89
13 144
14 233
15 377
16 610
17 987

Just by looking at it, hopefully you see the pattern. Each number is the sum of the previous two numbers. The loop in this program needed to swap the numbers in ax and bx each time before the next add. Technically, there are two other ways I could have achieved it. I could have used ecx as a temporary storage. I also could have used the xchg instruction which does the same thing, but the stack provided me a convenient way of doing it. We only needed to save the ax register before it was overwritten with bx, then we pop into bx the pushed ax from earlier. None of these methods is more correct than any other, but I tend to favor simplicity and therefore am limiting the type of instructions I use. I also think that using a third register or even another memory location is acceptable for something like this, but since the stack is already used for function calls and is an expected part of Assembly, there is no reason not to use it, especially when we only need to save one register.

Powers of 2

 1 org 100h
 2 main:
 3 
 4 mov word [radix],10
 5 mov word [int_width],1
 6 mov [int_newline],0
 7 
 8 mov cx,0
 9 
10 mov [array],byte 1
11 
12 powers_of_two:
13 
14 ;this section prints the digits
15 mov bx,[length]
16 array_print:
17 dec bx
18 mov ax,0
19 mov al,[array+bx]
20 call putint
21 cmp bx,0
22 jnz array_print
23 call putline
24 
25 ;this section adds the digits
26 mov dl,0
27 mov bx,0
28 array_add:
29 mov ax,0
30 mov al,[array+bx]
31 add al,al
32 add al,dl
33 mov dl,0
34 cmp al,10
35 jb less_than_ten
36 
37 sub al,10
38 mov dl,1
39 
40 less_than_ten:
41 mov [array+bx],al
42 inc bx
43 cmp bx,[length]
44 jnz array_add
45 
46 cmp dl,0
47 jz carry_is_zero
48 
49 mov [array+bx],1
50 inc [length]
51 
52 carry_is_zero:
53 
54 ;keeps track of how many times the loop has run
55 add cx,1
56 cmp cx,64
57 jna powers_of_two
58 
59 mov ax,4C00h
60 int 21h
61 
62 length dw 1
63 array db 32 dup 0
64 
65 include 'chastelib16.asm'

Prime Numbers

 1 org 100h
 2 main:
 3 
 4 mov word [radix],10
 5 mov word [int_width],1
 6 mov [int_newline],0
 7 
 8 ;the only even prime is 2
 9 mov ax,2
10 call putint
11 call putspace
12 
13 ;fill array with zeros up to length
14 mov bx,0
15 array_zero:
16 mov [array+bx],0
17 inc bx
18 cmp bx,length
19 jb array_zero
20 
21 ;start by filtering multiples of first odd prime: 3
22 mov ax,3
23 
24 primes:
25 
26 ;print this number because it is prime
27 call putint
28 call putspace
29 
30 mov bx,ax ;mov ax to bx as our array index variable
31 mov cx,ax ;mov ax to cx
32 add cx,cx ;add cx to itself
33 
34 sieve:
35 mov [array+bx],1 ;mark element as multiple of prime
36 add bx,cx ;check only multiples of prime times 2 to exclude even numbers
37 cmp bx,length
38 jb sieve
39 
40 ;check odd numbers until we find unused one not marked as multiple of prime
41 mov bx,ax
42 next_odd:
43 add bx,2
44 cmp [array+bx],0
45 jz prime_found
46 cmp bx,length
47 jb next_odd
48 prime_found:
49 
50 ;get next prime read to print in ax
51 mov ax,bx
52 cmp ax,length
53 jb primes
54 
55 mov ax,4C00h
56 int 21h
57 
58 include 'chastelib16.asm'
59 
60 length=1000
61 array rb length

How to use these examples

My suggestions is that you download the examples in this chapter from my github repository rather than trying to type them by hand or copy past them. That way you can assemble them with FASM and run them in the DOSBox emulator to see how they work.

These programs can produce long lists of numbers and so I can’t include all the output in this book. You will have to run them to get the full picture of how magnificent they are!