Add with Carry Example

edited June 2016 in Development
Hi,

I have been really struggling to understand this simple example from Mastering Machine Code and I wonder if anyone can help?

I have included the op codes, and my comments as to what I think is happening:
LD DE,3385
LD HL,7BC7
LD A,L			;A = 7B
ADD A,E			;A = 7B + 33 = AE
LD L,A			;L = AE
LD A,H			;A = C7
ADC A,D			;A = C7 + 85 = 14C so A set to 4C and carry flag set
LD H,A                  ;H = 4C
LD B,H                  ;B = 4C
LD C,L                  ;C = AE
RET

Therefore BC = 4CAE

I'm obviously getting confused because I thought with load HL etc, then the H is the last number, and L is the first.

Also the carry flag is not being set in the wrong place in my calculations, and the answer should be 44,876 (Decimal) or AF4C (Hexadecimal)!

Thanks
Post edited by jonesypeter on

Comments

  • Nope.

    After
    LD DE,$3385
    LD HL,$7BC7
    

    D = $33
    E = $85
    H = $7B
    L = $C7

    HTH,
    Derek
    1985: ZX Spectrum+ 48K Interface 1 ZX81 16KB ASZMIC/SP ROM Philips 12" B/W TV Epson Dot Matrix Printer ZX Printer Now: 2021 M1 iMac 4.5K 24" screen 8 CPU cores 8GB RAM macOS 14.4.1 1TB SSD Drive Ext 1TB SSD Drive Ext 5TB USB 3.1 Hard Disk iPad R7 32GB iPadOS 17.4.1 iPhone SE R2 64GB iOS 17.4.1 Apple TV Gen 2
  • edited June 2016
    Thanks Derek,

    I thought with LD HL etc, then the number were switched around?

    In the book it says with the instruction LD HL the first byte is 21h as you would expect, the second byte is the new value of L, and the third byte is the new value of H
    Post edited by jonesypeter on
  • edited June 2016
    Only in the assembled code to facilitate faster loading of the registers.

    Turns out the good folk at Intel named the H(i)L(o) register pair for a reason at the 8080 design stage...

    :)
    Post edited by dbolli on
    1985: ZX Spectrum+ 48K Interface 1 ZX81 16KB ASZMIC/SP ROM Philips 12" B/W TV Epson Dot Matrix Printer ZX Printer Now: 2021 M1 iMac 4.5K 24" screen 8 CPU cores 8GB RAM macOS 14.4.1 1TB SSD Drive Ext 1TB SSD Drive Ext 5TB USB 3.1 Hard Disk iPad R7 32GB iPadOS 17.4.1 iPhone SE R2 64GB iOS 17.4.1 Apple TV Gen 2
    Thanked by 1jonesypeter
  • Ohhh, its just clicked.... You only swap the numbers round when you are typing in the Hex codes! Thank you so much. I will sleep well tonight! I owe you a beer!
  • promises, promises...
    1985: ZX Spectrum+ 48K Interface 1 ZX81 16KB ASZMIC/SP ROM Philips 12" B/W TV Epson Dot Matrix Printer ZX Printer Now: 2021 M1 iMac 4.5K 24" screen 8 CPU cores 8GB RAM macOS 14.4.1 1TB SSD Drive Ext 1TB SSD Drive Ext 5TB USB 3.1 Hard Disk iPad R7 32GB iPadOS 17.4.1 iPhone SE R2 64GB iOS 17.4.1 Apple TV Gen 2
Sign In or Register to comment.