Request input (Z) from user, \$-65535 \leq Z \leq 65535\$.
Check if \$(X^2)-(Y^2)=Z\$ (while \$0 \leq X,Y \leq 1000\$).
I wrote a code which will receive Z from user, if it's negative, store the negative value and continue as if Z is positive. Two loops, one which will check Y from \$0 \mapsto 1000\$, after that add \$1\$ to X and continue, until both X and Y are 1000 (meaning no answer) or until an answer is found.
If Z contained a negative value, exchange X and Y after answer is found. Would love to see some reviews.
.MODEL SMALL .STACK 100h .DATA EnterNum DB 13,10,'Please enter a number between (and including) -65535 and 65535: ',13,10,'$' AnswerSTR DB 13,10,'Z = , X = , Y = ',13,10,'$' WrongSTR DB 13,10,'Wrong input, please type a number again (-65535 <= X <= 65535): ',13,10,'$' NoAnswerSTR DB 13,10,' has no solution below 1000',13,10,'$' Z DD ? X DD ? Y DD ? I DD ? J DD ? NumSign DD 1 Ten DD 10 .CODE .386 MOV AX,@DATA MOV DS,AX XOR EAX,EAX MOV CX,6 ;Initialize CX for the loop of receiving num MOV AH,9 MOV DX,OFFSET EnterNum INT 21h ;Print request num from user TypeAgain: XOR DX,DX ReceiveNum: MOV AH,1 INT 21h CMP AL,45 ;Ascii for minus JE SignSave CMP AL,13 ;Ascii for enter key JE EnterKey SUB AL,'0' ;Convert from Ascii value to numeral value (if reached this point then it's SUPPOSED to be a number) CMP AL,0 JB WrongInput CMP AL,9 JA WrongInput ;Check if input is correct (0-9) XOR AH,AH ADD Z,EAX ;Save digit in Z MOV EAX,Z MUL Ten MOV Z,EAX ;Save digit multiplied by 10 in order to make place for the new digit (if enter, will go to EnterKey label) XOR EAX,EAX LOOP ReceiveNum MOV AH,1 INT 21h SUB AL,'0' CMP AL,0 JB WrongInput CMP AL,9 JA WrongInput XOR AH,AH ADD Z,EAX ;Receive last digit in number JMP Initialize WrongInput: MOV AH,9 MOV DX,OFFSET WrongSTR INT 21h JMP TypeAgain SignSave: MOV NumSign,-1 JMP ReceiveNum EnterKey: XOR EAX,EAX MOV EAX,Z DIV Ten MOV Z,EAX ;Z has number entered by user Initialize: MOV X,0 MOV I,0 MOV J,0 JMP CheckAnswerX ;Z will have number from user, NumSign will have -1 if number is negative (remain 1 if positive) IncX: XOR EAX,EAX INC I ;To help with count of X MOV EAX,I MOV X,EAX MUL X MOV X,EAX ;X contains the value of X^2 CheckAnswerX: XOR EAX,EAX MOV Y,0 MOV J,0 CheckAnswerY: XOR EAX,EAX MOV EAX,J MOV Y,EAX MUL Y MOV Y,EAX ;Y contains value of Y^2 XOR EAX,EAX MOV EAX,X SUB EAX,Y ;EAX - Y, keep value in EAX CMP EAX,Z JNE ContinueCheck JMP Answer ;Meaning they're equal and we have the answer ContinueCheck: XOR EAX,EAX MOV EAX,I ADD EAX,J CMP EAX,2002 ;Check if X and Y made their 1001 loop separately (sum of both is 2002) JE NoAnswerStop XOR EAX,EAX MOV EAX,J CMP EAX,1001 ;Check if Y made it's 0-1000 run JE IncX ;Meaning Y needs to start from 0 again and add 1 to X INC J ;To help with count of Y JMP CheckAnswerY Answer: XOR EAX,EAX MOV EAX,I MOV X,EAX ;X contains it's original value of the answer (before the degree) XOR EAX,EAX MOV EAX,J MOV Y,EAX ;Y contains it's original value of the answer (before the degree) CMP NumSign,-1 ;If Z has a negative value JE ExchangeXY JMP Answer2 ;Else jump to Answer2 label ExchangeXY: ;Exchange Y and X values so we receive correct negative Z XOR EAX,EAX MOV EAX,X XCHG EAX,Y MOV X,EAX Answer2: ;Start inserting the values of Y, X and Z into the string XOR CX,CX MOV CX,4 XOR SI,SI MOV SI,31 MOV EAX,Y ReplaceAnswerY: DIV Ten ADD DL,'0' ;Convert from numeral value to Ascii value MOV AnswerSTR[SI],DL DEC SI XOR EDX,EDX LOOP ReplaceAnswerY XOR CX,CX MOV CX,4 XOR SI,SI MOV SI,21 MOV EAX,X ReplaceAnswerX: DIV Ten ADD DL,'0' MOV AnswerSTR[SI],DL DEC SI XOR EDX,EDX LOOP ReplaceAnswerX JMP ReplaceAnswerContinueX NoAnswerStop: JMP NoAnswer ReplaceAnswerContinueX: XOR CX,CX MOV CX,6 XOR SI,SI MOV SI,11 MOV EAX,Z ReplaceAnswerZ: DIV Ten ADD DL,'0' MOV AnswerSTR[SI],DL DEC SI XOR EDX,EDX LOOP ReplaceAnswerZ CMP NumSign,-1 JE ChangeSignString MOV AH,9 MOV DX,OFFSET AnswerSTR INT 21h JMP CodeEnd ChangeSignString: MOV AL,45 MOV AnswerSTR,AL MOV AH,9 MOV DX,OFFSET AnswerSTR INT 21h JMP CodeEnd NoAnswer: ;Insert value of Z into the string XOR CX,CX MOV CX,6 XOR SI,SI MOV SI,7 MOV EAX,Z ReplaceNoAnswerZ: DIV Ten ADD DL,'0' MOV NoAnswerSTR[SI],DL DEC SI XOR EDX,EDX LOOP ReplaceNoAnswerZ MOV AH,9 MOV DX,OFFSET NoAnswerSTR INT 21h CodeEnd: MOV AH,4Ch INT 21h END