How to resolve the algorithm Split a character string based on change of character step by step in the Z80 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Split a character string based on change of character step by step in the Z80 Assembly programming language

Table of Contents

Problem Statement

Split a (character) string into comma (plus a blank) delimited strings based on a change of character   (left to right). Show the output here   (use the 1st example below).

Blanks should be treated as any other character   (except they are problematic to display clearly).   The same applies to commas.

For instance, the string: should be split and show:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Split a character string based on change of character step by step in the Z80 Assembly programming language

Source code in the z80 programming language

PrintChar equ &BB5A ;Amstrad CPC BIOS call
Terminator equ 0    ;marks the end of a string
        org &8000

	LD HL,StringA
loop:
	ld a,(HL)		;load a char from (HL)
	cp Terminator	        ;is it the terminator?
	ret z			;if so, exit
	ld e,a			;store this char in E temporarily
	inc hl			;next char
	ld a,(HL)		;get next char
	cp Terminator	        ;is the next char the terminator?
	jp z,StringDone	        ;if so, print E and exit.

	;needed to prevent the last char from getting a comma and space.
	
	dec hl			;go back one so we don't skip any chars
	cp e                    ;does (HL) == (HL+1)?
	
	push af
	ld a,e
	call PrintChar		;either way, print E to screen.
	pop af			;retrieve the results of the last compare.
	
	jr z,SkipComma		;if A=E, no comma or space. Just loop again.
	ld a,','
	call PrintChar
	ld a,' '
	call PrintChar
SkipComma:
	inc hl			;next char
	jp loop			;back to start
StringDone:
	ld a,e			;last character in string is printed here.
	jp PrintChar
	
ReturnToBasic:
	RET
	

StringA:
	byte "gHHH5YY++///\",0

  

You may also check:How to resolve the algorithm Multiple distinct objects step by step in the Scala programming language
You may also check:How to resolve the algorithm Super-d numbers step by step in the D programming language
You may also check:How to resolve the algorithm ABC problem step by step in the PowerBASIC programming language
You may also check:How to resolve the algorithm Read entire file step by step in the TXR programming language
You may also check:How to resolve the algorithm Multiple regression step by step in the J programming language