Thread: I can't spot my mistake...

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048

    I can't spot my mistake...

    I'll start with the output
    Code:
    make rebuild
    MAKECMDGOALS=rebuild
    make -f main.mak rebuild
    make[1]: Entering directory '~/alu'
    PRJ_SRC_FILES = 'test.c alu_bit.c alu_main.c alu_math.c alu_mem.c alu_vec.c'
    rm -f *.AppImage *.exe *.so *.dll *.o *.obj
    cc -fPIC -shared -Wall -Wextra -I cloned/fbstdc/include  -o test.o -c test.c
    make[1]: Leaving directory '~/alu'
    fatal: not in a git directory
    In file included from test.c:1:
    alu.h:12:10: fatal error: allstdint.h: No such file or directory
       12 | #include <allstdint.h>
          |          ^~~~~~~~~~~~~
    compilation terminated.
    make[1]: *** [main.mak:98: test.o] Error 1
    make: *** [1st.mak:6: rebuild] Error 2
    Compilation failed.
    I've checked and the files were certainly downloaded and saved to the right place, I'm guessing I'm using the -I flag wrong maybe? If you need more just tell me what you're looking for and I'll post it

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Never mind, just spotted my mistake, I had renamed the files but forgot to change it in code

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Made another mistake that I can't identify, this seemed an appropriate thread to tack it onto instead of creating another thread.
    This:
    Code:
    $(info $(call rebase,$(FBSTDC_DIR)))
    $(shell $(call rebase,$(FBSTDC_DIR)))
    Lead to this:
    Code:
    cd 'cloned/fbstdc' && git config pull.rebase true && git pull
    main.mak:42: *** missing separator.  Stop.
    Any ideas what I did wrong there?

  4. #4
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    303
    You need a real tab instead of space
    Makefile:2: *** missing separator. Stop - Stack Overflow
    "without goto we would be wtf'd"

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    By that I assume you mean when under a target, this is supposed to be executed BEFORE any targets, or am I mistaken? Here's a slightly larger snippet:
    Code:
    COMPILE_EXE=$(CC) $(BIN_FLAGS) $1 $(COP)o $2 $3 $(RPATH_FLAG)
    COMPILE_DLL=$(CC) $(LIB_FLAGS) $1 $(COP)o $2 $3 $(RPATH_FLAG)
    COMPILE_OBJ=$(CC) $(SRC_FLAGS) $1 $(COP)o $2 -c $3
    
    $(info PRJ_SRC_FILES = '$(PRJ_SRC_FILES)')
    
    $(call mkdir,$(PRJ_EXT_DIR))
    $(call github_clone,$(FBSTDC_DIR),$(PRJ_EXT_DIR),awsdert/fbstdc)
    $(info $(call rebase,$(FBSTDC_DIR)))
    $(shell $(call rebase,$(FBSTDC_DIR)))
    
    info: .FORCE

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    I resolved it by putting an $(info) around the $(shell) part

  7. #7
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    First, a link to the project:
    GitHub - awsdert/alu: Library modeled after the Arithmetic Logic Unit built into CPUs
    Now for the main part, I ran into an inconsistency between the results of multiplication, small numbers work fine but a larger number resulted in an empty value when the opposite should have been there, someone mind taking a look at alu_mul() (in alu_math.c) and seeing if they can find potential causes

    Edit: Occurred to me just after hitting post that I could've just posted the code also:
    Code:
    int alu_mul( alu_t *alu, int num, int val )
    {
    	bool carry = 0;
    	int ret = alu_check2( alu, num, val ), tmp = 0;
    	alu_reg_t *N, *V, *T;
    	alu_bit_t v = {0}, e;
    	size_t bits = 0;
    	
    	if ( ret != 0 )
    		return ret;
    	
    	ret = alu_get_reg( alu, &tmp, sizeof(size_t) );
    	
    	if ( ret != 0 )
    		return ret;
    	
    	N = alu->regv + num;
    	V = alu->regv + val;
    	T = alu->regv + tmp;
    
    	v = V->init;
    	e = N->upto;
    	
    	for ( ; v.b < e.b; v = alu_bit_inc( v ), ++bits )
    	{
    		if ( *(v.S) & v.B )
    		{
    			(void)alu__shl( alu, num, bits );
    			ret = alu_add( alu, tmp, num );
    			bits = 0;
    			
    			if ( ret == EOVERFLOW )
    				carry = true;
    			else if ( ret != 0 )
    				break;
    		}
    	}
    	
    	memcpy( N->part, T->part, alu->buff.perN );
    	alu_rem_reg( alu, tmp );
    	
    	return carry ? EOVERFLOW : 0;
    }

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Managed to narrow the problematic line but unable to figure out a fix, used a fix that is slower to resolve the output temporarily:
    Code:
    int alu_mul( alu_t *alu, int num, int val )
    {
    	bool carry = 0;
    	int ret = alu_check2( alu, num, val ), tmp = -1;
    	alu_reg_t *V;
    	alu_bit_t p, v = {0}, e;
    	size_t bits = 0;
    	
    	if ( ret != 0 )
    		return ret;
    	
    	ret = alu_get_reg( alu, &tmp, sizeof(size_t) );
    	
    	if ( ret != 0 )
    		return ret;
    	
    	V = alu->regv + val;
    	p = v = V->init;
    	e = V->upto;
    	
    	alu__or( alu, tmp, num );
    	alu_xor( alu, num, num );
    	
    	for ( ; v.b < e.b; v = alu_bit_inc( v ), ++bits )
    	{
    		if ( *(v.S) & v.B )
    		{
    			//(void)alu__shl( alu, tmp, bits );
    			ret = alu_add( alu, num, tmp );
    			bits = 0;
    			
    			if ( ret == EOVERFLOW )
    				carry = true;
    			else if ( ret == ENODATA )
    				break;
    			else if ( ret != 0 )
    			{
    				alu_error( ret );
    				break;
    			}
    		}
    		(void)alu__shl( alu, tmp, 1 );
    	}
    	
    	alu_rem_reg( alu, tmp );
    	
    	return carry ? EOVERFLOW : ret;
    }
    As you can see I commented out the problematic line, did some tests with the shift function and I don't think that was the cause, I believe maybe the value of bits is somehow incorrect, any ideas are welcome

  9. #9
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Found the cause, I forgot to check for the situation where the number of bits to move by was 0, after skipping the shift in that situation the erroneous output was resolved.

  10. #10
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Not quite able to get signed division right:
    Code:
    make run
    MAKECMDGOALS=run
    make --no-print-directory -f main.mak run
    PRJ_SRC_FILES = 'test.c alu_bit.c alu_int.c alu_main.c alu_math.c alu_mem.c alu_uint.c alu_vec.c'
    Checking 3rd Party libraries are upto date
    cd 'cloned/fbstdc' && git config pull.rebase true && git pull
    Already up to date.
    Finished checking
    cc -D NDEBUG -fPIC -shared -Wall -Wextra -I cloned/fbstdc/include  -o alu_math.o -c alu_math.c
    cc -D NDEBUG -fPIC -shared  -o libalu.so alu_bit.o alu_int.o alu_main.o alu_math.o alu_mem.o alu_uint.o alu_vec.o -Wl,-rpath=./
    cc -D NDEBUG -fPIE -L . -l alu  -o alu.AppImage test.o -Wl,-rpath=./
    ./alu.AppImage
    test.c:874: main() 'Initiating ALU to 0...'
    test.c:877: main() 'Pre-allocating 16 ALU registers...'
    test.c:741: bitwise() 'Checking bitwise operations...'
    test.c:742: bitwise() '==========================================='
    test.c:766: bitwise() 'Shifting values...'
    test.c:781: bitwise() 'Rotating values...'
    test.c:798: mathmatical() 'Mathematics values...'
    test.c:551: int_modify() 3735929054 / 2989, Expected 1249892, Got 4294967295, op = '/'
    test.c:551: int_modify() 3735929054 % 2989, Expected 1866, Got 3735932043, op = '%'
    test.c:551: int_modify() -3735929054 / 2989, Expected -1249892, Got -4294967295, op = '/'
    test.c:551: int_modify() -3735929054 % 2989, Expected -1866, Got -3735932041, op = '%'
    test.c:551: int_modify() -3735929054 / -2989, Expected 1249892, Got 4294967295, op = '/'
    test.c:551: int_modify() -3735929054 % -2989, Expected -1866, Got 3735932039, op = '%'
    Compilation finished successfully.
    Any ideas based from this code?
    Code:
    int alu_divide( alu_t *alu, uint_t num, uint_t val, uint_t rem )
    {
    	int ret = alu_check3( alu, num, val, rem ), cmp = 0;
    	alu_reg_t *N, *V, *R, TR, TV;
    	alu_bit_t n, v;
    	size_t bits = 0, bit;
    	bool nNeg, vNeg;
    	
    	if ( ret != 0 )
    		return ret;
    		
    	N = alu->regv + num;
    	R = alu->regv + rem;
    	V = alu->regv + val;
    	
    	TR = *R;
    	TV = *V;
    	
    	nNeg = ((N->info & ALU_REG_F__SIGN) && (*(N->last.S) & N->last.B));
    	vNeg = ((V->info & ALU_REG_F__SIGN) && (*(V->last.S) & V->last.B));
    	
    	if ( nNeg )
    	{
    		alu_not( alu, num );
    		alu_dec( alu, num );
    	}
    	
    	if ( vNeg )
    	{
    		alu_not( alu, val );
    		alu_dec( alu, val );
    	}
    	
    	(void)alu_xor( alu, rem, rem );
    	(void)alu__or( alu, rem, num );
    	(void)alu_xor( alu, num, num );
    
    	(void)alu_end_bit( alu, rem, &(n) );
    	(void)alu_end_bit( alu, val, &(v) );
    	
    	R->upto = alu_bit_inc(n); R->last = n;
    	V->upto = alu_bit_inc(v); V->last = v;
    	
    	for (
    		R->init = n; R->init.b > TR.init.b;
    		++bits, R->init = alu_bit_dec(R->init)
    	)
    	{
    		(void)alu_cmp( alu, rem, val, &cmp, &bit );
    		
    		if ( cmp >= 0 )
    		{			
    			ret = alu_sub( alu, rem, val );
    			
    			if ( ret == ENODATA )
    				break;
    			
    			(void)alu__shl( alu, num, bits );
    			*(N->init.S) |= N->init.B;
    			bits = 0;
    			ret = 0;
    		}
    	}
    	
    	if ( bits )
    	{
    		(void)alu_cmp( alu, rem, val, &cmp, &bit );
    		(void)alu__shl( alu, num, bits );
    		
    		if ( cmp >= 0 )
    		{
    			ret = alu_sub( alu, rem, val );
    			*(N->init.S) |= N->init.B;
    		}
    	}
    	
    	*R = TR;
    	*V = TV;
    	
    	if ( nNeg != vNeg )
    	{
    		alu_not( alu, num );
    		alu_inc( alu, num );
    		
    		alu_not( alu, rem );
    		alu_inc( alu, rem );
    	}
    	
    	if ( vNeg )
    	{
    		alu_not( alu, val );
    		alu_inc( alu, val );
    	}
    	
    	return ret;
    }

  11. #11
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Managed to fix it, apparently division does not like optimizations all that much:
    Code:
    int alu_divide( alu_t *alu, uint_t num, uint_t val, uint_t rem )
    {
    	int ret = alu_check3( alu, num, val, rem );
    	alu_reg_t *N, *V, *R, TR;
    	alu_bit_t n;
    	size_t bits = 0;
    	bool nNeg, vNeg;
    	
    	if ( ret != 0 )
    		return ret;
    		
    	N = alu->regv + num;
    	V = alu->regv + val;
    	R = alu->regv + rem;
    	
    	TR = *R;
    	
    	nNeg = ((N->info & ALU_REG_F__SIGN) && (*(N->last.S) & N->last.B));
    	vNeg = ((V->info & ALU_REG_F__SIGN) && (*(V->last.S) & V->last.B));
    	
    	if ( nNeg )
    		(void)alu_neg( alu, num );
    	
    	if ( vNeg )
    		(void)alu_neg( alu, val );
    	
    	(void)alu_xor( alu, rem, rem );
    	(void)alu__or( alu, rem, num );
    	(void)alu_xor( alu, num, num );
    
    	n = alu_end_bit( *R );
    	
    	for (
    		; alu_compare( TR, *V, NULL ) >= 0
    		; ++bits, n = alu_bit_dec(n)
    	)
    	{
    		R->init = n;
    		if ( alu_compare( *R, *V, NULL ) >= 0 )
    		{
    			ret = alu_sub( alu, rem, val );
    			
    			if ( ret == ENODATA )
    				break;
    			
    			(void)alu__shl( alu, num, bits );
    			*(N->init.S) |= N->init.B;
    			bits = 0;
    		}
    	}
    	
    	/* Clear return value while we're at it */
    	ret = alu__shl( alu, num, bits + 1 );
    	
    	*R = TR;
    	
    	if ( nNeg != vNeg )
    	{
    		(void)alu_neg( alu, num );
    		(void)alu_neg( alu, rem );
    	}
    	
    	if ( vNeg )
    		(void)alu_neg( alu, val );
    	
    	return ret;
    }
    Now I just have the problem of 1 result not being negated as expected:
    Code:
    test.c:565: int_modify() -3735929054 % -2989, Expected -1866, Got 1866, op = '%'

  12. #12
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Managed to fix it:
    Code:
    	if ( nNeg != vNeg )
    		(void)alu_neg( alu, num );
    	
    	if ( nNeg )
    		(void)alu_neg( alu, rem );
    	
    	if ( vNeg )
    		(void)alu_neg( alu, val );
    Just had to adjust the method of re-negating

  13. #13
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    New problem, attempted to convert a string of "123" to and from a register, got a wildly different result:
    Code:
    make run
    MAKECMDGOALS=run
    make --no-print-directory -f main.mak run
    PRJ_SRC_FILES = 'test.c alu_bit.c alu_int.c alu_main.c alu_math.c alu_mem.c alu_uint.c alu_vec.c'
    Checking 3rd Party libraries are upto date
    cd 'cloned/fbstdc' && git config pull.rebase true && git pull
    Already up to date.
    Finished checking
    cc -D NDEBUG -fPIC -shared -Wall -Wextra -I cloned/fbstdc/include  -o test.o -c test.c
    cc -D NDEBUG -fPIE -L . -l alu  -o alu.AppImage test.o -Wl,-rpath=./
    ./alu.AppImage
    test.c:1002: main() 'Initiating ALU to 0...'
    test.c:1005: main() 'Pre-allocating 16 ALU registers...'
    test.c:953: print_value() num = '123'
    test.c:915: func_wrChar32() str = ''
    test.c:917: func_wrChar32() str = '1'
    test.c:915: func_wrChar32() str = '1'
    test.c:917: func_wrChar32() str = '10'
    test.c:915: func_wrChar32() str = '10'
    test.c:917: func_wrChar32() str = '106'
    test.c:915: func_wrChar32() str = '106'
    test.c:917: func_wrChar32() str = '1062'
    test.c:915: func_wrChar32() str = '1062'
    test.c:917: func_wrChar32() str = '10628'
    test.c:915: func_wrChar32() str = '10628'
    test.c:917: func_wrChar32() str = '106282'
    test.c:915: func_wrChar32() str = '106282'
    test.c:917: func_wrChar32() str = '106282'
    test.c:987: print_value() alu = ''
    test.c:691: compare() 'Comparing values...'
    test.c:692: compare() '==========================================='
    test.c:757: bitwise() 'Checking bitwise operations...'
    test.c:758: bitwise() '==========================================='
    test.c:782: bitwise() 'Shifting values...'
    test.c:797: bitwise() 'Rotating values...'
    test.c:814: mathmatical() 'Mathematics values...'
    Compilation finished successfully.
    Can anyone see potential causes from the below code?
    Code:
    int func_rdChar32( char32_t *dst, alu_block_t *src, long *nextpos )
    {
    	char *str = src->block;
    	if ( (size_t)(*nextpos) < src->bytes.used )
    	{
    		*dst = str[*nextpos];
    		return 0;
    	}
    	
    	*dst = 0;
    	return EOF;
    }
    
    int func_wrChar32( char32_t src, alu_block_t *dst )
    {
    	int ret;
    	char *str;
    	if ( dst->bytes.used >= dst->bytes.last )
    	{
    		ret = alu_block_expand( dst, dst->bytes.used + 50 );
    		if ( ret != 0 )
    			return ret;
    	}
    	str = dst->block;
    	alu_printf( "str = '%s'", str );
    	str[dst->bytes.used] = src;
    	alu_printf( "str = '%s'", str );
    	dst->bytes.used++;
    	return 0;
    }
    
    void func_flipstr( alu_block_t *dst )
    {
    	char *str = dst->block, c;
    	size_t n, v;
    	
    	for ( n = 0, v = dst->bytes.used; n < v; ++n, --v )
    	{
    		c = str[n];
    		str[n] = str[v];
    		str[v] = c;
    	}
    }
    
    int print_value( alu_t *alu, char *num, size_t size, size_t base )
    {
    	uint_t tmp = -1;
    	int ret = alu_get_reg( alu, &tmp, sizeof(size_t) );
    	alu_block_t src = {0};
    	long nextpos = 0;
    	
    	if ( ret != 0 )
    	{
    		alu_error(ret);
    		return ret;
    	}
    	
    	src.block = num;
    	src.bytes.upto = size;
    	src.bytes.last = size - 1;
    	src.bytes.used = strnlen( num, size - 1 );
    	
    	alu_printf( "num = '%s'", num );
    	
    	ret = alu_str2reg(
    		alu, &src, tmp,
    		(alu_func_rdChar32_t)func_rdChar32,
    		&nextpos,
    		base, false
    	);
    	
    	if ( ret != 0 )
    	{
    		alu_error(ret);
    		return ret;
    	}
    	
    	(void)memset( &src, 0, sizeof(alu_block_t) );
    	ret = alu_block( &src, size, 0 );
    	
    	if ( ret != 0 )
    	{
    		alu_error(ret);
    		return ret;
    	}
    	
    	ret = alu_reg2str(
    		alu, &src, tmp,
    		(alu_func_wrChar32_t)func_wrChar32,
    		(alu_func_flipstr_t)func_flipstr,
    		base, false
    	);
    	
    	if ( ret != 0 )
    		alu_error(ret);
    	
    	(void)alu_printf( "alu = '%s'", (char*)(src.block) );
    	alu_block_release( &src );
    	
    	return ret;
    }
    ...
    int alu_str2reg
    (
    	alu_t *alu,
    	void *src,
    	uint_t dst,
    	alu_func_rdChar32_t nextchar,
    	long *nextpos,
    	size_t base,
    	bool lowercase
    )
    {
    	size_t b, perN;
    	alu_reg_t *MUL, *VAL, *DST;
    	alu_bit_t n;
    	int ret;
    	uint_t mul = -1, val = -1;
    	char32_t *V, c = 0;
    	char *base_str = lowercase ?
    		ALU_BASE_STR_0toztoZ :
    		ALU_BASE_STR_0toZtoz;
    	
    	if ( !nextpos )
    		return EDESTADDRREQ;
    	
    	if ( *nextpos < 0 )
    		*nextpos = 0;
    	
    	ret = alu_check1( alu, dst );
    	
    	if ( ret != 0 )
    		return ret;
    	
    	if ( !val || !nextchar )
    		return EADDRNOTAVAIL;
    	
    	if ( base < 2 || base > strlen(base_str) )
    		return ERANGE;
    		
    		
    	ret = alu_get_reg( alu, &val, sizeof(size_t) );
    		
    	if ( ret != 0 )
    		return ret;
    	
    	ret = alu_get_reg( alu, &mul, sizeof(size_t) );
    		
    	if ( ret != 0 )
    	{
    		alu_rem_reg( alu, val );
    		return ret;
    	}
    	
    	/* Hook registers */
    	MUL = alu->regv + mul;
    	VAL = alu->regv + val;
    	DST = alu->regv + dst;
    
    	/* Clear all registers in direct use */
    	perN = alu->buff.perN;
    	
    	V = MUL->part;
    	*V = base;
    	
    	V = VAL->part;
    	n = DST->last;
    	
    	alu_reset_reg( alu, dst, false );
    	memset( DST->part, 0, alu->buff.perN );
    
    	do
    	{	
    		/* Failsafe if failed to get character but return code is 0 */
    		c = -1;
    		ret = nextchar( &c, src, nextpos );
    		
    		if ( ret != 0 && ret != EOF )
    			return ret;
    		
    		for ( b = 0; b < base; ++b )
    		{
    			if ( c == (char32_t)(base_str[b]) )
    				break;
    		}
    		
    		if ( b == base )
    			break;
    		
    		++(*nextpos);
    		
    		if ( *(n.S) & n.B )
    		{
    			ret = alu_setup_reg(
    				alu, alu->buff.qty.upto, perN + sizeof(size_t)
    			);
    			
    			if ( ret != 0 )
    				return ret;
    				
    			(void)alu_reset_reg( alu, dst, false );
    			
    			perN = alu->buff.perN;
    			V = VAL->part;
    			n = DST->last;
    		}
    		
    		*V = c;
    		
    		(void)alu_mul( alu, dst, mul );
    		(void)alu_add( alu, dst, val );
    	}
    	while ( ret == 0 );
    	
    	alu_rem_reg( alu, mul );
    	alu_rem_reg( alu, val );
    	
    	return 0;
    }
    
    int alu_reg2str
    (
    	alu_t *alu,
    	void *dst,
    	uint_t src,
    	alu_func_wrChar32_t nextchar,
    	alu_func_flipstr_t flipstr,
    	size_t base,
    	bool lowercase
    )
    {
    	alu_reg_t *DIV, *VAL, *NUM;
    	int ret;
    	uint_t num = -1, div = -1, val = -1;
    	char32_t *V;
    	char *base_str = lowercase ?
    		ALU_BASE_STR_0toztoZ :
    		ALU_BASE_STR_0toZtoz;
    		
    	ret = alu_check1( alu, src );
    	
    	if ( ret != 0 )
    		return ret;
    	
    	if ( !val || !nextchar || !flipstr )
    		return EADDRNOTAVAIL;
    	
    	if ( base < 2 || base > strlen(base_str) )
    		return ERANGE;
    	
    	ret = alu_get_reg( alu, &num, sizeof(size_t) );
    		
    	if ( ret != 0 )
    		return ret;
    		
    	ret = alu_get_reg( alu, &val, sizeof(size_t) );
    		
    	if ( ret != 0 )
    	{
    		(void)alu_rem_reg( alu, num );
    		return ret;
    	}
    	
    	ret = alu_get_reg( alu, &div, sizeof(size_t) );
    		
    	if ( ret != 0 )
    	{
    		(void)alu_rem_reg( alu, num );
    		(void)alu_rem_reg( alu, val );
    		return ret;
    	}
    	
    	/* Hook registers */
    	DIV = alu->regv + div;
    	VAL = alu->regv + val;
    	NUM = alu->regv + num;
    	
    	(void)alu_mov( alu, num, src );
    	
    	V = DIV->part;
    	*V = base;
    	
    	V = VAL->part;
    
    	do
    	{	
    		(void)alu_divide( alu, num, div, val );
    		ret = nextchar( base_str[*V], dst );
    		
    		if ( ret != 0 )
    			return ret;
    	}
    	while ( alu_compare( *NUM, *DIV, NULL ) >= 0 );
    	
    	alu_rem_reg( alu, num );
    	alu_rem_reg( alu, div );
    	alu_rem_reg( alu, val );
    	
    	ret = nextchar( 0, dst );
    		
    	if ( ret != 0 )
    		return ret;
    	
    	flipstr( dst );
    	
    	return 0;
    }
    General reminder the full code is hosted at GitHub - awsdert/alu: Library modeled after the Arithmetic Logic Unit built into CPUs

  14. #14
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Well just spotted the 1st mistake, used *V = c; instead of *V = b; in alu_str2reg(), started getting correct register value after that.
    alu_reg2str() however is not quite there, forgot to pass the final value of num to add the last digit, rectified now but still being cleared for some reason:
    Code:
    int alu_reg2str
    (
    	alu_t *alu,
    	void *dst,
    	uint_t src,
    	alu_func_wrChar32_t nextchar,
    	alu_func_flipstr_t flipstr,
    	size_t base,
    	bool lowercase
    )
    {
    	alu_reg_t *DIV, *VAL, *NUM;
    	int ret;
    	uint_t num = -1, div = -1, val = -1;
    	size_t *V, *N;
    	char *base_str = lowercase ?
    		ALU_BASE_STR_0toztoZ :
    		ALU_BASE_STR_0toZtoz;
    		
    	ret = alu_check1( alu, src );
    	
    	if ( ret != 0 )
    		return ret;
    	
    	if ( !val || !nextchar || !flipstr )
    		return EADDRNOTAVAIL;
    	
    	if ( base < 2 || base > strlen(base_str) )
    		return ERANGE;
    	
    	ret = alu_get_reg( alu, &num, sizeof(size_t) );
    		
    	if ( ret != 0 )
    		return ret;
    		
    	ret = alu_get_reg( alu, &val, sizeof(size_t) );
    		
    	if ( ret != 0 )
    	{
    		(void)alu_rem_reg( alu, num );
    		return ret;
    	}
    	
    	ret = alu_get_reg( alu, &div, sizeof(size_t) );
    		
    	if ( ret != 0 )
    	{
    		(void)alu_rem_reg( alu, num );
    		(void)alu_rem_reg( alu, val );
    		return ret;
    	}
    	
    	/* Hook registers */
    	DIV = alu->regv + div;
    	VAL = alu->regv + val;
    	NUM = alu->regv + num;
    	
    	(void)alu_mov( alu, num, src );
    	
    	V = DIV->part;
    	*V = base;
    	
    	N = NUM->part;
    	V = VAL->part;
    
    	do
    	{	
    		(void)alu_divide( alu, num, div, val );
    		ret = nextchar( base_str[*V], dst );
    		
    		if ( ret != 0 )
    		{
    			alu_rem_reg( alu, num );
    			alu_rem_reg( alu, div );
    			alu_rem_reg( alu, val );
    			return ret;
    		}
    	}
    	while ( alu_compare( *NUM, *DIV, NULL ) >= 0 );
    	
    	ret = nextchar( base_str[*N], dst );
    	
    	alu_rem_reg( alu, num );
    	alu_rem_reg( alu, div );
    	alu_rem_reg( alu, val );
    	
    	if ( ret != 0 )
    		return ret;
    	
    	ret = nextchar( 0, dst );
    		
    	if ( ret != 0 )
    		return ret;
    	
    	flipstr( dst );
    	
    	return 0;
    }

  15. #15
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,048
    Realised why I was getting a blank string, had to remove the extra NUL character I was adding and modify flipstr() to set v to used - 1.
    Apparently division is still having an issue somewhere down the line because 12 / 10 results in 4 atm.

    Edit: Resolved it, had to correct the final shift after the loop to this:
    Code:
    	if ( R->init.b )
    		ret = alu__shl( alu, num, bits + 1 );
    Last edited by awsdert; 08-20-2020 at 05:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can anyone help to spot mistake in the code
    By chess_queen in forum C Programming
    Replies: 1
    Last Post: 10-21-2012, 10:37 AM
  2. Can you spot my mistake?
    By Brewer in forum C Programming
    Replies: 13
    Last Post: 11-12-2006, 12:50 PM
  3. going to a certain spot in a file...
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2002, 02:31 AM

Tags for this Thread