zxbasic icon indicating copy to clipboard operation
zxbasic copied to clipboard

BinToStrinf Function

Open em00k opened this issue 1 year ago • 0 comments

Please can the following be added to both hex.bas which prints a binary representation of a byte, eg 8 would return "00001000"

function FASTCALL BinToString(num as ubyte) as String
	asm
	PROC
	push namespace core
	LOCAL END_CHAR
	LOCAL DIGIT
	LOCAL charloop
	LOCAL bitisset
	LOCAL nobitset
	push af   ; save ubyte 
	ld bc,10
	call __MEM_ALLOC
	ld a, h
	or l
	pop bc 
	ld c,b 
	ret z	; NO MEMORY
	
	push hl	; Saves String ptr
	ld (hl), 8
	inc hl
	ld (hl), 0
	inc hl  ; 8 chars string length

	; c holds out entry 8 bit value, b number of bits 

	ld b,8
charloop:
	call DIGIT
	djnz charloop 
	pop hl	; Recovers string ptr
	ret
	
DIGIT:
	ld a,c
	bit 7,a
	jr nz,bitisset 
	ld a,'0'
	jr nobitset
bitisset:
	ld a,'1'
nobitset:	
	
END_CHAR:
	ld (hl), a
	inc hl
	ld a,c 
	sla c 
	ret
	ENDP
	pop namespace
	end asm
end function

em00k avatar Feb 26 '24 11:02 em00k