a-conjecture-of-mine

An exercise on polyglossy: the same problem solved on multiple languages

commit 0c1f58d744e493a5a9519edd4620b53942883745
parent 115bb94a3c0fa8c8c4248b5411aec09b4490282b
Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date:   Sat,  4 May 2019 19:44:11 -0300

Merged Main.hs.

Diffstat:
Mx86/PROGRAM.ASM | 136+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
1 file changed, 95 insertions(+), 41 deletions(-)
diff --git a/x86/PROGRAM.ASM b/x86/PROGRAM.ASM
@@ -3,8 +3,9 @@
 .stack 1000h
 
 .data
-        input      db 22 DUP(0)
+        input      db 8 DUP(0)
         invalid    db "' is not a natural number!", 0dh, 0ah, 0dh, 0ah, "$"
+        too_big    db " is too big!", 0dh, 0ah, 0dh, 0ah, "$"
 
         max        dw 0
         a          dw 0
@@ -13,9 +14,11 @@
         sumA       dw 0
         sumB       dw 0
 
+        sums_cache dw 25000 DUP(0)
+
         start_time dw 0
         end_time   dw 0
-        
+
         proof      db 0dh, 0ah, "The conjecture is proved for all natural numbers smaller or equals to $"
         counter    db 0dh, 0ah, "The conjecture is disproved! Here's a counterexample: ($"
         loading    db 0dh, 0ah, "LOADING. . .$"
@@ -54,13 +57,14 @@ start:
         mov dx, offset loading
         int 21h
 
+        call cache_sums
         call iter
 
 read_uint:
         ; Load the user input into the input
         mov ax, cs
         mov ds, ax
-        mov input[0], 20 ; Configure the input length
+        mov input[0], 6 ; Configure the input length
         mov dx, offset input
         mov ah, 0ah
         int 21h
@@ -101,6 +105,9 @@ read_loop:
         pop cx
         add ax, bx
 
+        cmp ax, 12500
+        ja short input_too_big
+
         ; Jump if the counter is still smaller than the input length
         cmp cl, input[1]
         jbe short read_loop
@@ -112,39 +119,83 @@ invalid_input:
         mov ah, 2h
 
         ; Print '\''
-        mov dx, 0ah
-        int 21
+        ;mov dx, 0ah
+        ;int 21
         mov dx, 27h
         int 21
 
         mov si, offset input
-        add si, 2
-        mov cx, 1
+        call print_buffer
 
-invalid_input_loop:
-        mov dx, [si]
+        ; Print the rest of the message
+        mov ah, 9h
+        mov dx, offset invalid
         int 21h
 
-        inc cx
-        inc si
+        call quit
+        ret
 
-        cmp cl, input[1]
-        jbe short invalid_input_loop
+input_too_big:
+        mov si, offset input
+        call print_buffer
 
-invalid_input_end:
-        ; Print the rest of the message
         mov ah, 9h
-        mov dx, offset invalid
+        mov dx, offset too_big
         int 21h
 
         call quit
         ret
 
+cache_sums:
+        mov si, offset sums_cache
+
+        mov ax, max
+        mov bx, 2
+        mul bx
+        mov bx, ax
+
+        mov ax, 0
+cache_sums_loop:
+        push bx
+        push ax
+        call sum_digits
+        mov [si], bx
+
+        pop ax
+        add si, 2
+        inc ax
+
+        pop bx
+        cmp ax, bx
+        jb short cache_sums_loop
+        ret
+
+; Prints the characters of a buffer referenced by si
+print_buffer:
+        mov bh, si[1]
+
+        add si, 2
+        mov ch, 0
+
+        mov ah, 2h
+print_buffer_loop:
+        mov dl, [si]
+        int 21h
+
+        inc ch
+        inc si
+
+        cmp ch, bh
+        jb short print_buffer_loop
+        ret
+
 ; Iterate a from 0 to max
 iter:
+        mov ax, a
         call test_num
 
-        inc a
+        inc ax
+        mov a, ax
         cmp ax, max
         jbe short iter
 
@@ -157,8 +208,6 @@ test_num:
 
 test_loop:
         call test_pair
-        cmp dx, 0
-        jne disproved
 
         inc b
         mov ax, b
@@ -167,31 +216,39 @@ test_loop:
         ret        
 
 test_pair:
-        ; Calculate the a + b into cx
-        mov ax, a
-        add ax, b
-        
-        call sum_digits ; Calculate S(a + b)
-        mov sumAB, bx
+        ; Calculate S(A + B) and store it in sumAB
+        mov si, offset sums_cache
+        add si, a
+        add si, b
+        mov ax, [si]
+        mov sumAB, ax
+        mov ax, si
+
+        ; Calculate S(A) and store it in sumA
+        mov si, offset sums_cache
+        add si, a
+        mov ax, [si]
+        mov sumA, ax
+
+        ; Calculate S(B) and store it in sumB
+        mov si, offset sums_cache
+        add si, b
+        mov ax, [si]
+        mov sumB, ax 
 
-        mov ax, a ; Store the value of a in ax
-        call sum_digits ; Calculate S(a) into bx
-        mov sumA, bx
 
-        mov ax, b ; Store the value of b in ax
-        call sum_digits ; Calculate S(b) into bx
-        mov sumB, bx
-        
-        
         ; Calculate S(a + b) - S(a) - S(b) in ax
         mov ax, sumAB
         sub ax, sumA
         sub ax, sumB
-        
+
         mov cx, 9 ; Set the devident to 9
         mov dx, 0 ; Clear the register where the rest will be stored
         div cx
 
+        cmp dx, 0
+        jne disproved
+
         ret
 
 sum_digits:
@@ -229,14 +286,11 @@ proved:
         ret
 
 disproved:
-        push bx
-        push ax
-
         mov ah, 9h
         mov dx, offset counter
         int 21h
 
-        pop ax
+        mov ax, a
         call print_uint
 
         ; Print ', '
@@ -246,7 +300,7 @@ disproved:
         mov dx, ' '
         int 21h
 
-        pop ax
+        mov ax, b
         call print_uint
 
         ; Print ')\n'
@@ -316,14 +370,14 @@ get_time:
         ; Get system time
         mov ah, 2ch
         int 21h
-        
+
         ; Add the minutes
         mov ax, 0
         mov al, ch
         mov bx, 6000
         mul bx
         mov [si], ax
-        
+
         ; Add the seconds
         mov ax, 0
         mov al, dh