Local variables in assembly?

Page 5/5
1 | 2 | 3 | 4 |

Par hit9918

Prophet (2921)

Portrait de hit9918

21-01-2016, 00:26

there are two separate things.
grauw and msd push the masses down the stack.
but darkschneider just went out of registers, need just some more bytes.
local variables per se are not about arrays on the stack.
int i,j,k; that is 3 things, and you never walk over these 3 things with for i = 0 to 2 like they were an array.
they are 3 things like BC, DE, HL are 3 things.

it may happen that sometimes i is in HL and sometimes j is in HL. HL used for multiple things.
but that wouldnt happen if you had 8 HL registers.
the 68000 does. then i,j,k would be in hl0, hl1, hl2 and you got five more hl left.
that is a bit unfair that it has so many hl when it can do ld hl,(sp+20) in one instruction anyways.
same thing with 386.

but when one pushes whole structs down the stack, that has more touch of "local array".
they cant be in registers when there are functions used to index them in RAM.

another thing is interrupts messing the minus side of the stackpointer. that is why this doesn't work:

	ld hl,4
	add hl,sp
	ld sp,hl
	pop de

slide 4 further, quick pop a 16bit local variable.
this needs 39 cycles. while 2x ld r,(ix+n) needs 42 cycles.
when you push back the local soon later without another sp setup, or when you pop 2 in a row, the advantage gets dramatic.
but the variables on the minus side of the stackpointer get trashed by interrupts.

Par DarkSchneider

Paladin (942)

Portrait de DarkSchneider

21-01-2016, 09:25

The FOR I, J, K was only an example, not my current need Wink In a program is very likely you will be out of registers, so it has a global scope. As said, dynamic allocation is much more convenient.

Par Prodatron

Paragon (1808)

Portrait de Prodatron

21-01-2016, 18:11

msd wrote:

A while ago I made a function to allocate space on the stack, which is automatically cleaned when you exit a function

Cool routine, this is probably the most useful one, if you need it for many different subroutines!

Par ARTRAG

Enlighted (6832)

Portrait de ARTRAG

21-01-2016, 21:29

msd wrote:

A while ago I made a function to allocate space on the stack, which is automatically cleaned when you exit a function

AllocateStackSpace:
; Input
; 		BC = Size to allocate
; Output :	HL = Pointer to allocated stack space
;	
; Mofifies BC,HL,IX
		POP	IX			; Get return address
		LD	HL,0
		ADD	HL,SP
		OR	A,A
		SBC	HL,BC
		LD	SP,HL
		PUSH	BC			; Store object size on stack
		LD	BC,FreeStackSpace
		PUSH 	BC			; Free stack function on stack
		JP	(IX)
		
FreeStackSpace:
		POP	HL	; Get object size
		ADD	HL,SP
		LD	SP,HL	; Restore Stack
		RET

This function is the first function you need to call before any other push or pop.

It can be used as follows

  Test:
		LD	BC,100
		CALL	AllocateStackSpace
                ; HL points too allocated space          
                RET

Great code! Greetings

Par Grauw

Ascended (10565)

Portrait de Grauw

21-01-2016, 21:51

I like it a lot as well! Smile Great idea, you get actual scoping.

Par giangiacomo.zaffini

Champion (267)

Portrait de giangiacomo.zaffini

21-01-2016, 23:19

I read the whole thread in a rush, great stuff! Please continue with this tone Tongue

Par Grauw

Ascended (10565)

Portrait de Grauw

21-01-2016, 23:22

Here’s a variation on msd’s routine which puts the allocated space in ix:

; ix = -size
; ix <- allocated space
; Modifies: de, hl, on return: hl
AllocateStackSpace:
	pop de
	ld hl,0
	add hl,sp
	add ix,sp
	ld sp,ix
	push hl
	call AllocateStackSpace_JumpDE
	pop hl
	ld sp,hl
	ret
AllocateStackSpace_JumpDE:
	ex de,hl
	jp hl

I haven’t actually tested it but I think it works, also it’s a little faster.

Page 5/5
1 | 2 | 3 | 4 |