How Negative Numbers Are Stored in Hex

Problem:

How do we write a negative number (like -1, -2, etc.) in hex?

💡 Simple Answer:

We use something called two's complement, which means:

Instead of storing a "minus" sign, we store a big number that, when read the right way, means a negative value.

Example (8-bit system):

An 8-bit number has 8 "slots", so it can hold values from:

Now look at this:

Decimal Binary Hex
0 00000000 0x00
-1 11111111 0xFF
-2 11111110 0xFE
-3 11111101 0xFD
-128 10000000 0x80

So:

Even though 0xFF looks like 255, computers know (based on context) that in signed 8-bit, it actually means -1.

Rule of Thumb:

If you want to find the hex for a negative number:

  1. Pick how many bits (8, 16, 32…).
  2. Add that number to your negative number.

For example:

Simple Assembly Code Example:

Here’s how the CPU interprets the same value differently based on context:

; Assembly Code (x86, NASM syntax)
section .data
    a db 0xFF        ; one byte: 255 or -1 depending on context

section .text
    global _start

_start:
    movzx eax, byte [a]     ; zero-extend: treat a as unsigned
    ; EAX now = 255

    movsx ebx, byte [a]     ; sign-extend: treat a as signed
    ; EBX now = -1

    ; exit the program
    mov eax, 60             ; syscall: exit
    xor edi, edi            ; status 0
    syscall
    

Explanation:

The same byte (0xFF) gives different results depending on the instruction used.

General Purpose Registers in AMD64 and AArch64

General-purpose registers are used by the CPU to perform operations and store temporary data. Here’s a comparison of the registers in AMD64 and AArch64:

AMD64 (x86-64):

AArch64 (ARM 64-bit):

Both architectures have their own conventions and purposes for registers, but they serve similar roles in enabling efficient computation and function calls.