0

Lets say i have a data (15H) on memory 0040.

My question is, how an I extract that most significant and least significant bit for further usage?

I have lookup on the Z80 User manual and found nothing. Any help will be appreciated

Fahmi Jabbar
  • 329
  • 4
  • 12
  • 4
    To get the LSB use bitwise `AND` with 1. To get the MSB, shift right by 7. Those are the general ways, not specific to Z80. – Jester Oct 26 '20 at 14:36
  • Appreciate the answer, but i'm really looking for the details on Z80 Assembly. – Fahmi Jabbar Oct 26 '20 at 14:40
  • 2
    Z80 does have an `AND` so that's easy. Instead of a shift right, you can use a rotate left `RLCA` to move the MSB into LSB and then `AND` as previously. Or, if you want the result in the carry flag, you don't need the `AND` and can use a rotate right `RRCA` for the LSB. There is also the `BIT` instruction for testing a bit directly via the zero flag. – Jester Oct 26 '20 at 14:42
  • I'm affraid i haven't understand that answer. So let's say i wanted to get the LSB. Does AND instruction will give me that immidiately? – Fahmi Jabbar Oct 26 '20 at 14:48
  • 1
    You have at least 3 options after you loaded the byte into `A`: 1) `AND a, #1` and then your LSB is in `A`. 2) `RRCA` and then your LSB is in `CF`. 3) `BIT 0, A` and then your bit is in `ZF` negated. – Jester Oct 26 '20 at 14:50
  • Totally Get it! Can you explain it too for the MSB ? – Fahmi Jabbar Oct 26 '20 at 14:58
  • 1
    `RLCA` and then your MSB is in `CF`. Follow by `AND a, #1` to keep it in `A`. Alternatively, use `BIT 7, A` to put it in `ZF` negated. – Jester Oct 26 '20 at 14:59
  • Fantastic thank you!, If you add this to the answer it will be more helpful for the others – Fahmi Jabbar Oct 26 '20 at 15:02
  • There are several good Z80 books available for free online. Find one or two and learn from them. If you have this question and continue with the Z80, you will doubtless have others. One by Leventhal is a masterpiece of Z80 wisdom. – TomServo Oct 26 '20 at 22:17
  • Thank you for the information, but I don't understand why the down vote? I think I already did a research before asking a question. – Fahmi Jabbar Oct 27 '20 at 00:41
  • 1
    I didn't vote down, but you didn't show what you tried from the user manual, and why you think that it did not work. – the busybee Oct 27 '20 at 07:18

1 Answers1

0

I just want to wrote down what @Jester already explained to me

how to get the LSB

  1. using AND
LD A, 1
LD B, 19H ;The data
AND B     ;LSB is at register A
  1. using RRCA
  2. using BIT

how to get the MSB

  1. using RLCA followed by AND
  2. using BIT
Fahmi Jabbar
  • 329
  • 4
  • 12
  • 1
    You don't need to `LD A, 1` separately, just `ld b, 19h` / `AND 1` . http://z80-heaven.wikidot.com/instructions-set:and shows that Z80's AND instruction can have immediate operand. – Peter Cordes Oct 28 '20 at 18:46