How does the computer program get the 2s complement codes ?

Previously, we have learned:

  • Integer (= whole number) data types used in a computer program are:

       byte:  uses the  8 bits 2s complement code   
       short: uses the 16 bits 2s complement code
       int :  uses the 32 bits 2s complement code
       long:  uses the 64 bits 2s complement code
      

 

$64,000 question:

  • How does a computer program get the 2s complement code ???

How does a program get 2s complement codes when I'm writing decimal numbers in my program ?
 

Consider this program in a high level programming language:

   int x;

   int main()
   {
      x = 15;   // 15 = 00001111 (bin) = 0F (hex)     
   }
   

Question:   how does the 15 become a (binary) 2s complement code (00001111) ???

  • Answer: the compiler translates every number you write into binary !!!

I will explain this program compilation process in the next few slides...

The program compilation process

A computer program is translated into machine code by a compiler and an assembler:

 

How does a program get 2s complement codes when I'm writing decimal numbers in my program ?

The compiler+assembler will translate all integer constants into its 2s complement representation:

How does a program get 2s complement codes when I'm writing decimal numbers in my program ?
 

Compile this demo program:

int x;
short y;

int main()
{
   x =  15; // Use -15 also

   y =  15; // # bits in representation changed !
}

DEMO:   /home/cs255001/demo/c-asm/2s-compl.c

   gcc -c  2s-compl.c

   objdump  -d  2s-compl.o 
 

See the result in next slide...

How does a program get 2s complement codes when I'm writing decimal numbers in my program ?

Source code:

int x;
short y;

int main()
{
   x =  15; // Use -15 also

   y =  15; // # bits in representation changed !
}

Hex machine code "dump" of 2s-compl.o: (the 2s compl code for 15 is highlighted in red)

0000000000000000 <main>:
   ....
   8:	c7 05 00 00 00 00 0f 	movl   $0xf,0x0(%rip)      
   f:	00 00 00 
  12:	66 c7 05 00 00 00 00 	movw   $0xf,0x0(%rip)     
  19:	0f 00 

  00 00 00 0f (Hex) = 00000000000000000000000000001111 = (int) 15
  00 0f (Hex) = 0000000000001111 = (short) 15

How does a program get 2s complement codes when I'm writing decimal numbers in my program ?

When we use negative values:

int x;
short y;

int main()
{
   x = -15; // Use -15 also

   y = -15; // # bits in representation changed !
}

Hex machine code "dump" of 2s-compl.o: (the 2s compl code for 15 is highlighted in red)

0000000000000000 <main>:
   ....
   8:	c7 05 00 00 00 00 f1 	movl   $0xf,0x0(%rip)      
   f:	ff ff ff 
  12:	66 c7 05 00 00 00 00 	movw   $0xf,0x0(%rip)     
  19:	f1 ff 

  ff ff ff f1 (Hex) = 11111111111111111111111111110001 = (int) -15
  ff f1 (Hex) = 1111111111110001 = (short) -15

Still to come....

  • Computer languages will always provide library functions/methods to print/display intger values (represented as 2s complement codes) in sign-magnitude format

  • Example: Java has the System.out.print(int) method

     (Inside computer)     System.out.print(int)    (Shown on terminal)
        00001111              ----------->           15
    
        11110001              ------------>         -15
    

  • The System.out.print(int) is quite complex and you need to know the ASCII code

    • We will soon discuss the ASCII code and study how to write the System.out.print( ) method