Hi there,

I'm looking over a small program by M. Abrash which animates a bitmap across the screen.

New locations for the bitmap are calculated by adding the direction variable ( 1 or -1 ) to the current x position of the bitmap.

The direction is changed with the following code -

neg

where ImageXDirection is defined by -

ImageXDirection dw 1 ;in bytes

The x position is changed with the following code -

add ax,

mov ,ax

Obviously, if ImageXDirection is negative the x value will decrease and the bitmap will move to the left and if ImageXDirection is positive, the x value will increase and the bitmap will move to the right.

The question is, how does the computer know whether ImageXDirection is positive or negative? How does it distinguish between -1 and a really big positive number?

Thanks,

S

I'm looking over a small program by M. Abrash which animates a bitmap across the screen.

New locations for the bitmap are calculated by adding the direction variable ( 1 or -1 ) to the current x position of the bitmap.

The direction is changed with the following code -

neg

where ImageXDirection is defined by -

ImageXDirection dw 1 ;in bytes

The x position is changed with the following code -

add ax,

mov ,ax

Obviously, if ImageXDirection is negative the x value will decrease and the bitmap will move to the left and if ImageXDirection is positive, the x value will increase and the bitmap will move to the right.

The question is, how does the computer know whether ImageXDirection is positive or negative? How does it distinguish between -1 and a really big positive number?

Thanks,

S

Hmm! Interesting question!!!

The last byte of any data-type is the sign bit. In this DWORD data type the 31st byte is the sign bit.

how does the computer know whether ImageXDirection is positive or negative?

I don't have a technical explanation but I think a program has to base a point on screen as the **base point**. For example: Base Point(5,5) anything subtracted on the x value will go left likewise anything subtracted to the y value will go up...How does it distinguish between -1 and a really bit positive number?

The last byte of any data-type is the sign bit. In this DWORD data type the 31st byte is the sign bit.

```
+/-
```

[31][30][29]...[0] -> EAX register

Basically a negative number has the most semnificative bit set (ie the left most bit) this is posssible becouse some convention named "1's complement" or "2's complement".

adding and substracting naturally deal with negative numbers by default as the same number can be seen as a very big unsigned value or a smaller signed value... its up to you to decide how you will use it in your programms, however there are flags and instructions that will assist you in this operations. Division and multiplications are a little more complicated so you get diffrent instructions/opcodes: DIV/IDIV and MUL/IMUL

The AoA i guess explains this in great detail

adding and substracting naturally deal with negative numbers by default as the same number can be seen as a very big unsigned value or a smaller signed value... its up to you to decide how you will use it in your programms, however there are flags and instructions that will assist you in this operations. Division and multiplications are a little more complicated so you get diffrent instructions/opcodes: DIV/IDIV and MUL/IMUL

The AoA i guess explains this in great detail

First off, if it's a 8bit signed number (easier to spell out than a 32bit), anything over 127 is negative. So the computer doesn't tell the difference. It just knows that it's negative. 1's complement is just a not operation 2's (a neg operation) is done by not'ing and adding one (or sub 1, and then not'ing). The purpose of 2's complement is that you can have -1 (0b11111111) and add 5 to get 4 (since in this example we're working with 8bit numbers, we get an overflow that results in 00000100).

Another way to look at, is that negitive one is the number you add to one to get zero. So, for 8-bit numbers we have:

```
0000 0001 ; one
```

+ 1111 1111 ; negitive one

------------

0000 0000 ; zero

This concept extrapolates to the rest of the negitive numbers as well.```
0111 1111 ; 127
```

+ 1000 0001 ; -127

------------

0000 0000 ; zero

Hi,

I think I get it.

However, its seems quite uncanny that the sums come out correct regardless of whether one considers a given value to be signed or unsigned.

S.

I think I get it.

However, its seems quite uncanny that the sums come out correct regardless of whether one considers a given value to be signed or unsigned.

S.

However, its seems quite uncanny that the sums come out correct regardless of whether one considers a given value to be signed or unsigned.

A number is signed or unsigned depending on how you treat it. If you say it's a singed number it is a signed number. The processor doesn't care as add and substract operations are the same for signed and unsigned values (that's the way the 2-complement system is designed).

Multiplication and division however require different methods, that's why there's a DIV an an IDIV, MUL and IMUL..

Thomas