Moar log!

So logging strings is cool. But I also need to log numbers. So I’ve created a function that accepts a string and a 16 bit number, converts the number to a string, concatinates the message with the number and pass the resulting string on to the log function I’ve shown previously.

This is the resulting function:

subcall WriteLogNumber
{
  IN_S  Message 100
  IN_16 Number

  DATAS   NumberString  7
  DATAS   FullString    128
  DATA16  FiguresCounterNumber
  DATA16  FiguresCounter

  // Check if value is negative. In that case we need to make it positive
  JR_LT16( Number, 0, NegateNumber )
  MOVE16_16( Number, FiguresCounterNumber )
  JR( StartCounting )

NegateNumber:
  // Value is negative. Negate it back to positive and get going
  MUL16( Number, -1, FiguresCounterNumber )

StartCounting:
  JR_LT16( FiguresCounterNumber, 10, Figures10 )
  JR_LT16( FiguresCounterNumber, 100, Figures100 )
  JR_LT16( FiguresCounterNumber, 1000, Figures1000 )
  JR_LT16( FiguresCounterNumber, 10000, Figures10000 )
  MOVE16_16( 5, FiguresCounter )
  JR( CheckNegative )

Figures10:
  MOVE16_16( 1, FiguresCounter )
  JR( CheckNegative )

Figures100:
  MOVE16_16( 2, FiguresCounter )
  JR( CheckNegative )

Figures1000:
  MOVE16_16( 3, FiguresCounter )
  JR( CheckNegative )

Figures10000:
  MOVE16_16( 4, FiguresCounter )
  JR( CheckNegative )

CheckNegative:
  JR_GTEQ16( Number, 0, StartPrinting )   // If number is >= 0 then we can simply start printing
  ADD16( 1, FiguresCounter, FiguresCounter )

StartPrinting:
  STRINGS( NUMBER_TO_STRING, Number, FiguresCounter, NumberString )
  STRINGS( DUPLICATE, Message, FullString )
  STRINGS( ADD, FullString, NumberString, FullString )

  CALL( WriteLog, FullString )
}

And to try it out I did this code:

CALL( WriteLog, 'Hello log world' )
CALL( WriteLogNumber, 'Hello int16: ', 0 )
CALL( WriteLogNumber, 'Hello int16: ', 1 )
CALL( WriteLogNumber, 'Hello int16: ', 9 )
CALL( WriteLogNumber, 'Hello int16: ', 10 )
CALL( WriteLogNumber, 'Hello int16: ', 11 )
CALL( WriteLogNumber, 'Hello int16: ', 99 )
CALL( WriteLogNumber, 'Hello int16: ', 100 )
CALL( WriteLogNumber, 'Hello int16: ', 101 )
CALL( WriteLogNumber, 'Hello int16: ', 999 )
CALL( WriteLogNumber, 'Hello int16: ', 1000 )
CALL( WriteLogNumber, 'Hello int16: ', 1001 )
CALL( WriteLogNumber, 'Hello int16: ', 9999 )
CALL( WriteLogNumber, 'Hello int16: ', 10000 )
CALL( WriteLogNumber, 'Hello int16: ', 10001 )
CALL( WriteLog, 'And now, the negatives!' )
CALL( WriteLogNumber, 'Hello int16: ', -1 )
CALL( WriteLogNumber, 'Hello int16: ', -9 )
CALL( WriteLogNumber, 'Hello int16: ', -10 )
CALL( WriteLogNumber, 'Hello int16: ', -11 )
CALL( WriteLogNumber, 'Hello int16: ', -99 )
CALL( WriteLogNumber, 'Hello int16: ', -100 )
CALL( WriteLogNumber, 'Hello int16: ', -101 )
CALL( WriteLogNumber, 'Hello int16: ', -999 )
CALL( WriteLogNumber, 'Hello int16: ', -1000 )
CALL( WriteLogNumber, 'Hello int16: ', -1001 )
CALL( WriteLogNumber, 'Hello int16: ', -9999 )
CALL( WriteLogNumber, 'Hello int16: ', -10000 )
CALL( WriteLogNumber, 'Hello int16: ', -10001 )

That generates this log file:
Hello log world
Hello int16: 0
Hello int16: 1
Hello int16: 9
Hello int16: 10
Hello int16: 11
Hello int16: 99
Hello int16: 100
Hello int16: 101
Hello int16: 999
Hello int16: 1000
Hello int16: 1001
Hello int16: 9999
Hello int16: 10000
Hello int16: 10001
And now, the negatives!
Hello int16: -1
Hello int16: -9
Hello int16: -10
Hello int16: -11
Hello int16: -99
Hello int16: -100
Hello int16: -101
Hello int16: -999
Hello int16: -1000
Hello int16: -1001
Hello int16: -9999
Hello int16: -10000
Hello int16: -10001

Leave a Reply