> On Sep 9, 2025, at 8:52 PM, Martin Eberhard via cctalk 
> <[email protected]> wrote:
> 
> I just love the PDP11's assembly language. I needed a super-tight
> subroutine to print a 16-bit value as 6 octal digits. This is as tight as I
> could make it, 16 words (including writing to the serial port, which takes
> 5 words). Can you beat it?
> 
> ;*** Subroutine *************************
> ;Print a 16-bit value as 6 octal digits
> ;Calling Sequence:
> ;  jsr     PC,PROCT6
> ;On Entry:
> ;  R2 = value to print
> ;Trashes R1,R2
> ;****************************************
> PROCT6: mov     #100030,R1      ;Digit loop ends when '1' lands in C
>                                ;..'30' makes it ASCII
>        sec                     ;All done when this is in C again
> 
> ;Extract a digit and convert it to ASCII. Check for done.
> 1$:     rol     R2              ;Shift digit out of R2 & into R1
>        beq     3$              ;Return when done
> 
>        rol     r1              ;Build next octal digit
>        bcc     1$              ;Done when c = shift pattern bit
> 
> ;Write digit to the serial port
> 2$:     tstb    @#CTXSTA        ;Wait for transmitter (clears C)
>        bpl     2$
>        movb    R1,@#CTXDAT     ;Transmit now
> 
> ;Next digit
>        mov     #020006,R1      ;Digit ends when "2" lands in C.
>                                ;Printing ends when "sec" bit
>                                ;..leaves R2. '6' makes ASCII
>        br      1$
> 
> 3$:    rts PC
> 
> Martin E.

Very nice.  Here is the code used in the KMON (resident part of the keyboard 
monitor, i.e., CLI) of RT-11 V2.  I think it was created by Anton Chernoff.

OPRINT: MOV     #30,R0          ;CONVERT WORD TO OCTAL AND PRINT IT
        SEC
4$:     ROL     R2              ;DON'T TRY TO UNDERSTAND THIS ROUTINE
        ROLB    R0              ;  JUST USE IT & LOVE IT
        .TTYOUT
        MOV     #206,R0
5$:     ASL     R2              ;DONE YET ?
        BEQ     6$              ;YES
        ROLB    R0
        BCS     5$
        BR      4$
6$:     <exit>

Same approach but yours is shorter because it forms the next digit in one place 
rather than two.  Interesting that was missed in the earlier version.

        paul

Reply via email to