Author Topic: Help with a strange division...  (Read 660 times)

0 Members and 1 Guest are viewing this topic.

Offline Joseman

  • 6128 Plus
  • ******
  • Posts: 780
  • Country: es
  • Liked: 628
  • Likes Given: 429
Help with a strange division...
« on: 17:53, 28 September 16 »
Hi pals

i'm learning something about sprites but i have one question about a strange division...

'ld b,mask_table / 256'

Why in the hell with this instruction b change from low byte of a memory direction to the high byte?

is something peculiar with the Z80 dealing with a "strange" division?  something about the compiler?

thankyou!!



Offline Prodatron

  • 6128 Plus
  • ******
  • Posts: 830
  • Country: de
  • Back on the Z80
    • index.php?action=treasury
    • SymbOS SYmbiosis Multitasking Based Operating System
  • Liked: 1059
  • Likes Given: 554
Re: Help with a strange division...
« Reply #1 on: 17:57, 28 September 16 »
The mask_table is probably 256byte aligned (starting at address #xx00). So B is loaded with the highbyte of its address. Now you can access all 256 entries of mask_table with BC just by changing the C register (0-255).

GRAPHICAL Z80 MULTITASKING OPERATING SYSTEM

Offline PulkoMandy

  • 464 Plus
  • *****
  • Posts: 337
  • Country: fr
  • Liked: 268
  • Likes Given: 1
Re: Help with a strange division...
« Reply #2 on: 18:27, 28 September 16 »
And it's not the z80 doing the division, it's done by the assembler while compiling (the address must be known/constant for this to work).

Offline Joseman

  • 6128 Plus
  • ******
  • Posts: 780
  • Country: es
  • Liked: 628
  • Likes Given: 429
Re: Help with a strange division...
« Reply #3 on: 19:55, 28 September 16 »
The mask_table is probably 256byte aligned (starting at address #xx00). So B is loaded with the highbyte of its address. Now you can access all 256 entries of mask_table with BC just by changing the C register (0-255).

And it's not the z80 doing the division, it's done by the assembler while compiling (the address must be known/constant for this to work).

Yes Yes, i know that, i'm learning mask sprites, and at the moment i understand all the code.

Example of my question:

if masktable is in #0200

and we do ld b,masktable, b takes de #00 value, low byte of the address

but why if we do: 'ld b,masktable / 256' ; b takes de #02 value, highbyte of the address!

I see no mathematical / logical reason in divide masktable by 256 and reg b obtaining the high byte of the address!
« Last Edit: 19:57, 28 September 16 by Joseman »

Offline Prodatron

  • 6128 Plus
  • ******
  • Posts: 830
  • Country: de
  • Back on the Z80
    • index.php?action=treasury
    • SymbOS SYmbiosis Multitasking Based Operating System
  • Liked: 1059
  • Likes Given: 554
Re: Help with a strange division...
« Reply #4 on: 19:59, 28 September 16 »
It's easy:
#0200 / #100 = 2
in decimal:
512 / 256 = 2

Maybe you mixed hexadecimal and decimal calculation (#0200 is 512 in decimal)?

Dividing any 16bit value by 256 always returns the high byte.

GRAPHICAL Z80 MULTITASKING OPERATING SYSTEM

Offline andycadley

  • Supporter
  • 6128 Plus
  • *
  • Posts: 848
  • Liked: 404
  • Likes Given: 71
Re: Help with a strange division...
« Reply #5 on: 20:58, 28 September 16 »

I think what may actually be confusing you is ld b,masktable


See, B is an 8-bit register and masktable is a 16-bit value, so it doesn't fit. Now some assemblers will actually error when you do this, but many will instead just assign the low byte of masktable. Dividing the value by 256 gives you the high byte of masktable, which is what the code wants to put in B.

Offline IanS

  • CPC6128
  • ****
  • Posts: 195
  • Country: gb
    • index.php?action=treasury
  • Liked: 50
  • Likes Given: 42
Re: Help with a strange division...
« Reply #6 on: 21:52, 28 September 16 »
Yes Yes, i know that, i'm learning mask sprites, and at the moment i understand all the code.

Example of my question:

if masktable is in #0200

and we do ld b,masktable, b takes de #00 value, low byte of the address

but why if we do: 'ld b,masktable / 256' ; b takes de #02 value, highbyte of the address!

I see no mathematical / logical reason in divide masktable by 256 and reg b obtaining the high byte of the address!
Are you confused by the number 256?
Divide a binary number by two, shifts it one bit to the right.
Dividing by 4 is the same as dividing by 2 twice, 4=2x2. Which shifts it two bits to the right.
So if you want the top 8 bits of a 16-bit value, you need to divide by 2, 8 times.  So 8 times is 2x2x2x2x2x2x2x2 which is 256.
Did that help?