a-conjecture-of-mine

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

Commit
f492c2e5f176dbe9f37a92b232b7d53fc6975276
Parent
f168b19bba7365ac4baca060a983ee936b223a2a
Author
Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date

Improved the x86 implementation.

Started working on the 'read_uint' routine.

Diffstat

2 files changed, 115 insertions, 15 deletions

Status File Name N° Changes Insertions Deletions
Modified Go/conjecture.go 2 1 1
Modified x86/PROGRAM.ASM 128 114 14
diff --git a/Go/conjecture.go b/Go/conjecture.go
@@ -18,7 +18,7 @@ import (
 func main() {
 	fmt.Println("\nThis program is a simple test for the following conjecture:")
 	fmt.Println("\nLet S: N -> N be the sum of the digits of a positive integer.")
-	fmt.Println("For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger.")
+	fmt.Println("For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer.")
 	fmt.Print("\nWhat value would you like to test the conjecture for? ")
 
 	reader := bufio.NewReader(os.Stdin)
diff --git a/x86/PROGRAM.ASM b/x86/PROGRAM.ASM
@@ -1,29 +1,130 @@
 .model tiny
 
+.stack 100h
+
 .data
-        intro db "Hey", 0dh, 0ah, "$"
-        proof db "The conjecture is proved for all natural numbers smaller or equals to", 0dh, 0ah, "10000!", 0dh, 0ah, "$"
+        buffer db 20 DUP(30h)
+        invalid db " is not a natural number!", 0dh, 0ah, 0dh, 0ah, "$"
+
+        proof db "The conjecture is proved for all natural numbers smaller or equals to 10000!", 0dh, 0ah, "$"
+
+        intro1 db 0dh, 0ah, "This program is a simple test for the following conjecture:", 0dh, 0ah, "$"
+        intro2 db 0dh, 0ah, "Let S: N -> N be the sum of the digits of a positive integer.", 0dh, 0ah, "$"
+        intro3 db "For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer.", 0dh, 0ah, "$"
+        intro4 db 0dh, 0ah, "What value would you like to test the conjecture for? $"
 
 .code
-org 100h
+        org 100h
 
 start:
-        mov dx, offset intro
+        mov ax, @data
+        mov ds, ax
+
+        ; Print the intro messages to the screen
         mov ah, 9h
+        mov dx, offset intro1
+        int 21h
+        mov dx, offset intro2
         int 21h
+        mov dx, offset intro3
+        int 21h
+        mov dx, offset intro4
+        int 21h
+
+        ; Retrieve user input
+        call read_uint
+        mov bx, ax
           
         mov ax, 0
         call iter
 
+read_uint:
+        ; Load the user input into the buffer
+        mov ax, cs
+        mov ds, ax
+        mov dx, 0
+        mov buffer[0], 20 ; Configure the buffer length
+        mov dl, offset buffer
+        mov ah, 0ah
+        int 21h
+
+        mov ax, 0 ; Initialize the register were the result will be stored
+        mov cx, 2 ; Initialize a counter
+        mov si, offset buffer
+        add si, 3
+
+read_loop:
+        mov bx, 0 ; Clear bx
+        mov bl, [si] ; Read a character into bx
+        inc cx ; Increment the counter
+        inc si ; Increment the source index
+
+        ; Check if it's a numeric character
+        cmp bl, 30h
+        jb short invalid_input
+        cmp bl, 39h
+        ja short invalid_input
+
+        ; Convert the character code into a decimal digit
+        sub bl, 30h
+
+        ; ax = ax * 10 + bx
+        push cx
+        mov cx, 10
+        mul cx
+        pop cx
+        add ax, bx
+
+        ; Jump if the counter is still smaller than the buffer length
+        cmp cl, buffer[0]
+        jb short read_loop
+        cmp cl, buffer[1]
+        jb short read_loop                
+        ret
+
+invalid_input:
+        mov ah, 2
+
+        ; Print '\''
+        mov dx, 27h
+        int 21h
+
+        mov si, offset buffer
+        mov cx, 0
+
+invalid_input_loop:
+        mov dl, [si]
+        int 21h
+        inc cx
+        inc si
+
+        cmp cl, 6
+        jbe short invalid_input_loop
+
+invalid_input_end:
+        ; Print '\''
+        mov dx, 27h
+        int 21h
+
+        ; Print the rest of the message
+        mov ah, 9h
+        mov dx, offset invalid
+        int 21h
+
+        call quit
+        ret
+
+; Iterate ax from 0 to 10000
 iter:
         call test_num
 
-        add ax, 1
+        inc ax
         cmp ax, 10000
-        jle short iter
+        jbe short iter
       
         call proved
 
+; Iterate bx from 0 to ax
 test_num:
         mov bx, 0
 test_loop:
@@ -36,10 +137,10 @@ test_loop:
 
         pop ax
         pop bx
-        add bx, 1
+        inc bx
 
         cmp bx, ax
-        jle short test_loop
+        jbe short test_loop
         ret        
 
 test_pair:
@@ -86,25 +187,24 @@ sum_loop:
 
         ; Loop until ax equals 0
         cmp ax, 0
-        jg short sum_loop
+        ja short sum_loop
         ret
 
 proved:
-        ; Print the message to the screen
-        mov dx, offset proof
         mov ah, 9h
+        mov dx, offset proof
         int 21h
 
         call quit
         ret
+
 disproved:
-        mov dx, offset proof
         mov ah, 9h
+        mov dx, offset proof
         int 21h
 
         call quit
         ret        
 quit:
-        mov ah, 4ch
-        int 21h
+        int 20h
 end start