Reading the keyboard in assembler

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
PeterJ
Site Admin
Posts: 6858
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Reading the keyboard in assembler

Post by PeterJ »

I'm trying to get to grips with reading the keyboard with assembler.

I appreciate the low byte of all the addresses for scanning the sets of keys is FE or 254.

One text I'm reading says load the BC register pair with the appropriate address such as 61438 then use an in a,(c). Then start reading the bits which is clear.

The other book I'm reading does things rather differently. First it uses LD A,0EFH then IN A,(0FEH). Is there significance in the leading zero of the hexadecimal number?

I'm struggling to understand what the second method is doing. Can anyone provide me with enlightenment?
AndyC
Dynamite Dan
Posts: 1388
Joined: Mon Nov 13, 2017 5:12 am

Re: Reading the keyboard in assembler

Post by AndyC »

You know how IN A,(C) put's the value of B on the high address pins, so that it's really doing something a bit more like IN A,(B*256 + C)? Well IN A,(x) does pretty much the same thing, except with the current value of the A register, so it's really IN A,(A * 256 + x).

Thus LD A, 0EFh; IN A(0FEh) is really doing IN A,(0EFFEh). There is some good examples of some other Z80 quirks in http://z80.info/zip/z80-documented.pdf

As to the leading 0, that's a common requirement for assemblers that use a H suffix to denote hex numbers, because otherwise the assembler might be unable to distinguish between a hex number and a text label. Since labels can't typically begin with a number, it removes any ambiguity. It's also why a lot of assemblers use a prefix like $, & or 0x for hexadecimal instead.
User avatar
PeterJ
Site Admin
Posts: 6858
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Reading the keyboard in assembler

Post by PeterJ »

Thanks [mention]AndyC[/mention] for such a great explanation. The undocumented features guide also looks interesting.
User avatar
RMartins
Manic Miner
Posts: 776
Joined: Thu Nov 16, 2017 3:26 pm

Re: Reading the keyboard in assembler

Post by RMartins »

AndyC wrote: Sat Feb 17, 2018 6:04 pm You know how IN A,(C) put's the value of B on the high address pins, so that it's really doing something a bit more like IN A,(B*256 + C)? Well IN A,(x) does pretty much the same thing, except with the current value of the A register, so it's really IN A,(A * 256 + x).
...
Very well put, and clear.
I wouldn't have explained it better myself.
Post Reply