0

I know that -4 is copied into the EAX register.

My doubts:

  • -4 will be converted into two's complement binary notation before copying to EAX or not?

  • If it is converted into two's complement binary notation , who does the job?

  • Is there any special opcode for denoting negative numbers?

  • What is the possible maximum negative number we can store in EAX register?

  • Is there any special opcode or instructions for signed arithmetic?

what happens when we multiply a negative and positive number in CPU?

Michael
  • 57,169
  • 9
  • 80
  • 125
  • _"what happens when we multiply a negative and positive number in CPU?"_ Did you try looking up division and multiplication [in Intel's manuals](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)? – Michael Nov 20 '17 at 11:01
  • As far as conversions go, there's no conversion going on at runtime. `mov` with an immediate operand simply copies the bits that were encoded into the instruction when you assembled and linked your code. Volume 1 of Intel's manuals states: _"Signed integers are signed binary numbers held in a byte, word, doubleword, or quadword. All operations on signed integers assume a two's complement representation. The sign bit is located in bit 7 in a byte integer, bit 15 in a word integer, bit 31 in a doubleword integer, and bit 63 in a quadword integer (see the signed integer encodings in Table 4-1)"_ – Michael Nov 20 '17 at 11:21
  • thank you @Michael –  Nov 20 '17 at 11:24
  • @Michael: you can get sign-extension at runtime with `add eax, -4`, because that will use an 8-bit immediate which is sign-extended to 32-bit. (`mov` doesn't have any encodings with immediates narrower than the operand size, except in x86-64 with `mov r/m64, imm32` (e.g. `mov rax, -4`) which will use an imm32 sign-extended to 64-bit at runtime.) – Peter Cordes Nov 20 '17 at 21:15
  • related: https://stackoverflow.com/questions/27677906/assembly-x86-registers-signed-or-unsigned. and [how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?](https://stackoverflow.com/questions/19464202/how-does-c-compiler-handle-unsigned-and-signed-integer-why-the-assembly-code-fo) – Peter Cordes Nov 20 '17 at 23:05

1 Answers1

1

The conversion to two's complement binary is done by the assembler. The machine code emitted by the assembler contains the data in binary form.

Some opcodes interpret data as unsigned values, some work on signed values, and some opcodes are the same for both. There are no opcodes that are specifically for negative numbers.

The range of eax when interpreted as a signed number is is -2^31 .. 2^31-1.

Assuming the use of a signed multiply instruction, and no overflow, multiplying a negative number by a positive number gives the correct negative result as a two's complement value.

prl
  • 11,716
  • 2
  • 13
  • 31
  • in c we declare signed and unsigned integer to specify the sign, how to specify signed and unsigned integer in x86 assembly? –  Nov 20 '17 at 16:39
  • You don't indicate the type with the data; the data type is controlled by the opcodes used to perform operations on it. You can perform both a signed multiply and an unsigned multiply with the same data by using different opcodes. – prl Nov 20 '17 at 16:42
  • so the data is binary only, but different opcodes see the binary differently. am i right? –  Nov 20 '17 at 16:47
  • if a declare a variable with negative value , how the add instruction see ti? –  Nov 20 '17 at 16:50
  • @Naveenprakash: Addition and subtraction of two's complement numbers do not care if the numbers should be interpreted as signed or unsigned, because the operation works for both. – Michael Nov 20 '17 at 20:46
  • @Naveenprakash: [Understanding Carry vs. Overflow conditions/flags for signed vs. unsigned](http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt) explains that add/sub are the same operation, and set flags both ways. Afterwards you can look at whichever flags are relevant. Also related: [Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted?](https://stackoverflow.com/questions/34377711/which-2s-complement-integer-operations-can-be-used-without-zeroing-high-bits-in) – Peter Cordes Nov 20 '17 at 21:19