title: Perform 2's complement in assembly programming
body: <p>I'm trying to make a calculator that performs arithmetic operations with negative numbers. This code right here is used to convert inputted numbers into ASCII values. I want to modify this program to perform 2's complement conversion if the input is negative and it should result in desired output.</p>
  
  <p>Currently this is the flow of my calculator:</p>
  
  <p>-1+2=66675 (should be 1)</p>
  
  <p>-1-1=656745 (should be -2)</p>
  
  <p>-1*-1=66757 (should be 1)</p>
  
  <p>someone advises me that i should modify this part of my code to perform what i want. I've tried my best but it's not working. can u help me with this one? oh by the way, im really new to assembly..</p>
  
  <pre><code>CONVERT_ASSCII PROC NEAR
  
      MOV AH , BYTE PTR RESULT        ;MOVE FIRST TWO BYTE OF RESULT TO AX
      MOV AL , BYTE PTR RESULT + 1
  
      ;AT FIRST CHECK IF AX IS POSITIVE OR NEGETIVE
      TEST EAX , 8000H                    ;CHECK THE LAST BIT . IF THATS 1 THEN AX IS NEG , OTHERWISE THATS POSITIVE
      MOV EDI , 0                     ;AT FIRST SET OUR FLAG TO ZERO
      JZ EAX_POSITIVE                 ;AX IS POSITIVE
      NEG EAX                         ;CALCULATE NEGETIVE OF AX 
      MOV EDI , 1                     ;DI INDICATES THAT AX IS NEG
  
  EAX_POSITIVE :
      MOV ECX , 10
      LEA ESI , ASSCII_NUM
      ADD ESI , 29                        ;MOVE TO THE LAST SPACE
  
      MOV EBP , 0                     ;THIS IS THE COUNTER OF CHARS
  
      ASSCII_LOOP : MOV EDX , 0
                    DIV ECX 
                    OR DL , 30H       ;MAKE REMINDER ASSCII
                    MOV [ESI] , DL        ;PUT ASSCII IN ASSCII_NUM
                    DEC ESI
                    INC EBP           ;ADD ONE TO THE CHAR'S COUNTER
                    CMP EAX , 0       ;IF AX &gt; 0 GOTO 
                    JA ASSCII_LOOP    ;ASSCII_LOOP
  
      CMP EDI , 0                     ;CHECK IF THAT WAS A NEGETIVE NUMBER
      JZ REST                         ;IF THATS NOT NEGETIVE GOTO REST
      MOV DL , '-'
      MOV [ESI] , DL                  ;ADD A MINES SIGN TO THE STRING
      DEC ESI
      INC EBP
  
  REST :
      LEA EDI , ASSCII_NUM
  
      ;MOVE THE ASSCII CODE TO IT'S RIGHT PLCAE IN ASSCII_NUM
      ORDER_ASSCII : INC ESI
                     MOV AL , BYTE PTR [ESI] 
                     MOV BYTE PTR [EDI] , AL
                     INC EDI
                     DEC EBP
                     CMP EBP , 0
                     JA ORDER_ASSCII
  
      MOV CL , '$'           
      MOV BYTE PTR [EDI] , CL         ;AT LAST PUT A DOLLOR SIGN AT THE END OF ASSCII_NUM
  
      RET
  CONVERT_ASSCII ENDP
  </code></pre>