TITLE	Assignment 3 BITPACK.ASM

COMMENT | Written by Caitlin Ross, 100735219, COMP 2003 A 
	|

.MODEL SMALL

EXTRN C checkDigit:PROC
EXTRN C printDigit:PROC

.CODE

PUBLIC _bitPack

_bitPack PROC

	ARG num:PTR WORD, byte_arr:PTR BYTE
	push BP
	push DI
	mov BP,SP
	;mov BX, [BP + 6]
	mov BX,[byte_arr]	;Use BX as byte array
	mov CX,[num]		;Use CX for the number of bytes in the array
	;mov CX, [BP + 4]
	call printDigit C, CX
	
start_byte:
	mov DL,00H		;make sure current byte is 00H
	mov DH,80H		;intialize mask
	mov DI, 8

read_bits:
	mov AH, 01H
	int 21H			;read in character
	cmp AL,'1'		;check if character is '1' in ASCII
	jne next_bit		;if not, keep going in loop
	or DL,DH		;or the mask with the character to add the bit

next_bit:
	shr DH,1		;shift the mask to correspond with next bit
	dec DI
	cmp DI, 0
	jne read_bits		;if the byte isn't done, get next bit
	mov DH,00H
	call checkDigit C, DX	;check if the digit is a valid hex digit
	cmp AX,1
	jne next_byte		;if not, restart this byte
	inc BX			;if it is, go to next element
	mov byte ptr [BX],DL

next_byte:
	;loop start_byte		;keep going until number of bytes is reached
	dec CX
	jnz start_byte
	;jmp end_byte_loop	;if number is reached, go to end of loop

;continue:
	;jmp read_bits		;if the byte wasn't done, read another bit

end_byte_loop:
	mov byte ptr [BX],00H	;add null character to end of array
	
	pop BP
	ret

_bitPack ENDP

END