8051, addition of 32-bit numbers

Discussion in 'Embedded Systems and Microcontrollers' started by PG1995, Jun 17, 2012.

  1. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Hi

    The result I'm getting on the simulator is different from the one which I get when I do the problem by hand. What's wrong? Please help me. Thank you.


    Code ( (Unknown Language)):
    1.  
    2. ;Write a program to add two 32-bit numbers stored in RAM locations.
    3.  
    4. ;Let's assume that the numbers are stored at RAM location 40-43H and 50-53H respectively.
    5.  
    6. ORG 0H
    7.         MOV 40H, #23
    8.         MOV 41H, #15
    9.         MOV 42H, #60
    10.         MOV 43H, #70
    11.  
    12.         MOV 50H, #30
    13.         MOV 51H, #40
    14.         MOV 52H, #63
    15.         MOV 53H, #77
    16.  
    17.         MOV R0, #40H    ;pointer of bank 0
    18.         MOV R1, #60H    ;result storage bank 0
    19.         MOV R2, #4    ;counter
    20.         SETB RS0
    21.         MOV R1, #50H    ;pointer bank 1
    22.         CLR RS0
    23.         CLR C
    24.  
    25.     REPEAT:    MOV A, @R0
    26.         SETB RS0
    27.         ADDC A, @R1
    28.         INC R1
    29.         CLR RS0
    30.         MOV @R1, A
    31.         INC R0
    32.         INC R1
    33.         DJNZ R2, REPEAT
    34.  
    35.         JNC EXIT
    36.         INC @R1
    37.  
    38.     EXIT:    NOP
    39.         SJMP $
    40.  
    41. END
    42.  
     
  2. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    If you look at your simulator closely, you'd find that your numbers in decimal are already converted to Hex by the assembler before doing the addition.

    70 60 15 23 dec -> 46 3c 0F 17 hex
    77 63 40 30 dec -> 4D 3F 28 1E hex

    In 8051, if you want to do BCD addition, you have to load the numbers in Hex (0-9 only) and make use of the AC flag (Auxiliary Carry Flag) to adjust any carry from bit 3 to bit 4.

    Allen
     
  3. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Hi Allen

    I'm sorry but I don't get your point. I'm not doing BCD addition. I'm aware that the assembler automatically converts the decimal numbers into hex numbers but I'm still not able to see why I'm getting wrong result. Could you please elaborate a little more? Thank you.

    Regards
    PG
     
  4. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    But 70 60 15 23 dec <> 46 3C 0F 17 hex. Check it out with a scientific calculator.

    Using a simple example here to substitute into your program:
    12 34 dec -> 41H=0C 40H=22

    Now convert 0C22 hex back to decimal again would give 3106dec and NOT 1234dec.

    And if you convert 1234 to hex using a calculator it should give you 4D2 and not C22.

    Hope the explanation is clear now.

    Allen
     
    PG1995 likes this.
  5. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
  6. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    Thank you, Allen. :)

    Let me rephrase what you have already mentioned above. When we store 12D into memory location 41H, it becomes 0CH, and storing 34D into memory location 40H would make it 22H. In short, 12 34 <=> 0C 22. But the scientific calculator doesn't give the same result; the result we get is: 1234 <=> 4D2.

    Well, why don't we get same results in both cases?! :confused: Could you please tell me the reason?

    Regards
    PG
     
  7. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    This has something to do with the number weight system...

    if you break 1234D into 12D & 34D in decimal system, it is same as saying
    12*100 + 34*1 = 1200 + 34 = 1234

    But the hex equivalent of "12" which is 0CH & "34" as 22H has the value of
    0C*100H + 22*1H = 12*256D + 34*1D = 3106

    Allen
     
    PG1995 likes this.
  8. PG1995

    Thread Starter Active Member

    Apr 15, 2011
    753
    5
    So, the conclusion of this discussion is that when doing addition problem one should should hex numbers and not decimal. Right? For example, if I had input numbers, then there wouldn't be any contradictory results. Thank you.

    Best wishes
    PG
     
  9. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    Only in multiple precision numbers that used up more than one byte in a 8-bit mpu/mcu. If you are able to enter 32 bit number on a 32 bit machine in one instruction. there should be no problem.

    Allen
     
    PG1995 likes this.
Loading...