-1

I'm currently learning x86 assembly language.

I have the following code

.386
.MODEL FLAT, STDCALL
.STACK 4096
ExitProcess PROTO, dwExitCode: DWORD

.CODE
    main PROC 
    MOV AX, 7FF0h
    ADD AL, 10h

invoke ExitProcess, 0
main ENDP
END main

Before I moved 10h into AL, AX contain the value 0x7FF0

enter image description here

After moving 10h into AL, I was expecting the AX value to be 0x8000, but instead, the value in AX was 0x7F00.

enter image description here

How come adding 0x10 to 0xF0 in the right-most byte (the least significant byte) did not carry over 1 into the second right-most byte? In other words, how come adding 0x10 to 0x7FF0 did not result in 0x8000?

My guess is because when I added 0x10, I added 0x10 to AL (AL at the time of addition contain 0xF0), therefore adding 0x10 to 0xF0 result in 0x100, but the carried over 1 is not preserved.

Is my assumption correct?

halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137

3 Answers3

7

The code added 0x10 to AL, not to AX, resulting in AL = 0xF0 + 0x10 = 0x00. Change the code to add 0x10 to AX (instead of AL). You could also use | ADD AL,010H | ADC AH,000H | .

rcgldr
  • 27,407
  • 3
  • 36
  • 61
  • hi rcgldr, thanks for your answer. May I ask, is it right to say that regardless whether I'm adding to `AL, AH, AX, EAX` etc, if the result overflow, the overflow will be lost? – Thor Aug 25 '17 at 01:10
  • 1
    adding any register just affects that register (except adding 32-bit registers in 64-bit mode will clear the top 32 bits), so adding AL will only change the result in AL, the remaining bits will be unchanged – phuclv Aug 25 '17 at 01:30
  • 1
    "the overflow will be lost" If you are referring to the overflow bit of the flags register, it is not lost. It is always set based on the result of the operation. – David Wohlferd Aug 25 '17 at 02:05
2

you can do this better like this (as the other answer suggest):

MOV AX, 7FF0h
ADD AX, 0010h

but what you wanted to achieve (I assume) is this:

MOV AX, 7FF0h
ADD AL, 10h
ADC AH, 00h ; propagate carry to higher BYTE

which is the base building stone for bigint math as you can add up together any number of bytes. First is LSW add and all the others up to MSW with adc for more info see related:

Spektre
  • 49,595
  • 11
  • 110
  • 380
1

No the intel docs say that an add affects the CF so your carry out would "be preserved" there.

old_timer
  • 69,149
  • 8
  • 89
  • 168