Skip to content
This repository was archived by the owner on Oct 6, 2026. It is now read-only.
justAnArthurPublic archive

About

RAM program that reverses the digits of a number, with unit and logarithmic cost analysis

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

Digit reversal on a RAM: A random-access machine program that writes a number's digits in reverse, with its time and space cost worked out per instruction.

Digit reversal on a RAM

A random-access machine (RAM) program that reads a whole number below 10000 and writes its digits in reverse order, so 2563 becomes 3652. Built for the TZIV course (project RAM) at FIIT STU in the 2022/23 summer semester.

Finished and archived. The report works out the unit and logarithmic time cost of every instruction.

What it does

  • Reads the number N into register R1 with a single read and rejects it if N ≥ 10000
  • Splits off the last digit (R1 − ⌊R1 / 10⌋ · 10) and stores it through the pointer in R3, filling R5, R6, … until R1 reaches 0
  • Writes the stored digits to the output tape from R5 upwards through a second pointer in R4, which gives the reversed number
  • 32 instructions in ram.ram, no subroutines: one loop to split the digits, one to write them

How it works

flowchart TD
  A["Read N into R1"] --> B{"N ≥ 10000?"}
  B -->|yes| R["Reject"]
  B -->|no| C{"R1 = 0?"}
  C -->|no| D["Store the last digit of R1 in the register R3 points to"]
  D --> E["R1 = ⌊R1 / 10⌋, R3 + 1"]
  E --> C
  C -->|yes| F["Write the register R4 points to, R4 + 1"]
  F --> G{"R3 − R4 > 0?"}
  G -->|yes| F
  G -->|no| H["Halt"]
Loading

Results

From the report and timeComplexity.xlsx, with n the number of digits:

Measure Result
Time, unit cost 23n + 10 steps, O(n)
One digit / four digits 32 / 101 steps (report summary, 23n + 9)
N ≥ 10000 rejected after 8 steps
Space 5 + ⌊log₁₀ N⌋ registers, O(log N)

The summary line in the report and its per-instruction table differ by one step (23n + 9 against 23n + 10). The logarithmic cost of each instruction is in the same table.

Run

Load ram.ram into SimStudio, put a number on the input tape and run it. With 2563 on the input, the output tape reads 3652.

Stack

SimStudio RAM program (ram.ram). Cost tables in Excel, report in Word.

Documentation

License

CC BY-NC-ND 4.0: share it with credit, but no changes and no commercial use. Don't hand it in as your own coursework.


Pôvodná dokumentácia (SK)

documentation_page-0001.jpg documentation_page-0002.jpg documentation_page-0003.jpg

About

RAM program that reverses the digits of a number, with unit and logarithmic cost analysis

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors