-1

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?

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
TeSa
  • 37
  • 1
  • 8
  • 5
    _AX_ is the lower 16-bits of the _EAX_ register. So what is in the lower 16-bits of _EAX_? 0000 . So when you do `dec ax` it won't affect the upper 16-bit of _EAX_ thus subtracting 1 from _AX_ is FFFF. The upper bits remain unaltered. I disagree with the result being 3001FFFF . I think it should be 3002FFFF. – Michael Petch Oct 09 '16 at 00:56
  • do you think maybe it decrements AX (so both AH and AL) and not just the AL portion of the register? @MichaelPetch – TeSa Oct 09 '16 at 01:00
  • Maybe you have an issue with how negative numbers work? – Michael Petch Oct 09 '16 at 01:02
  • Might be easier to ask to see if you understand a different question altogether. If _AL_ is 00h and you subtract 1, what is the value in _AL_ after? – Michael Petch Oct 09 '16 at 01:02
  • Another answer about the relation of the register is here: http://stackoverflow.com/a/37275984/3857942 – Michael Petch Oct 09 '16 at 01:11
  • @MichaelPetch I'm thinking 11h would be in AL after – TeSa Oct 09 '16 at 01:17
  • 1
    See [Understanding Carry vs. Overflow conditions/flags](http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt) for an explanation of wraparound in binary math. – Peter Cordes Oct 09 '16 at 01:18
  • 1
    Are you mixing up binary with hex? `11h` is `00010001b`. The actual result in AL would be `11111111b` after 0 - 1. – Peter Cordes Oct 09 '16 at 01:24
  • Michael Perch asked me what 00h would be after subtracting 1. I though 11h would be the answer. @PeterCordes – TeSa Oct 09 '16 at 01:36
  • 1
    That's what I'm talking about. I think you're mixing up binary with hex. Go read that http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt, which has examples in binary. Remember that 1111 in binary is F in hex. – Peter Cordes Oct 09 '16 at 01:38
  • okay, thank you. @PeterCordes – TeSa Oct 09 '16 at 01:39
  • I said _AL_ is 00h. _AL_ is an 8 bit register so that has the binary value 00000000 . The 8 bit value for 1 has these 8 bits 00000001 . So you are doing binary 00000000 - 00000001 . The answer is binary 11111111 (in hex the result is FF) – Michael Petch Oct 09 '16 at 01:40

1 Answers1

0

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.

XorMalice
  • 154
  • 3