Question

I have an LC-3 program that has a counter. I can print the counter to the screen using TRAP x21. However, if the counter number has two digits my program does not work.

Example: 9 will print perfectly, but not 19.

I am guessing that before I print my Register which contains the counter I need a loop, which will chop the number by dividing by 2 (same as I would in decimal by dividing by 10, but 2 in binary). Then I guess I would print digit1, digit0. I have a problem though, how do I divide in LC-3? Right shift? That seems too hard for this problem and beyond my knowledge.

Please help.

    0010 000 000000011    ; R0 <= x30 which is for  
    0001 000 000 0 00 010 ; R0 <= R0 + R2 

    1111 0000 00100001    ; TRAP x21 
    1111 0000 00100101    ; TRAP x25 
    0000000000110000
Was it helpful?

Solution

It only works with one digit because of your number->digit routine. You're adding a number to the character '0', and obviously there's no character '11', etc.

You can find the maximal divisor by multiplying by ten (usually by repeated addition) until the divisor exceeds the number (then backing off to the previous value). This will let you use Gareth's method, above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top