a-conjecture-of-mine

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

commit c17981170b18f9bb0070411fb99a27fbc2eb9967
parent 9ea168c26072a88a492c8c46055dcdfddd054888
Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date:   Sun, 21 Apr 2019 07:47:15 -0300

Merge pull request #2 from Androthi/master

Integrated a 32 bit assembly HLA version into the repo.

Diffstat:
Ahla/conjecture.hla | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mx86/PROGRAM.ASM | 20++++++++++----------
2 files changed, 126 insertions(+), 10 deletions(-)
diff --git a/hla/conjecture.hla b/hla/conjecture.hla
@@ -0,0 +1,116 @@
+program conjecture;
+
+	#includeonce( "stdlib.hhf" )
+	
+	?@nodisplay := true;
+	?@noalignstack := true;
+
+// This program is a simple test for the following conjecture:
+
+// Let S: N -> N be the sum of the digits of a positive integer.
+// For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger.
+
+// data segments
+storage
+	max			:uns32;
+	sumAB		:uns32;
+	sumA		:uns32;
+	sumB		:uns32;
+	
+static
+	err			:boolean := false;
+	
+
+// input eax = number to sum
+// returns sum in ebx
+procedure sum_digits;
+
+	// don't build a stack frame for this procedure
+	@nodisplay; @noframe;
+
+begin sum_digits;
+	
+	mov( 10, ecx );
+	xor( ebx, ebx );
+	while( eax ) do
+		xor( edx, edx );
+		div( ecx );
+		add( edx, ebx );
+	endwhile;
+	ret;
+
+end sum_digits;
+
+
+
+begin conjecture;
+
+	// read an unsigned integer from the user
+	forever
+		try
+			stdout.put ("enter an unsigned integer :>" );
+			stdin.getu32();
+			mov( eax, max );
+			break;
+		anyexception;
+			stdout.newln();
+			continue;
+		endtry;
+	endfor;
+	
+	
+	stdout.put ("Loading ..." nl );
+	
+	for( xor(esi, esi); esi <= max; inc( esi ) ) do
+		
+		// outer loop esi = 0 to max iterations
+		
+		for( mov(esi, edi); edi <= max; inc( edi ) ) do
+			
+			// inner loop. edi = esi to max iterations
+			
+			// get S(a+b)
+			mov( esi, eax );
+			add( edi, eax );
+			call sum_digits;
+			mov( ebx, sumAB );
+			
+			// get S(a)
+			mov( esi, eax );
+			call sum_digits;
+			mov( ebx, sumA );
+			
+			// get S(b)
+			mov( edi, eax );
+			call sum_digits;
+			mov( ebx, sumB );
+			
+			// get S(a+b) - S(a) - S(b)
+			mov( sumAB, eax );
+			sub( sumA, eax );
+			sub( sumB, eax );
+			
+			// sign extend into edx
+			cdq();
+			mov( 9, ecx );
+			idiv( ecx );	// signed division of edx:eax
+			test( edx, edx ); // is remainder zero?
+			if( @nz ) then
+				// if remainder is not zero, the conjecture is false
+				stdout.put ("Conjecture is disproved, here is the counter example :", (type uns32 eax), nl );
+				mov( true, err );
+				break;
+			endif;
+		endfor;
+		
+		breakif( err );
+		
+	endfor;
+	
+	// if we get here, we looped through all the values and the
+	// conjecture is proved.
+	if( !err ) then
+		stdout.put ("The conjecture is proved for all natural numbers smaller or equals to ", max, "!", nl );
+	endif;
+
+end conjecture;
diff --git a/x86/PROGRAM.ASM b/x86/PROGRAM.ASM
@@ -3,7 +3,7 @@
 .stack 1000h
 
 .data
-        buffer  db 22 DUP(0)
+        input   db 22 DUP(0)
         invalid db "' is not a natural number!", 0dh, 0ah, 0dh, 0ah, "$"
 
         max     dw 0
@@ -44,11 +44,11 @@ start:
         call iter
 
 read_uint:
-        ; Load the user input into the buffer
+        ; Load the user input into the input
         mov ax, cs
         mov ds, ax
-        mov buffer[0], 20 ; Configure the buffer length
-        mov dx, offset buffer
+        mov input[0], 20 ; Configure the input length
+        mov dx, offset input
         mov ah, 0ah
         int 21h
 
@@ -59,8 +59,8 @@ read_uint:
         mov dx, 0ah
         int 21h
 
-        ; Move si to the to adress of the first character in the buffer
-        mov si, offset buffer
+        ; Move si to the to adress of the first character in the input
+        mov si, offset input
         add si, 2
 
         mov ax, 0 ; Initialize the register where the result will be stored
@@ -88,8 +88,8 @@ read_loop:
         pop cx
         add ax, bx
 
-        ; Jump if the counter is still smaller than the buffer length
-        cmp cl, buffer[1]
+        ; Jump if the counter is still smaller than the input length
+        cmp cl, input[1]
         jbe short read_loop
 
         mov max, ax
@@ -104,7 +104,7 @@ invalid_input:
         mov dx, 27h
         int 21
 
-        mov si, offset buffer
+        mov si, offset input
         add si, 2
         mov cx, 1
 
@@ -115,7 +115,7 @@ invalid_input_loop:
         inc cx
         inc si
 
-        cmp cl, buffer[1]
+        cmp cl, input[1]
         jbe short invalid_input_loop
 
 invalid_input_end: