Previously, we have learned:
|
$64,000 question:
|
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) ???
|
I will explain this program compilation process in the next few slides...
A computer program is translated into machine code by a compiler and an assembler:
The compiler+assembler will translate all integer constants into its 2s complement representation:
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...
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 |
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 |
|