so I was looking for how output works in lc3 but none of them were clear. all I know is that it has something to do with a trap function. so I just would like to know how to print a number to the console.
LC-3's output trap prints characters to the console — and that is all it can do. This is how computers with consoles actually work — character output only.
You can print anything you need using character output only: strings, spaces, newlines, tabs, numbers, etc...
If you want multiple digit numeric output, that consists of multiple characters that are individual digit characters. If you want formatted output, that requires characters for formatting — numbers and strings, interspersed with spaces, newlines as desired.
So, for numeric output you print as many characters as you need in order to print the digits for the number.
Conversion algorithms for number to string are often called itoa
— for integer to ascii. They convert the number to digit characters either going left to right or right to left.
A right to left approach computes value mod (%) 10 in order to extra one digit at a time, the does div (/) 10 and loops until nothing (zero) is left. The digits, however, come out backwards, so they have to be buffered in order to reverse them so they can come out forwards.
A left to right approach starts with the maximum power of 10 as a divisor (e.g. 10,000 for 16-bit numbers, smaller for smaller number ranges), and produces one digit at a time in that manner repeating with the remainder after the division for the next digit (using the next smaller power of ten).
Complicating things is that either approach requires mod and div, which are not available directly in LC-3, but both can be had at once by repetitive subtraction.
Hex output is somewhat simpler in that it does not require divide or mod by 10, but does require a fair amount of careful shifting (which LC-3 can only do one bit at a time, and to the left, by adding something to itself).