I was wondering if someone could explain why it changes from 0000 to FFFF.
What will be the value in EAX after the following lines execute?
mov eax,30020000h
dec ax
The value in eax is changed to 3001FFFFh why does it flip?
I was wondering if someone could explain why it changes from 0000 to FFFF.
What will be the value in EAX after the following lines execute?
mov eax,30020000h
dec ax
The value in eax is changed to 3001FFFFh why does it flip?
The value in eax is changed to 3001FFFFh why does it flip?
The value of eax is 3002FFFFh.
The initial instruction copies 30020000h into EAX. This sets all 32 bits of EAX.
The second instruction ONLY affects the lower 16 bits of EAX, also known as AX. Any instruction with AX will affect only those bits, so you are decrementing 0000h. Your carry flag will also be set. Here are some helpful ways to think of this, I think.
1- What number would give you 0000h when you increment it? FFFFh qualifies, because if you were to add one, it would make the rightmost F into a 0, and the one would carry to the next, etc. Your answer would be 10000h, which does not fit into 16 bits, leaving your 16 bits as 0000h.
2- To subtract and this problem, you would need to borrow.
0000h
-1h
You can ALWAYS borrow in this sort of math. You just have to set the "carry" flag to indicate that you did that! Using this trick, you really have:
10000h
-1h
Now the answer is FFFFh pretty obviously, and the carry flag will be set.
3- This math is all really just modulo 2 raised to the power (register size). This means you can picture a number wheel instead of a number line. A 16 bit register like AX has 2^16 = 65536 possible combinations, which you can write 0000h, 0001h, 0002h... FFFEh, FFFFh, 0000h, etc. Just as incrementing from 0000h will go to 0001h, decrementing will go to FFFFh, etc.
4- Do the two's complement manually. When you decrement, you are adding negative one.
Start with
"1" = 0001h
Invert the bits:
FFFEh
Add one:
Now you have the two's complement:
"-1" = FFFFh
Then you add the two's complement (FFFFh) to the number you are trying to decrement (0000h), and get FFFFh.
But your question says you get 3001FFFFh. Check again: you do not. It is 3002FFFFh. You would only get 3001FFFFh if you decremented eax, not ax. In that case, you would have borrowed from the "2" to get your result.