2024-06-30 15:49:30 +02:00
|
|
|
; 8086 assembly to compute e to n digits.
|
|
|
|
; build with masm 5.0 like this:
|
|
|
|
; ntvdm -h masm /Zi /Zd /z /l %1,,,;
|
|
|
|
; ntvdm -h link %1,,%1,,nul.def
|
|
|
|
; replicate this:
|
|
|
|
; #define TRUE 1
|
|
|
|
; #define FALSE 0
|
|
|
|
; #define SIZE 8190
|
|
|
|
;
|
|
|
|
; char flags[SIZE+1];
|
|
|
|
;
|
|
|
|
; int main()
|
|
|
|
; {
|
|
|
|
; int i,k;
|
|
|
|
; int prime,count,iter;
|
|
|
|
;
|
|
|
|
; for (iter = 1; iter <= 10; iter++) { /* do program 10 times */
|
|
|
|
; count = 0; /* initialize prime counter */
|
|
|
|
; for (i = 0; i <= SIZE; i++) /* set all flags true */
|
|
|
|
; flags[i] = TRUE;
|
|
|
|
; for (i = 0; i <= SIZE; i++) {
|
|
|
|
; if (flags[i]) { /* found a prime */
|
|
|
|
; prime = i + i + 3; /* twice index + 3 */
|
|
|
|
; for (k = i + prime; k <= SIZE; k += prime)
|
|
|
|
; flags[k] = FALSE; /* kill all multiples */
|
|
|
|
; count++; /* primes found */
|
|
|
|
; }
|
|
|
|
; }
|
|
|
|
; }
|
|
|
|
; printf("%d primes.\n",count); /*primes found in 10th pass */
|
|
|
|
; return 0;
|
|
|
|
; }
|
|
|
|
;
|
|
|
|
|
|
|
|
dosseg
|
|
|
|
.model small
|
|
|
|
.stack 100h
|
|
|
|
|
|
|
|
; DOS constants
|
|
|
|
|
|
|
|
dos_write_char equ 2h
|
|
|
|
dos_realtime_clock equ 1ah
|
|
|
|
dos_exit equ 4ch
|
|
|
|
|
|
|
|
; app constants
|
|
|
|
|
|
|
|
true equ 1
|
|
|
|
false equ 0
|
|
|
|
loops equ 10
|
|
|
|
arraysize equ 8190
|
|
|
|
|
2024-07-17 01:11:50 +02:00
|
|
|
tttdataseg segment para public 'sievedata'
|
|
|
|
assume ds: tttdataseg
|
2024-06-30 15:49:30 +02:00
|
|
|
|
|
|
|
crlfmsg db 13,10,0
|
|
|
|
primesmsg db ' primes.',13,10,0
|
|
|
|
count dw 0
|
|
|
|
ITER dw 0
|
|
|
|
PKFLAGS dd 0
|
|
|
|
|
|
|
|
align 16
|
|
|
|
flags db arraysize + 1 dup( ? )
|
|
|
|
afterflags db 0
|
2024-07-17 01:11:50 +02:00
|
|
|
tttdataseg ends
|
2024-06-30 15:49:30 +02:00
|
|
|
|
|
|
|
.code
|
|
|
|
|
|
|
|
start:
|
2024-07-17 01:11:50 +02:00
|
|
|
mov ax, tttdataseg
|
2024-06-30 15:49:30 +02:00
|
|
|
mov ds, ax
|
|
|
|
|
|
|
|
iteragain: ; for ( iter = 1; iter <= 10; iter++ )
|
|
|
|
mov word ptr [ count ], 0 ; count = 0
|
|
|
|
|
|
|
|
; set all array entries to true: for (i = 0; i <= SIZE; i++) flags[i] = TRUE;
|
|
|
|
|
2024-07-17 01:11:50 +02:00
|
|
|
mov ah, true
|
|
|
|
mov al, true
|
|
|
|
mov di, offset flags
|
|
|
|
mov cx, ( arraysize + 2 ) / 2 ; 4095
|
|
|
|
push ds
|
|
|
|
pop es
|
|
|
|
cld
|
|
|
|
rep stosw
|
|
|
|
stosb
|
2024-06-30 15:49:30 +02:00
|
|
|
|
|
|
|
; iterate through array entries and count primes
|
|
|
|
|
2024-07-17 01:11:50 +02:00
|
|
|
mov di, offset afterFlags ; handy place for this constant
|
|
|
|
xor cx, cx ; handy place for 0
|
2024-06-30 15:49:30 +02:00
|
|
|
xor bx, bx ; bx is "i" in the outer for loop
|
|
|
|
|
|
|
|
nextprime: ; for (i = 0; i <= SIZE; i++) {
|
|
|
|
cmp byte ptr [ flags + bx ], 0
|
|
|
|
jz flagisoff
|
|
|
|
|
|
|
|
mov ax, 3 ; ax == prime = i + i + 3
|
|
|
|
add ax, bx
|
|
|
|
add ax, bx
|
|
|
|
mov si, offset flags
|
|
|
|
add si, bx ; for (k = i + prime; k <= SIZE; k += prime)
|
|
|
|
|
|
|
|
add si, ax ; k += prime
|
2024-07-17 01:11:50 +02:00
|
|
|
cmp si, di ; is si >= offset afterFlags? (i.e. k <= size)
|
|
|
|
jge inccount ; redundant check to the one in the loop below but it saves a jump instruction in the loop
|
2024-06-30 15:49:30 +02:00
|
|
|
|
2024-07-17 01:11:50 +02:00
|
|
|
kloop:
|
2024-06-30 15:49:30 +02:00
|
|
|
mov byte ptr [ si ], cl ; flags[ k ] = false. use cl for 0 because it's faster than an immediate
|
2024-07-17 01:11:50 +02:00
|
|
|
add si, ax ; k += prime
|
|
|
|
cmp si, di ; is si >= offset afterFlags? (i.e. k <= size)
|
|
|
|
jl kloop
|
2024-06-30 15:49:30 +02:00
|
|
|
|
|
|
|
inccount:
|
|
|
|
inc word ptr [ COUNT ] ; count++
|
|
|
|
|
|
|
|
flagisoff: ; check if outer loop is done
|
|
|
|
inc bx
|
|
|
|
cmp bx, arraysize + 1
|
|
|
|
jnz nextprime
|
|
|
|
|
|
|
|
checknextiter: ; are we done iterating loops times?
|
|
|
|
inc word ptr [ ITER ]
|
|
|
|
cmp word ptr [ ITER ], loops
|
|
|
|
jnz iteragain
|
|
|
|
|
|
|
|
mov ax, word ptr [COUNT]
|
|
|
|
call printint
|
|
|
|
|
|
|
|
call printcrlf
|
|
|
|
mov dx, offset primesmsg
|
|
|
|
call printstring
|
|
|
|
|
|
|
|
mov al, 0
|
|
|
|
mov ah, dos_exit
|
|
|
|
int 21h
|
|
|
|
;start endp
|
|
|
|
|
|
|
|
; print the integer in ax
|
|
|
|
|
|
|
|
printint proc near
|
|
|
|
test ah, 80h
|
|
|
|
push ax
|
|
|
|
push bx
|
|
|
|
push cx
|
|
|
|
push dx
|
|
|
|
push di
|
|
|
|
push si
|
|
|
|
|
|
|
|
jz _prpositive
|
|
|
|
neg ax ; just one instruction for complement + 1
|
|
|
|
push ax
|
|
|
|
mov dx, '-'
|
|
|
|
mov ah, dos_write_char
|
|
|
|
int 21h
|
|
|
|
pop ax
|
|
|
|
_prpositive:
|
|
|
|
xor cx, cx
|
|
|
|
xor dx, dx
|
|
|
|
cmp ax, 0
|
|
|
|
je _przero
|
|
|
|
_prlabel1:
|
|
|
|
cmp ax, 0
|
|
|
|
je _prprint1
|
|
|
|
mov bx, 10
|
|
|
|
div bx
|
|
|
|
push dx
|
|
|
|
inc cx
|
|
|
|
xor dx, dx
|
|
|
|
jmp _prlabel1
|
|
|
|
_prprint1:
|
|
|
|
cmp cx, 0
|
|
|
|
je _prexit
|
|
|
|
pop dx
|
|
|
|
add dx, 48
|
|
|
|
mov ah, dos_write_char
|
|
|
|
int 21h
|
|
|
|
dec cx
|
|
|
|
jmp _prprint1
|
|
|
|
_przero:
|
|
|
|
mov dx, '0'
|
|
|
|
mov ah, dos_write_char
|
|
|
|
int 21h
|
|
|
|
_prexit:
|
|
|
|
pop si
|
|
|
|
pop di
|
|
|
|
pop dx
|
|
|
|
pop cx
|
|
|
|
pop bx
|
|
|
|
pop ax
|
|
|
|
ret
|
|
|
|
printint endp
|
|
|
|
|
|
|
|
printcrlf proc near
|
|
|
|
push ax
|
|
|
|
push bx
|
|
|
|
push cx
|
|
|
|
push dx
|
|
|
|
push di
|
|
|
|
push si
|
|
|
|
mov dx, offset crlfmsg
|
|
|
|
call printstring
|
|
|
|
pop si
|
|
|
|
pop di
|
|
|
|
pop dx
|
|
|
|
pop cx
|
|
|
|
pop bx
|
|
|
|
pop ax
|
|
|
|
ret
|
|
|
|
printcrlf endp
|
|
|
|
|
|
|
|
printstring proc near
|
|
|
|
push ax
|
|
|
|
push bx
|
|
|
|
push cx
|
|
|
|
push dx
|
|
|
|
push di
|
|
|
|
push si
|
|
|
|
|
|
|
|
mov di, dx
|
|
|
|
|
|
|
|
_psnext:
|
|
|
|
mov al, byte ptr [ di ]
|
|
|
|
cmp al, 0
|
|
|
|
je _psdone
|
|
|
|
mov dx, ax
|
|
|
|
mov ah, dos_write_char
|
|
|
|
int 21h
|
|
|
|
|
|
|
|
inc di
|
|
|
|
jmp _psnext
|
|
|
|
|
|
|
|
_psdone:
|
|
|
|
pop si
|
|
|
|
pop di
|
|
|
|
pop dx
|
|
|
|
pop cx
|
|
|
|
pop bx
|
|
|
|
pop ax
|
|
|
|
ret
|
|
|
|
printstring endp
|
|
|
|
|
|
|
|
end
|