Understand any programing language using chatbots

Generated by Windows Copilot

Using chatbots wisely

Original post

I suppose there’s a whole class of programmers for whom a compiler is akin to magic, but knowing what’s getting emitted and how it works is fundamental to your success over the long haul. Write some assembly. Read the Dragon book on an airplane. AI will still be here when you get back. In five years, anyone capable of structured thought will be able to become a prompt engineer, but only 1% of software developers will actually be able to debug the stuff that comes out the other end. Be the 1%

What did I do next?

Here’s the full transcription of the PDP-11 assembly code using MACRO-11 syntax, followed by a detailed explanation:
Transcribed Code

.TITLE  BTOA                ; Program title
.MCALL  .PRINT,.EXIT,.TTYOUT ; System macros

HELLOMSG: .ASCIZ /BTOA Example/

        .EVEN               ; Ensure we're on a word boundary

START:  .PRINT  #HELLOMSG
        MOV     #12345., R0 ; Number to convert
        JSR     PC, BTOA
        .EXIT

BTOA:
        MOV     R0, -(SP)   ; Save R0 on stack
        MOV     R1, -(SP)   ; Save R1 on stack
        MOV     R2, -(SP)   ; Save R2 on stack

        MOV     R0, R1      ; Move number to R1 (low part of dividend)
        CLR     R0          ; Clear R0 (high part of dividend)
        DIV     #10., R0    ; Divide R0:R1 by 10, quotient in R0, remainder in R1

        TST     R0          ; Check if quotient is 0
        BEQ     PRINT       ; If quotient is 0, print digit
        JSR     PC, BTOA    ; Recursive call with quotient

PRINT:  ADD     #'0, R1     ; Convert remainder to ASCII
        MOV     R1, R0      ; Move ASCII digit to R0
        .TTYOUT R0          ; Print the digit

        MOV     (SP)+, R2   ; Restore R2
        MOV     (SP)+, R1   ; Restore R1
        MOV     (SP)+, R0   ; Restore R0
        RTS     PC          ; Return

.END    START

🧠 Explanation

This program converts a binary number (in this case, 12345) to its ASCII decimal representation and prints it to the terminal.

🔹 Header and Setup

  • .TITLE BTOA: Sets the title of the program.
  • .MCALL .PRINT,.EXIT,.TTYOUT: Declares macros for printing, exiting, and terminal output.
  • HELLOMSG: .ASCIZ /BTOA Example/: Defines a null-terminated string.
  • .EVEN: Ensures the next instruction is aligned on a word boundary.

🔹 Main Routine (START)

  • .PRINT #HELLOMSG: Prints the greeting message.
  • MOV #12345., R0: Loads the number 12345 into register R0.
  • JSR PC, BTOA: Calls the BTOA subroutine to convert and print the number.
  • .EXIT: Ends the program.

🔹 Subroutine (BTOA)

  • Saves registers R0, R1, and R2 to the stack.
  • Sets up for division: moves the number to R1, clears R0 to form a 32-bit dividend.
  • DIV #10., R0: Divides R0:R1 by 10. Quotient goes to R0, remainder to R1.
  • TST R0: Tests if quotient is zero.
    • If yes, go to PRINT.
    • If not, recursively call BTOA with the quotient.
  • PRINT: Converts the remainder (digit) to ASCII by adding '0', then outputs it using .TTYOUT.
  • Restores registers from the stack and returns.

This recursive approach ensures digits are printed in the correct order (most significant to least significant). Would you like to see how this would look in C or another language for comparison?


Great question! 🧠

BTOA stands for Binary To ASCII. It’s a common abbreviation used in programming to describe the process of converting a binary (or numeric) value into its ASCII character representation, typically for display or output purposes.

🔍 What does “Binary to ASCII” mean?

  • Binary: In this context, it refers to a numeric value stored in binary format (e.g., the number 12345 in machine-readable form).
  • ASCII: The American Standard Code for Information Interchange — a character encoding standard that maps numbers to printable characters like '0' to '9', 'A' to 'Z', etc.

        MOV     R0, R1      ; Move number to R1 (low part of dividend)
        CLR     R0          ; Clear R0 (high part of dividend)
        DIV     #10., R0    ; Divide R0:R1 by 10, quotient in R0, remainder in R1

        TST     R0          ; Check if quotient is 0
        BEQ     PRINT       ; If quotient is 0, print digit
        JSR     PC, BTOA    ; Recursive call with quotient

This block of PDP-11 assembly code is the core of the binary-to-ASCII conversion logic. It performs division by 10 to extract digits from a number and uses recursion to print them in the correct order. Here’s a breakdown of each line:

🔍 Line-by-Line Explanation

18 MOV R0, R1 ; Move number to R1 (low part of dividend)
  • Copies the number from R0 to R1.
  • This sets up the 32-bit dividend for division: R0:R1 (high:low).
19 CLR R0 ; Clear R0 (high part of dividend)
  • Clears R0 to zero.
  • Now the full dividend is 0:R1, effectively treating the original number as a 32-bit value with high word = 0.
20 DIV #10., R0 ; Divide R0:R1 by 10, quotient in R0, remainder in R1
  • Performs a 32-bit division of R0:R1 by 10.
  • After this:
  • R0 contains the quotient (i.e., number / 10)
  • R1 contains the remainder (i.e., number % 10), which is the rightmost digit
22 TST R0 ; Check if quotient is 0
  • Tests if the quotient is zero.
  • This determines whether there are more digits to process.
23 BEQ PRINT ; If quotient is 0, print digit
  • If R0 is zero, it means this is the last digit.
  • Branches to PRINT to output the digit.
24 JSR PC, BTOA ; Recursive call with quotient
  • If R0 is not zero, it means there are more digits.
  • Recursively calls BTOA with the quotient in R0, continuing the process.

🧠 Summary

This block:

  • Extracts the least significant digit of the number.
  • Uses division and recursion to process digits from most significant to least significant.
  • Ensures digits are printed in the correct order by printing only after recursion unwinds.

Conclusion

Solve everyday issues with GenAI assistant

In this post I’d like to provide a short example of how chatbots, in particular Windows Copilot, can help you address issues related to web browsing, programming, reverse engineering, intelligent internet searching (aka semantic searching) etc.

The issue

In this case I wanted to check what sci-fi writer Pavel Amnuel had on his website. Even though that website looks dated and it is, the information it contains is still relevant to this day. There you can find his essays on hard science fiction, a “A Course of Lectures on TRIZ and the Development of Creative Imagination (PTB)” and more.

That course on Development of Creative Imagination consists of thirteen parts, and it turns out that the 11th one has incorrect encoding and looks like this at the time of writing

ÐÒ – ÝÒÎ Î×ÅÍÜ ÏÐÎÑÒÎ!

Ï.Àìíóýëü

Êóðñ ëåêöèé ïî ðàçâèòèþ òâîð÷åñêîãî âîîáðàæåíèÿ è òåîðèè ðåøåíèÿ èçîáðåòàòåëüñêèõ çàäà÷ äëÿ íà÷èíàþùèõ.
(Ïðîäîëæåíèå 11)


 òåêñòå èñïîëüçîâàíû íåêîòîðûå çàäà÷è ïî ÒÐÈÇ è ÐÒ èç êíèã è ñòàòåé Ã.Ñ.Àëüòøóëëåðà, Á.Ë.Çëîòèíà, À.Â.Çóñìàí è äðóãèõ àâòîðîâ.
 

ÝÒÈ ÔÀÍÒÀÑÒÈ×ÅÑÊÈÅ ÎÒÊÐÛÒÈß

Ìû ãîâîðèëè îá “îòêðûòèÿõ”, êîòîðûå äåëàåò ñàìà ïðèðîäà. Íî è â ÷åëîâå÷åñêîì îáùåñòâå íå îäíè ëèøü ðàáîòíèêè íàóêè ñîâåðøàþò îòêðûòèÿ. Íå íóæíî çàáûâàòü î êîíêóðåíòàõ – ïèñàòåëÿõ-ôàíòàñòàõ. Òîíêîñòÿìè íàó÷íûõ çíàíèé îíè âëàäåþò äàëåêî íå âñåãäà, íî ïðàâèëàìè ôàíòàçèðîâàíèÿ ïîëüçóþòñÿ êóäà ëó÷øå ó÷åíûõ.

How to fix it with GenAI?

So, what I did I’ve posted a part of it in Windows Copilot and asked about the encoding issue. It came up with a correct explanation right away.

prompt: fix encoding of this text …

response:
The text you’ve provided appears to be Cyrillic text misinterpreted as Latin characters due to incorrect character encoding. Most likely, it was originally written in Russian using Windows-1251 encoding, but then read or displayed using Windows-1252 (Latin-1) or UTF-8 without proper conversion.

🔍 Correct Encoding: Windows-1251 (Cyrillic)

When decoded properly, the text reads as:
РТВ — ЭТО ОЧЕНЬ ПРОСТО!
Г. Альтшуллер
Курс лекций по развитию творческого воображения и теории решения изобретательских задач для начинающих.
(Proverb 11)

В тексте есть некоторые задачи по ТРИЗ из РТВ, из книг и статей Г.С. Альтшуллера, Б.Л. Злотина, А.В.В. Асмана и других авторов.

🛠 How to fix encoding issues

If you encounter similar garbled text:

  1. Identify the original encoding — in this case, Windows-1251.
  2. Use a text editor or converter that allows you to change encoding (e.g., Notepad++, Sublime Text, iconv).
  3. Convert from Windows-1251 to UTF-8 to make it readable across modern systems.

Would you like me to help you convert a file or more text like this?


I’ve tried a couple of approaches, again, using Copilot’s assistance. In the end what I’ve ended up doing is saving the page as html file and updating the head element with the following meta tag below which provided correct Cyrillic encoding instead of the Latin one.

<meta charset=”windows-1251″>

This is how the properly encoded version looks like


I’ve also notified the author about this issue, and hopefully, by the time you are reading this post it will be fixed already.

Bonus

One more thing, if you’ve noticed the link to Pavel Amnuel’s website opens that page in English. This is no coincidence. The page is originally in Russian, but Copilot helped me to discover that Google Translate allows to translate a web page into a specific language and get a link to that translated page. How neat is that?


prompt: I mean I need a link that I can share after it was translated
response:
Ah, got it! You want a sharable link to the translated version of the page. Here’s how to do that using Google Translate:

✅ Create a sharable translated link

Use this format:
https://translate.google.com/translate?hl=en&sl=ru&u=https://milkyway2.com/amnuel/Rtv.htm
This link:

  • Can be shared with anyone — they’ll see the English version of the Russian page
  • Translates the page from Russian (sl=ru) to English (hl=en)
  • Automatically opens the translated version in Google Translate

By the way, while on that website you can right click on the mouse button and translate any of the articles into English. Nowadays, translations are of a very high quality.
Also, I saw that when you navigate to a translated page link, other linked pages are translated automatically!

Summary

Here you have it Windows Copilot or any other similar tool can do wonders if you ask it to help.