Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BinToStrinf Function #709

Open
em00k opened this issue Feb 26, 2024 · 0 comments
Open

BinToStrinf Function #709

em00k opened this issue Feb 26, 2024 · 0 comments

Comments

@em00k
Copy link

em00k commented Feb 26, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant