0

I'm having trouble understanding registers in x86 Assembly, I know that EAX is the full 32 bits, AX is the lower 16 bits, and then AH and AL the higher and lower 8 bits of AX, But I'm doing a question.

If AL=10 and AH=10 what is the value in AX?

My thinking on this is to convert 10 into binary (1010) and then take that as the higher and lower bits of AX (0000 1010 0000 1010) and then converting this to decimal (2570) am I anywhere close to the right answer here, or way off?

4
  • 3
    Yes, that's correct. You can also just do AX=256*AH+AL=2560+10=2570. PS: when in doubt you can run some code in a debugger. Commented May 15, 2016 at 21:25
  • Thanks @Jester that shortcut will come in handy :) Commented May 15, 2016 at 21:35
  • 4
    Converting to hex is mentally easier. Every byte is 2 hex digits. It's just 0x0a0a, with AH first. Commented May 16, 2016 at 1:16
  • Also related: How do AX, AH, AL map onto EAX? Commented Apr 9, 2021 at 22:10

1 Answer 1

4

As suggested by Peter Cordes, I would imagine the data as hexadecimal values:

RR RR RR RR EE EE HH LL
|           |     || ||
|           |     || AL
|           |     AH  |
|           |     |___|
|           |     AX  |
|           |_________|
|           EAX       |
|_____________________|
RAX

...where RAX is the 64-bit register which exists in x86-64.

So if you had AH = 0x12 and AL = 0x34, like this:

00 00 00 00 00 00 12 34
|           |     || ||
|           |     || AL
|           |     AH  |
|           |     |___|
|           |     AX  |
|           |_________|
|           EAX       |
|_____________________|
RAX

...then you had AX = 0x1234 and EAX = 0x00001234 etc.

Note that, as shown in this chart, AH is the only "weird" register here which is not aligned with the lower bits. The others (AL, AX, EAX, RAX for 64-bit) are just different sizes but all aligned on the right. (For example, the two bytes marked EE EE in the chart don't have a register name on their own.)


Writing AL, AH, or AX merge into the full RAX, leaving other bytes unmodified for historical reasons. (Prefer a movzx eax, byte [mem] or movzx eax, word [mem] load if you don't specifically want this merging: Why doesn't GCC use partial registers?)

Writing EAX zero-extends into RAX. (Why do x86-64 instructions on 32-bit registers zero the upper part of the full 64-bit register?)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.