How to displace a memory address in assembler n times

By dehm

Supporter (8)

dehm's picture

04-10-2021, 21:57

Hello:

I have a begginer question. If I want to displace a memory address a known number of times, I have tried that (snippet):

    ld    de,  texto	
    ld    b,    4
loop_incrementar_posicion_memoria:
    inc    de
    djnz    loop_incrementar_posicion_memoria
texto:	db	"Hola!"	;5 caracteres

It works, but I wonder if I have a long number of iterations, I would be a problem, and then, I was working what is the properly way to displace n times a memory address.

Thank you in advance

Login or register to post comments

By Grauw

Ascended (10146)

Grauw's picture

04-10-2021, 21:59

Hi dehm,

Put the memory address in hl and the offset in bc, and use the add hl,bc instruction.

By dehm

Supporter (8)

dehm's picture

04-10-2021, 22:25

Grauw wrote:

Hi dehm,

Put the memory address in hl and the offset in bc, and use the add hl,bc instruction.

Very thank you!

Edit:

I get an error if I try to do that in DE

Well, the thing is that I want to do is
- move HL and DE to the first position of the string
- move DE n times (until the last position of the string)
- then, I'll can to swap the characteres of the string

This is my complete code (it works):

CHGET	equ	#009F
CHPUT	equ	#00A2
POSIT	equ	#00C6
INLIN	equ	#00B1


	db #FE
	dw START
	dw END
	dw START
	
	org #8200
		
START:

mostrar_texto_en_pantalla:
	ld		hl,#0505
	call	POSIT	
	ld		hl,pregunta
loop_mostrar:
	ld		a,(hl)
	call	CHPUT
	inc		hl
	cp		0
	jp		nz,loop_mostrar	
	ld		hl,	texto
	ld		de,	texto	
	ld		b,4
;	call	INLIN
	
loop_incrementar_posicion_memoria:
	inc		de
	djnz	loop_incrementar_posicion_memoria
;ahora tenemos DE en la posicion de destino
	ld		b,2
loop_permutar:
	ld		a,(hl)
	ex		af,af'
	ld		a,(de)
	ld		(hl),a
	ex		af,af'
	ld		(de),a
	inc		hl
	dec		de
	djnz	loop_permutar
	ret
	
;constantes
texto:	db	"Hola!"	;5 caracteres
pregunta:	db	"Inserte una frase:",0
	
END:

Then, I need to offset DE (or change the strategy, or following study assembler oO )

By santiontanon

Paragon (1519)

santiontanon's picture

04-10-2021, 23:10

You cannot do 16bit additions on DE directly. So, if you want to offset DE, the easiest way is to move the value of DE to HL, then do what Grauw suggested, and then move the result back to DE. For example:

    ld de,texto
    ex de,hl  ; <- this swaps the content of DE and HL (so, now HL = texto)
    ld bc,4
    add hl,bc   ; HL is now "texto + 4"
    ex de,hl  ; <- swaps DE and HL again. So, now DE = texto + 4

By dehm

Supporter (8)

dehm's picture

05-10-2021, 19:27

santiontanon wrote:

You cannot do 16bit additions on DE directly. So, if you want to offset DE, the easiest way is to move the value of DE to HL, then do what Grauw suggested, and then move the result back to DE. For example:

    ld de,texto
    ex de,hl  ; <- this swaps the content of DE and HL (so, now HL = texto)
    ld bc,4
    add hl,bc   ; HL is now "texto + 4"
    ex de,hl  ; <- swaps DE and HL again. So, now DE = texto + 4

Very thank you @santiontanon and Grauw. It works perfectly!