Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

assembly - x86 Calculating AX given AH and AL?

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...