csnotes/337/lec/lec3.md

1.0 KiB

Lecture 3

One's & Two's Complement (in depth(or something))

In order to change recall from last lecture that we wanted to represent 3 with a single nibble like so 0b0011.

To make this into a -3 we:

  1. Flipped all the bits : value xor 0xff..

  2. Added 1 to the result of step 1

Ok, but like, why do I care? we're just multiplying things by -1 how does that matter at all?

It matters because certain types operations just suck on pretty much every general use platform.

Most assemblers accept something like neg targetValue however you can also use an exclusive or[xor targetValue, 0xFF]. Keep in mind that the immediate value should be sign-extended to reflect the proper targetValue size.

Two's complement proccess:

  1. Flips bits
  2. Add 1

Sign Flag

Set whenever we produce (any) number where the leading bit is set(1). Regardless if we're dealing with signed or unsigned data.

If we mess with some data but the sign bit remains the same then our sign flag just stays in its current value.