Topic: ZX Spectrum machine code  (Read 1173 times)

Author Message

0 Members and 1 Guest are viewing this topic.

Offline Panther

  • Administrator
  • Amiga 4000
  • ******
  • Posts: 3895
  • Kudos 33
  • Gender: Male
  • Look at the size of my.......Paws
    • View Profile
ZX Spectrum machine code
« on: June 18, 2012, 10:22:36 AM »
Been trying to get my head round machine code on the speccy and have come up with the following just not been able to try it on a proper spectrum, would this work in filling the screen with a single character ?

     ld hl,22528
     ld bc,767
     ld a,24
loop
     ld (hl),a
     inc hl
     dec bc
     djnz loop
     ret

Offline Phu

  • RCM Workshop
  • Administrator
  • Amiga 4000
  • ******
  • Posts: 2077
  • Kudos 41
  • Gender: Male
  • Pay no attention to that PCB....
    • View Profile
    • ZX Spectrum Laptop Project
Re: ZX Spectrum machine code
« Reply #1 on: June 18, 2012, 10:44:53 AM »
Not quite...

You've set hl to 22528 (16384 + 6144) the address of the attribute map, and bc with the size (32 * 24 = 768). The code is set up to fill the attribute map with 24 (11000 binary) which is magenta paper, black ink.

If you call a space a "character" then it sort of fills the screen with characters, except...

djnz only operates on the c register. Since you start at 767 (2FFh) the loop will exit after 255 characters (one char short of 8 char rows) when c (low byte) reaches zero.

An easier way is to use the LDIR instruction (block copy):

ld hl, 22528
ld de, 22529
ld bc, 767
ld a, 24
ld (hl),a
ldir
ret

ldir copies bc bytes from hl to de. By setting de to one byte above hl, and loading the memory at hl with the required byte it does a repetitive "n to n + 1" copy, thus filling the attribute map with the required byte in one instruction :)

-- Richard
8 End of File, RCM:1
PCB Assembly, Custom Power Supply and Cable Assembly for the masses: http://www.gellman.co.uk

Offline Panther

  • Administrator
  • Amiga 4000
  • ******
  • Posts: 3895
  • Kudos 33
  • Gender: Male
  • Look at the size of my.......Paws
    • View Profile
Re: ZX Spectrum machine code
« Reply #2 on: June 18, 2012, 03:58:28 PM »
This is tougher than I thought, think I need to get my studying head on !

Offline AndyRCM

  • >=))))º> GO FEED THE FISH! <º((((=<
  • Administrator
  • Amiga 4000
  • ******
  • Posts: 9103
  • Kudos 46
  • Gender: Male
  • Manic Jet Set Willy
    • View Profile
    • Retro Computer Museum
Re: ZX Spectrum machine code
« Reply #3 on: June 18, 2012, 04:25:25 PM »
Join me mate . . . I am just getting back into it too! :)

A


"Your past is your lesson. Your present is your gift. Your future is your motivation." - Mark Zucherberg

Offline Phu

  • RCM Workshop
  • Administrator
  • Amiga 4000
  • ******
  • Posts: 2077
  • Kudos 41
  • Gender: Male
  • Pay no attention to that PCB....
    • View Profile
    • ZX Spectrum Laptop Project
Re: ZX Spectrum machine code
« Reply #4 on: June 18, 2012, 08:24:21 PM »
I spent an hour with Andy going through Z80 code... every so often Andy would say "oh I get it! that's clever..."

;)

-- Richard
8 End of File, RCM:1
PCB Assembly, Custom Power Supply and Cable Assembly for the masses: http://www.gellman.co.uk

Offline Panther

  • Administrator
  • Amiga 4000
  • ******
  • Posts: 3895
  • Kudos 33
  • Gender: Male
  • Look at the size of my.......Paws
    • View Profile
Re: ZX Spectrum machine code
« Reply #5 on: June 19, 2012, 07:57:30 AM »
I'd be like that, but then within a few days I'd forget everything  :( growing old is no fun sometimes  :-[

Offline BassHead

  • Moderator
  • Vic 20
  • ******
  • Posts: 76
  • Kudos 6
  • Gender: Male
    • View Profile
Re: ZX Spectrum machine code
« Reply #6 on: June 21, 2012, 03:04:33 PM »
Panther, you might want to have a look at: The Complete Machine Code Tutor.  Runs on the Spectrum with interactive code examples. It's almost like interpreted Asm, as the examples are freely editable, but it's quite good and not letting you kill the machine while you play.  Oh, and it has a simple of monitor built in so you can see what each line of code is doing to the registers/memory space as you step through.  I'd never done z80 before, only 68000 and a little x86 and this has helped speed the transition nicely what with all the restricted use of registers and such.

There is also a version for the C64, and one or two other platforms that I forget OTTOMH. 
b(H)

Offline muguk

  • Administrator
  • Amiga 4000
  • ******
  • Posts: 3131
  • Kudos 32
  • Gender: Male
  • Mug U.K(tm)
    • View Profile
Re: ZX Spectrum machine code
« Reply #7 on: June 21, 2012, 04:28:55 PM »
I've got it for the Amstrad and I think, buried in a box somewhere, is the Spectrum version too.

If you can't find a cheap copy on eBay, let me know.
Sarcastic Git plc!  Also can be found on Atari-Forum.com, CPC Wiki forum, GBA / NDS hacking forum and a few other places.  I'm also one of the main Twitter personages for RCM :)


Offline Panther

  • Administrator
  • Amiga 4000
  • ******
  • Posts: 3895
  • Kudos 33
  • Gender: Male
  • Look at the size of my.......Paws
    • View Profile
Re: ZX Spectrum machine code
« Reply #8 on: June 22, 2012, 08:12:12 AM »
Cheers for the tips/advice peeps, I need to get a speccy set up with the divIDE upgrade Andy showed me.

What programs woukld people suggest for entering machine code, or is it just as easy to set up Read and Data statements and Poke into speccy ram using Basic ?

Also if anyone wants to post simple examples of code so I can see what's it's doing that woukld be great as well.

Offline Phu

  • RCM Workshop
  • Administrator
  • Amiga 4000
  • ******
  • Posts: 2077
  • Kudos 41
  • Gender: Male
  • Pay no attention to that PCB....
    • View Profile
    • ZX Spectrum Laptop Project
Re: ZX Spectrum machine code
« Reply #9 on: June 22, 2012, 08:28:29 AM »
You can try the READ/DATA/POKE method, but maintaining your code will be fun :)

Oh and have fun converting 16-bit values into 8-bit pairs.

Most people prefer an assembler, of which many are available for the Speccy and which you use is largely down to preference. World of Spectrum has a number of them to download.

-- Richard
8 End of File, RCM:1
PCB Assembly, Custom Power Supply and Cable Assembly for the masses: http://www.gellman.co.uk

Offline muguk

  • Administrator
  • Amiga 4000
  • ******
  • Posts: 3131
  • Kudos 32
  • Gender: Male
  • Mug U.K(tm)
    • View Profile
Re: ZX Spectrum machine code
« Reply #10 on: June 22, 2012, 10:05:13 AM »
I'd get that upgrade that Andy's been using too done to mine.

Will be easier to have the dev tools *and* be able to write back to my DivIDE.
Sarcastic Git plc!  Also can be found on Atari-Forum.com, CPC Wiki forum, GBA / NDS hacking forum and a few other places.  I'm also one of the main Twitter personages for RCM :)


Offline BassHead

  • Moderator
  • Vic 20
  • ******
  • Posts: 76
  • Kudos 6
  • Gender: Male
    • View Profile
Re: ZX Spectrum machine code
« Reply #11 on: June 22, 2012, 10:28:18 AM »
Assembler  ???  What's wrong with pencil and paper  :P

b(H)

Offline Panther

  • Administrator
  • Amiga 4000
  • ******
  • Posts: 3895
  • Kudos 33
  • Gender: Male
  • Look at the size of my.......Paws
    • View Profile
Re: ZX Spectrum machine code
« Reply #12 on: June 25, 2012, 11:28:55 AM »
After reading elsewhere that DJNZ decrements the b register and jumps if b>0 then would this have the effect that I was after initially (filling the screen with a single character '@') ? If this is incorrect and it should be 'c' then swap b and c over in the below.

          ld hl,22528
          ld c,3
loop1
          ld b,255
          ld a,64
loop2
          ld (hl),a
          inc hl
         djnz loop2
         dec c
          ld b,c
         djnz loop1
         ret

I'm not sure I understand the ldir command, what happens when the screen is full ? what stops the loop ?

Offline Panther

  • Administrator
  • Amiga 4000
  • ******
  • Posts: 3895
  • Kudos 33
  • Gender: Male
  • Look at the size of my.......Paws
    • View Profile
Re: ZX Spectrum machine code
« Reply #13 on: June 25, 2012, 12:13:57 PM »

I'm not sure I understand the ldir command, what happens when the screen is full ? what stops the loop ?

I didn't realise that the LDIR instruction automatically uses the bc register pair as a counter and continues until this reaches zero.

Offline Phu

  • RCM Workshop
  • Administrator
  • Amiga 4000
  • ******
  • Posts: 2077
  • Kudos 41
  • Gender: Male
  • Pay no attention to that PCB....
    • View Profile
    • ZX Spectrum Laptop Project
Re: ZX Spectrum machine code
« Reply #14 on: June 25, 2012, 12:48:31 PM »
After reading elsewhere that DJNZ decrements the b register and jumps if b>0 then would this have the effect that I was after initially (filling the screen with a single character '@') ? If this is incorrect and it should be 'c' then swap b and c over in the below.

          ld hl,22528
          ld c,3
loop1
          ld b,255
          ld a,64
loop2
          ld (hl),a
          inc hl
         djnz loop2
         dec c
          ld b,c
         djnz loop1
         ret

I'm not sure I understand the ldir command, what happens when the screen is full ? what stops the loop ?

djnz operates on c - hence the "c" being thought of as the "counter" register. This is carried through to the 8086 where  cx is called "counter" (ax,bx,cx,dx = Accumulator, Base, Counter, Data).

As you noted in your later post, ldir stops when bc = 0, and it decrements bc after each copy. It's best to think of it as "copies bc bytes from hl to de, going up in memory" compare with lddr which copies bc bytes from hl to de, but does so going down, i.e. the first copy is from hl + (bc -1) to de + (bc -1).

Your code won't fill the screen with characters, only attributes. The Spectrum memory map has two parts:

1) The bitmap display at 16384, consisting of 24 rows of 32 columns with 8 pixel columns and 8 pixel rows per character. A 1 bit indicates the pixel is INK, a 0 bit indicates paper.

2) The attribute map at 22528. This holds one byte for each 8 pixel by 8 pixel character cell. The rightmost 3 bits are the INK colour (0-7), the next 3 bits (to the left) are the PAPER colour (0-7), then the next bit is the BRIGHT attribute, and the leftmost bit is the FLASH attribute.

So all your code will do is make the screen brighter ;)

For reference, the screen address works as follows 16-bit address, add 16384 to result:

Bits 0-4: Text column, or leftmost 3 bits of graphics x coordinate
Bits 5-7: rightmost 3 bits of text row, or rightmost 3 bits of graphics y-coordinate
Bits 8-10: remaining 3 bits of text row, or next 3 bits of graphics y co-ordinate
Bits 11-12: remaining 2 bits of graphics y-coordinate (11 is not valid).

So you'll need to fill this with data to put characters on screen.

-- Richard
8 End of File, RCM:1
PCB Assembly, Custom Power Supply and Cable Assembly for the masses: http://www.gellman.co.uk