Task

Create and display the sequence of binary digits for a given   non-negative integer.

   The decimal value      5   should produce an output of               101
   The decimal value     50   should produce an output of            110010
   The decimal value   9000   should produce an output of    10001100101000

The results can be achieved using built-in radix functions within the language   (if these are available),   or alternatively a user defined function can be used.

The output produced should consist just of the binary digits of each number followed by a   newline.

There should be no other whitespace, radix or sign markers in the produced output, and leading zeros should not appear in the results.




org &8000
PrintChar equ &BB5A   ;syscall - prints accumulator to Amstrad CPC's screen
 
 
main:
 
	ld hl,TestData0
	call PrintBinary_NoLeadingZeroes
 
	ld hl,TestData1
	call PrintBinary_NoLeadingZeroes
 
	ld hl,TestData2
	call PrintBinary_NoLeadingZeroes
 
	ret
 
TestData0:  
	byte 5,255
TestData1:  
	byte 5,0,255
TestData2:  
	byte 9,0,0,0,255
 
temp:
    byte 0	
	;temp storage for the accumulator
	;	we can't use the stack to preserve A since that would also preserve the flags.
 
 
PrintBinary_NoLeadingZeroes:
;setup:
	ld bc,&8000            ;B is the revolving bit mask, C is the "have we seen a zero yet" flag
 
 
NextDigit:
	ld a,(hl)
	inc hl
	cp 255
	jp z,Terminated
 
	ld (temp),a
NextBit:
	ld a,(temp)
	and b
	jr z,PrintZero
	; else, print one
	ld a,'1'                ;&31
	call &BB5A
	set 0,b			;bit 0 of B is now 1, so we can print zeroes now.
	jr Predicate
 
PrintZero:
	ld a,b
	or a
	jr z,Predicate		;if we haven't seen a zero yet, don't print a zero.
	ld a,'0'		;&30
	call &BB5A
 
 
Predicate:
	rrc b				
	;rotate the mask right by one. If it sets the carry, 
	;	it's back at the start, and we need to load the next byte.
	jr nc,NextBit
 
	jr NextDigit		;back to top
 
Terminated:
	ld a,13
	call &BB5A
	ld a,10
	jp &BB5A		;its ret will return for us.