[NXC][API][SE] Use of bcd2dec

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Use of NXC function bcd2dec

Post by mattallen37 »

I am not sure how to see the generated NBC code, other than uploading the rxe from the NXT and opening it with BCC. Anyhow, here is the NXC program.

Code: Select all

task main()
{
  byte dec = bcd2dec(101);
  NumOut(0, LCD_LINE1,dec);
  while(true);
}
I tried many variations such as changing the input number, using int instead of byte, global variable instead of local... It always displays 0. If I use this line instead, byte dec = bcd2dec(101)+7; it displays 7, so it seems obvious that it is the bcd2dec function that has an issue.

Here is the generated NBC code, uploaded from the NXT.

Code: Select all

/*
testing bcd2dec.rxe
Header
----------------
FormatString = MindstormsNXT
Version = 6
DSCount = 24 (0x18)
DSSize = 72 (0x48)
DSStaticSize = 52 (0x34)
DSDefaultsSize = 26 (0x1A)
DynDSDefaultsOffset = 6 (0x6)
DynDSDefaultsSize = 20 (0x14)
MemMgrHead = 0 (0x0)
MemMgrTail = 1 (0x1)
DVArrayOffset = 52 (0x34)
ClumpCount = 2 (0x2)
CodespaceCount = 64 (0x40)
----------------
DataSpace
----------------
DSTOC
  sl0000: 06 01 0000
  sl0001: 06 01 0004
  ul0002: 05 01 0008
  sl0003: 06 01 000C
  ul0004: 05 01 0010
  sl0005: 06 01 0014
  ub0006: 01 01 0018
  sb0007: 02 00 0019
  ub0008: 01 01 001A
  ub0009: 01 01 001B
  ub000A: 01 01 001C
  ub000B: 01 01 001D
  sb000C: 02 00 001E
  ub000D: 01 01 001F
  sb000E: 02 00 0020
  sb000F: 02 00 0021
   c0010: 08 00 0004
  sb0011: 02 01 0024
   c0012: 08 00 0002
  sw0013: 04 01 0028
  sw0014: 04 01 002A
   a0015: 07 00 002C
  ub0016: 01 00 0000
  ul0017: 05 01 0030
Static DS Defaults
65 0A 10 07 01 00 
Dynamic DS Defaults
Dope Vectors (offset, elem size, count, back ptr, link)
00 34 00 0A 00 02 FF FF 00 01
FF FF 00 01 00 00 00 2C FF FF
----------------
ClumpRecords
----------------
2 record(s) (Fire Cnt, Dependent Cnt, Code Start)
t000: 00 00 0000
t001: 01 00 003E
----------------
*/
; -------------- variable declarations --------------
dseg	segment
;------- definitions -------
c0012_def	struct
sw0013	sword
sw0014	sword
c0012_def	ends
c0010_def	struct
sb0011	sbyte
c0012	c0012_def	
a0015	byte[]
ul0017	dword
c0010_def	ends
;------- declarations -------
sl0000	sdword	
sl0001	sdword	
ul0002	dword	
sl0003	sdword	
ul0004	dword	
sl0005	sdword	
ub0006	byte	
sb0007	sbyte	101
ub0008	byte	
ub0009	byte	
ub000A	byte	
ub000B	byte	
sb000C	sbyte	10
ub000D	byte	
sb000E	sbyte	16
sb000F	sbyte	7
c0010	c0010_def	
dseg	ends
; -------------- program code --------------
	thread t000
	subcall	t001, ub0008
	set	ub0006, 0x65
	div	ub000B, sb0007, sb000E
	mod	ub000A, ub0006, sb000E
	mul	sl0003, ub000B, sb000C
	add	sl0003, sl0003, ub000A
	add	ub0009, ul0004, sb000F
	set	sl0001, 0x0
	set	sl0005, 0x38
	mov	ul0002, ub0009
	mov	c0010.c0012.sw0013, sl0001
	mov	c0010.c0012.sw0014, sl0005
	set	c0010.ul0017, 0x0
	numtostr	c0010.a0015, ul0002
	syscall	DrawText, c0010
	mov	sl0000, c0010.sb0011
lbl0030:	set	sl0000, 0x1
	tst	NEQ, ub000D, sl0000
	brtst	EQ, lbl003B, ub000D
	jmp	lbl0030
lbl003B:	exit	-1, -1
	endt
;-----------------------------------
	thread t001
	subret	ub0008
	endt
Hopefully that will help you.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Use of NXC function bcd2dec

Post by muntoo »

mattallen37 wrote:I am not sure how to see the generated NBC code, other than uploading the rxe from the NXT and opening it with BCC.
Press F12 after compiling.

(So, F5/F6, then F12.)
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Use of NXC function bcd2dec

Post by mattallen37 »

muntoo wrote:Press F12 after compiling.

(So, F5/F6, then F12.)
Ah, thank you. Here is the NBC code doing it that way.

Code: Select all

dseg	segment
;------- definitions -------
Location_def	struct
X	sword
Y	sword
Location_def	ends
__TextOutArgs_def	struct
Result	sbyte
Location	Location_def	
Text	byte[]
Options	dword
__TextOutArgs_def	ends
;------- declarations -------
__D0main	sdword	
__signed_stack_001main	sdword	
__DU0main	dword	
__D0bcd2dec_inline_main	sdword	
__DU0bcd2dec_inline_main	dword	
__signed_stack_002main	sdword	
__bcd2dec_7qG2_bcd_7qG2_000_inline_main	byte	
__constVal101	sbyte	101
____initialize_global_data_return	byte	
__main_7qG2_dec_7qG2_000	byte	
__bcd2DecOnes	byte	
__bcd2DecTens	byte	
__constVal10	sbyte	10
__zfmain	byte	
__constVal16	sbyte	16
__constVal7	sbyte	7
__TextOutArgs	__TextOutArgs_def	
dseg	ends
;------- code -------
thread main
	subcall __initialize_global_data, ____initialize_global_data_return
	set __bcd2dec_7qG2_bcd_7qG2_000_inline_main, 101
#pragma macro 6
	div __bcd2DecTens, __constVal101, __constVal16
	mod __bcd2DecOnes, __bcd2dec_7qG2_bcd_7qG2_000_inline_main, __constVal16
	mul __D0bcd2dec_inline_main, __bcd2DecTens, __constVal10
	add __D0bcd2dec_inline_main, __D0bcd2dec_inline_main, __bcd2DecOnes
	add __main_7qG2_dec_7qG2_000, __DU0bcd2dec_inline_main, __constVal7
	set __signed_stack_001main, 0
	set __signed_stack_002main, 56
	mov __DU0main, __main_7qG2_dec_7qG2_000
	mov __TextOutArgs.Location.X, __signed_stack_001main
	mov __TextOutArgs.Location.Y, __signed_stack_002main
	set __TextOutArgs.Options, 0
	numtostr __TextOutArgs.Text, __DU0main
	syscall 13, __TextOutArgs
	mov __D0main, __TextOutArgs.Result
__NXC_Label_562:
	set __D0main, 1
	tst 5, __zfmain, __D0main
	brtst 4, __NXC_Label_563, __zfmain
	jmp __NXC_Label_562
__NXC_Label_563:
	exit -1, -1
endt
;------------------------
subroutine __initialize_global_data
	subret ____initialize_global_data_return
ends
;------------------------
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Use of NXC function bcd2dec

Post by afanofosc »

The NBC code shows that it is a signed/unsigned register bug. Try this as a workaround while waiting for a new compiler release:

Code: Select all

inline byte bcd2dec_fixed(byte bcd) { asm { __bcd2dec(bcd, __URETVAL__) } }
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests