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

.486

PUBLIC _bitPack

_bitPack PROC

	ARG num:PTR WORD, byte_arr:PTR BYTE
	mov BP,SP
	mov BX,[byte_arr]	;Use BX as byte array
	mov CX,[num]		;Use CX for the number of bytes in the array
	
start_byte:
	mov DL,00H		;make sure current byte is 00H
	mov DH,80H		;intialize DH as a mask
	mov DI, 8		;Use DI as a counter for the number of bits in a byte

read_bits:
	mov AH, 01H
	int 21H			;read in character
	cmp AL,'1'		;check if character is '1' in ASCII
	jne next_bit		;
	or DL,DH		;if it is, or the mask with the character to add the bit to the byte

next_bit:
	shr DH,1		;shift the mask to correspond with next bit
	dec DI			;go to next bit in input
	cmp DI, 0		;check if end of byte is reached
	jne read_bits		;if the byte isn't done, get next bit
	mov DH,00H		;otherwise, re-initialize the mask
	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	;also, put the character into the array

next_byte:
	dec CX			;to go to next byte, decrement the character counter
	jnz start_byte		;if it hasn't reached zero, go to next character

end_byte_loop:
	mov byte ptr [BX],00H	;add null character to end of array
	
	pop BP
	ret

_bitPack ENDP

END