FAQ  •  Register  •  Login

Assembly question

<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Sun May 12, 2013 5:00 pm

Assembly question

In assembly how do you know where to put data?

like say I wanted some arrays how would I know which address in RAM to place the variables?

Is it possible to access arrays created in zxbasic from assembly? how would I do that
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Sun May 12, 2013 6:26 pm

Re: Assembly question

slenkar wrote:In assembly how do you know where to put data?

like say I wanted some arrays how would I know which address in RAM to place the variables?

Is it possible to access arrays created in zxbasic from assembly? how would I do that

This is a... hard question... :| but here we go:
Arrays are just labels. Create a simple program with a DIM array and compile it with --asm and check for the result. After that label, some meta-data follows it, and finally, array cells. The metadata is the number of cells per dimension followed by a Cell Size. For example:
  Code:
DIM myarray(2, 3) as Ubyte

Will generate the following ASM:
  Code:
ZXBASIC_USER_DATA:  ; User declared global variables start here
_myarray:       ; Array LABEL
    DEFW 0001h  ; Number of dimensions minus 1, in this case 2 dimensions => 1
    DEFW 0004h  ; 2nd dimension size, that is 0, 1, 2, 3 => size 4
    DEFB 01h    ; Size of each cell. "As uByte " = 1
    DEFB 00h    ; Cell data (initialized to 0)
    DEFB 00h
    DEFB 00h
    DEFB 00h
    DEFB 00h
    DEFB 00h
    DEFB 00h
    DEFB 00h
    DEFB 00h
    DEFB 00h
    DEFB 00h
    DEFB 00h

The real size of this array is DIM myarray(0..2, 0..3) => 3 x 4 = 12 cells. Each cell is a byte, so 12 bytes.
The first dimension is not taken into account. So, for the compiler, the important information is the 2nd dimension and above.
  • The 1st number (Uinteger) is 0001h, and counts the total number of dimensions minus 1. So, in this case, myarray has 2 dimensions, so this number is 2 - 1 = 1.
  • The 2nd number is the 2nd dimension "range size", that is from 0 TO 3 = size 4.
You can access this data either calling the array subroutine ZX Basic uses (it's already linked with your program if you use any array), or calculate an array cell address on your own, and this is easy:
Array data starts at: ARRAY_LABEL + 3 + 2 * (n - 1), where n = Number of dimensions.
So in this case, the array data starts at:
_myarray + 3 + 2 * (2 - 1) = _myarray + 5
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Sun May 12, 2013 8:05 pm

Re: Assembly question

thanks, I was thinking of translating my pathfinding into assembly to get some more speed
<<

britlion

Posts: 766

Joined: Mon Apr 27, 2009 7:26 pm

Location: Slough, Berkshire, UK

Post Sun May 12, 2013 9:46 pm

Re: Assembly question

You could read my tutorial for Berksman? It discusses things like where to place data for the map in an array within ZX Basic as one of the many topics I rambled on about randomly in passing while making a game....
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Sun May 12, 2013 10:30 pm

Re: Assembly question

britlion wrote:You could read my tutorial for Berksman? It discusses things like where to place data for the map in an array within ZX Basic as one of the many topics I rambled on about randomly in passing while making a game....

I was about to comment on it, but the link is not in the Wiki tutorials page. Can you add it, please?

Also was about to tell about the improvements you suggested for the Array management (some of them speed up the execution by 2x!). At the moment I have almost no time, and the little time I have for this is for... refactoring. The new ZX Basic will be a different, more robust and extensible compiler (I hope).
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Sun May 12, 2013 10:32 pm

Re: Assembly question

slenkar wrote:thanks, I was thinking of translating my pathfinding into assembly to get some more speed

What is the bottleneck? Maybe the A* / pathfinding algorithm?
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Mon May 13, 2013 6:43 pm

Re: Assembly question

The bottleneck is probably trying to find the closest node,

I store the closest distance so I only look for the closest node when I have to. But its still kinda slow
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Tue May 14, 2013 12:27 am

Re: Assembly question

britlion wrote:You could read my tutorial for Berksman? It discusses things like where to place data for the map in an array within ZX Basic as one of the many topics I rambled on about randomly in passing while making a game....



WHere is it again?
<<

britlion

Posts: 766

Joined: Mon Apr 27, 2009 7:26 pm

Location: Slough, Berkshire, UK

Post Tue May 14, 2013 10:08 am

Re: Assembly question

It's listed in the wiki: http://www.boriel.com/wiki/en/index.php/ZXBasic

Explicitly, this one is at: http://goo.gl/4jPd5


Early on it talks about storing UDG data inside the code in safe places and accessing it. Then how to put the maze data into a DIM. Then a faster way - store the screen image instead, since it's way faster to put up as a screen$ type block. Then function fastHalfSquares is discussed, which contains a data table, as well as the code that accesses it - and so on.

Hope it helps!
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Tue May 14, 2013 1:22 pm

Re: Assembly question

POKE UINTEGER 23675, @graphicsbank1

Does this poke the address of graphicsbank1 to RAM location 23675?


So the spectrum looks for UDG's at the address that 23675 points to?
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue May 14, 2013 1:35 pm

Re: Assembly question

slenkar wrote:POKE UINTEGER 23675, @graphicsbank1

Does this poke the address of graphicsbank1 to RAM location 23675?


So the spectrum looks for UDG's at the address that 23675 points to?

Yes, that's right. :roll:
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Tue May 14, 2013 3:09 pm

Re: Assembly question

im trying to learn assembly, but keep getting strange results

this program for example

function FASTCALL downone() as ubyte
asm
INC HL
end asm
end function

function FASTCALL setup() as ubyte
asm
LD HL,16384
end asm
end function
'LD B,0

function FASTCALL drawthis() as ubyte
asm
LD A,128
LD (HL),A
end asm
end function

Dim res as String
setup()
for x=0 to 100
downone()
drawthis()
next



this is supposed to draw something to the screen 100 times
is the for next loop interfering with the assembly?
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Tue May 14, 2013 7:46 pm

Re: Assembly question

how is it best to think of the spectrum screen memory?

as hex or binary?
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue May 14, 2013 9:13 pm

Re: Assembly question

slenkar wrote:how is it best to think of the spectrum screen memory?

as hex or binary?

From what I've read (and what I used for drawing) binary is better if you want to understand ZX Spectrum awkward screen memory addressing :roll:
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue May 14, 2013 9:17 pm

Re: Assembly question

slenkar wrote:im trying to learn assembly, but keep getting strange results

this program for example

function FASTCALL downone() as ubyte
asm
INC HL
end asm
end function

function FASTCALL setup() as ubyte
asm
LD HL,16384
end asm
end function
'LD B,0

function FASTCALL drawthis() as ubyte
asm
LD A,128
LD (HL),A
end asm
end function

Dim res as String
setup()
for x=0 to 100
downone()
drawthis()
next


this is supposed to draw something to the screen 100 times
is the for next loop interfering with the assembly?

Probably. From one basic instruction to another, *everything* can be destroyed in assembler (registers, etc).

ZX Basic uses memory (stack) to store temporary results from one instruction to another, but sometimes it realizes that two basic instructions together can be "operated" as a single instruction. And again, this new instruction can be packed with the next one as a single instruction, and so on. But once you insert an ASM block, this optimization is stopped.
Next

Return to Help & Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums/DivisionCore.

phpBB SEO