Local variables in assembly?

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

By DarkSchneider

Paladin (819)

DarkSchneider's picture

18-01-2016, 11:09

If I remember well, in x86 we could ADD or SUB the SP, so we could make room for function local variables. But what about in Z80?. I only see PUSH and POP and I don't like them much, because the order and 16-bit size requirements.

Login or register to post comments

By SuchAGoonie

Expert (108)

SuchAGoonie's picture

18-01-2016, 11:14

You can use LD SP,HL to set the stack pointer to where ever you want. Just be sure to DI and EI and of course to restore the stack pointer when you're done. Wink

By Metalion

Paladin (999)

Metalion's picture

18-01-2016, 11:18

Not sure I understand your question.

But SP can only be modified directly (by using LD SP, xxxx) in order to move the stack around. Being a stack, that's the reason its content is best accessed thru POP and PUSH, though it can also be accessed thru a very limited set of other instructions (like EX (SP),HL for example). Generally speaking, unless you plan to use tricky stack access, it's best to stay with POP and PUSH.

Now, as far as local variables are concerned, it's up to you to make room for them in RAM, knowing where SP is.

By DarkSchneider

Paladin (819)

DarkSchneider's picture

18-01-2016, 12:04

@Metalion currently I use that, memory. But then they are more like global, and requires to define them in "phase" part of the code instead the function itself.

Local variables like:

function
  sub sp, 4 ; use 4 bytes for local variables
  ...
  ld (sp+1), a ; store A in our local space
  ...
  add sp, 4 ; restore SP
  ret

At least, this is how it was done in x86.

@SuchAGoonie but how can I read SP?, the LD HL, SP doesn't exist.

By Daemos

Paragon (1669)

Daemos's picture

18-01-2016, 12:19

you mean something like?


 ld a,2
 ld (variable),a ;write to memory

;read from memory
 ld a,(variable)

variable: db 0 ;place in ram to store

By RetroTechie

Paragon (1563)

RetroTechie's picture

18-01-2016, 12:26

DarkSchneider wrote:

@SuchAGoonie but how can I read SP?, the LD HL, SP doesn't exist.

ld (address),sp

Restore with

ld sp,(address)

Other instructions that read SP:

add hl,sp
adc hl,sp
sbc hl,sp
add ix,sp
add iy,sp

By DarkSchneider

Paladin (819)

DarkSchneider's picture

18-01-2016, 12:52

@Daemos yes, but local to the function. So it can be "exported" to a separate file.

@RetroTechie OK given those, I think it could be done like:

	; make room on SP
	ld (spram), sp ; ld hl, sp
	ld hl, (spram)
	ld de, 4 ; size
	or a ; clean CY
	sbc hl, de ; make room
	ld sp, hl ; overwrite
	ld ix, (spram) ; use IX for accessing local storage
	;use SP
	ld a, (ix - 1) ; get first byte of our local storage
	; restoreSP
	ld hl, (spram)
	ld sp, hl

; in RAM
spram
  dw 0

But we need RAM definition yet, and be sure is not the same for multiple functions if there are inter-calls. So it seems Z80 is not much prepared to modify the SP.

By Daemos

Paragon (1669)

Daemos's picture

18-01-2016, 12:56

separate file
like?:


 ld a,(variable)


include "variables.asm"

in file variables.asm

variableadress: equ $c000
variables:

variable1: db 0
variable2: db 0
variable3: db 0

;etc etc etc
lenghtvariables: equ $ - variables

then you can for example do

ld hl,$c000 ;or whatever RAMarea of choice
ld de,variables
ld bc,lenghtvariables
ldir

By DarkSchneider

Paladin (819)

DarkSchneider's picture

18-01-2016, 13:12

But they are global. Think in this situation:

func1
  ...
  ld a, (variable1) ; it uses variable1
  ...
  call func2
  ...
  ret

func2
  ...
  ld a, (varialbe1) ; the same one?
  ...
  ret

With local data you have no problem at all. There is no need to see what functions call what other ones, or write the functions so they use unique variables not used by others depending on calls. (SP-1) is unique for all if they sub and restore the SP correctly.

By SuchAGoonie

Expert (108)

SuchAGoonie's picture

18-01-2016, 13:15

I don't think what you want is possible. Maybe the best solution would be to prefix the variables and keep them with the function. In pseudo-code:

func1_var1: db 0
func1_var2: db 0

func1:
  ...
  ret

func2_var1: db 0
func2_var2: db 0

func2:
  ...
  ret

By wouter_

Champion (412)

wouter_'s picture

18-01-2016, 13:18

It could be done like this

   ; sub sp,4
   ld hl,-4
   add hl,sp
   ld sp,hl
   ...
   ; add sp,4
   ld hl,4
   add hl,sp
   ld sp,hl

Or if you use IX/IY instead of HL, you can use the (IX+n) addressing mode to access your local vars.

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