Machine code help pls: LDI example

edited December 2008 in Development
[deleted]
Post edited by Unknown User on

Comments

  • edited November 2008
    No, it doesn't look like a typo.

    "Load (DE) with (HL)" means copy the contents of the address pointed to by HL to the address pointed to by DE - it doesn't affect the values in the registers DE and HL. So:

    DE=9, HL=15, BC=24

    LDI copies the contents of address 15 (HL) into address 9 (DE), then increments and decrements, so:

    DE=10, HL=16, BC=23
    SkoolKit - disassemble a game today
    Pyskool - a remake of Skool Daze and Back to Skool
  • edited November 2008
    SkoolKid is correct, except for his first sentence :-)

    Yes, it is a typo. The end result of an LDI will indeed be DE=10, HL=16 - not HL=10, DE=16 - as per the reasoning you've both given.
  • edited November 2008
    * blinks *

    * laughs *

    Hey, it's still early here in Canada. :)
    SkoolKit - disassemble a game today
    Pyskool - a remake of Skool Daze and Back to Skool
  • edited December 2008
    JimTheBrit wrote: »
    My question: Won't the Found code also execute if BC=0? After all this sets Z=1 too

    Nope, a CPI instruction only sets/resets the zero flag according to the result of the comparison - the value of BC doesn't enter into it at all (which is why that bit of code does an inc c / dec c later, to force the zero flag to consider the C register at that point).

    If you like, you can think of CPI as internally doing a DEC BC instruction, which doesn't touch the zero flag either. (Or you can just shrug and accept that that's the way CPI works...)
  • edited December 2008
    JimTheBrit wrote: »
    Thanks for the replies, that clears that up!

    Here's another query though, this time regarding CPI ...

    CPI compares A with (HL), incs HL and decs BC (the counter). Here's the example given - it assumes the length of the block that's being searched is less than 255 bytes long:

    Search CPI
    JR Z, Found
    INC C
    DEC C
    JR NZ, Search
    NotFound .......
    ....
    Found ............

    So Found is jumped to if Z is set by the comparison and NotFound is executed if Z is set by BC=0 i.e. all the bytes have been checked.

    My question: Won't the Found code also execute if BC=0? After all this sets Z=1 too and will cause the JR Z, Found instruction to jump. I can't see how NotFound will be jumped to.

    Edit: The post has thrown the code segment formatting out a little :(

    By heart,
    CPI set Z if A=(HL) and sets P-vlag when BC=0

    You can do this

    CPI
    JR Z,AISHL
    JP PE,BCZERO ; could also be PO
  • edited December 2008
    Dr BEEP wrote: »
    By heart,
    CPI set Z if A=(HL) and sets P-vlag when BC=0

    You can do this

    CPI
    JR Z,AISHL
    JP PE,BCZERO ; could also be PO

    yup,
    LDI/LDD
    CPI/CPD

    are same family and set P/V flag to zero when BC==0

    JP PO, xxxx jumps if P/V is zero
    JP PE, xxxx jumps if P/V is set
Sign In or Register to comment.