News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_Joseman

Help with a strange division...

Started by Joseman, 15:53, 28 September 16

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Joseman

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!!



Prodatron

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

PulkoMandy

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).

Joseman

#3
Quote from: Prodatron on 15: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).

Quote from: PulkoMandy on 16: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).

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!

Prodatron

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

andycadley


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.

IanS

Quote from: Joseman on 17:55, 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?

Powered by SMFPacks Menu Editor Mod