Print a number in any base without help
Repeated division for the digits, a buffer filled backwards from its end, and the same number printed in base 16, 10 and 2.
48879 printed three times, as BEEF, as 48879 and as 1011111011101111, by a subroutine that
turns a number into characters itself. Task 15 does the same job in one request; this is what it does
inside, and it is the program every language writes once and then hides in a library.
Read two numbers and print their sum handed a number to a task and got text back. Here the only task used is the one that prints a string, and everything between the number and the string is yours.
You need to know: the "Multiply and divide, with the remainder" Example and the "trap #15 and
its tasks" lecture. What is new here is that the digits come out backwards, the last one first, so
the buffer is filled from its end towards its front with -(a1).
Dividing by the base and keeping the remainder gives you one digit, and it is the lowest one:
48879 divided by 16 is 3054 with 15 left over, and 15 is the F at the right hand end of BEEF. So
the digits arrive in the opposite order to the one they are printed in, and the two ways round that
are to reverse the buffer afterwards or to write it backwards in the first place. -(a1) does the
second one for nothing: it subtracts 1 from a1 and then writes, so every digit lands in front of
the ones already there and a1 is left pointing at the first character.
A digit is a number from 0 to base - 1 and it has to become a character. '0' is $30, so adding
it turns 0 to 9 into '0' to '9'; 'A' is $41 and a 10 has to become that, so the amount added
is 'A'-10, worked out by the assembler while it assembles. cmp.b #9, d3 and bhi pick between
the two, which is what makes a base up to 36 work.
move.l d2, d0 is the loop's condition as well as its assignment: move sets Z from what it
moved, so bne under it means "if there is anything left of n, go round again". The buffer has 34
bytes because the longest answer is a 32 bit number in base 2, and after the binary run a1 comes
out at 00002011, seventeen bytes down from buffer_end.
Try changing move.l #2, d1 to move.l #36, d1, the largest base the digits reach. The third line
of the console becomes 11PR.