Search code examples
assemblyx86cpu-registersatt

CPU registers individual or addressing parts


I'm sorry for this hardly understandable caption - I could not make up a better one.
I am trying to learn assembly (AT&T) and have a question about registers:
Are register-names each accessing completely different and independent memory? Or do they give access to specific smaller parts of one register and therefore address the same memory? e.g.: Is al addressing the lowest 8 bits of ax, eax & rax, so that modifying ah modifies bit 9-16 of ax, eax & rax ? Or are ah, al, ax, eax & rax all different independent registers ?
Thanks in advance
Moritz


Solution

  • Registers use some type of electronic cycles called flip-flops, a flip-flop is a cycle that stores 0 or 1 and keeps it stored. An N size register is a block of N flip-flops. The flip-flop looks like this:

    flip-flop

    • clock: is a trigger that when it activated the data is passed, so no data, 0 or 1, is stored by ascendent. It's controlled by the control unit in the CPU.
    • input: is a input for 1 bit, tiny wire, stores either 0 or 1 in the flip-flop.
    • output: is output where you can read the data.

    In a 32 bit register, 32 of these are aligned in one block with 32 bit input and 32 bit output:

    Register block

    notice, in this register there is no sub-registers, like eax, ax, ah, al. I guess Intel guys had to use 4 bit clock rather than 1 as:

    • First clock, activates all the 32 bit flip-flops, eax.
    • Second clock, activates only the lower 16 bit flip-flops, ax.
    • Third clock, activates only the second 8 bit, ah.
    • Fourth clock, activates only the first 8 bit, al.

    something like (8 FF, 8 flip-flops, and the dots means the line is connected there):

    Inside the register

    now when the processor decodes the instructions, it can tell which instruction you want and which register you target, which clock to trigger, using the opcode of the instruction:

    [b0] ff             mov    $0xff, %al
    [b4] ff             mov    $0xff, %ah
    [66 b8] ff ff       mov    $0xffff, %ax
    [b8] ff ff ff ff    mov    $0xffffffff, %eax
    

    The realty might be different, but the principle is the same. You can read more about this stuff in any logic-design or computer architecture book, but you don't need to it to start program in assembly, but it will help you to understand how stuff works.